MCPcopy Create free account
hub / github.com/ActiveState/code / watch

Function watch

recipes/Python/282085_Watch_That_Post/recipe-282085.py:26–72  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

24import time
25
26def watch():
27 urls = "urls.txt" # change this to whatever you want
28 separator = " "
29 change = 0
30 try:
31 fp = open(urls,"r")
32 except IOError:
33 fp = open(urls,"w")
34 fp.close()
35 sys.exit(0)
36
37 urls_lines = fp.readlines()
38 orig_content = ""
39 count = 0
40 for x in urls_lines:
41 orig_content = orig_content + x
42 fp.close()
43
44 if len(urls_lines) == 0:
45 sys.exit(0)
46
47 rewrite_content = ""
48 count = 0
49 for url_line in urls_lines:
50 url_and_size = re.split(" ", url_line)
51 if len(url_and_size) == 1:
52 url_and_size = re.split("\n", url_and_size[0])
53 rewrite_content = rewrite_content + url_and_size[0] + separator + "0"
54 change = 1
55 else:
56 url = url_and_size[0]
57 size = url_and_size[1]
58 size2 = len(urllib.urlopen(url).readlines())
59 if int(size) != size2:
60 print "New comment: " + url
61 rewrite_content = rewrite_content + url + separator + str(size2)
62 change = 1
63 else:
64 rewrite_content = rewrite_content + url_line
65 #if count < len(urls_lines) - 1:
66 # rewrite_content = rewrite_content + "\n"
67 count = count + 1
68
69 if change == 1:
70 fp = open(urls,"w")
71 fp.write(rewrite_content)
72 fp.close()
73
74period = 100 # time in seconds...change it to whatever you want
75while 1:

Callers 1

recipe-282085.pyFile · 0.70

Calls 7

strFunction · 0.85
openFunction · 0.50
closeMethod · 0.45
exitMethod · 0.45
readlinesMethod · 0.45
splitMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected