Welcome, guest | Sign In | My Account | Store | Cart

This is a simple Python script to watch a post.

Python, 77 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
"""
 $watch.py$ <4:41 PM 5/1/04>
 Watch That Post!
 A simple Python script to "watch" a post. I wrote this to
 watch posts on LiveJournal, but actually you can use it
 to watch any URL.

 Copyright (C) 2004 Premshree Pillai. All rights reserved.
 <http://www.qiksearch.com/>
 <http://www.livejournal.com/~premshree>

 To use the script:
	1. Create a file (e.g., urls.txt) and add the URLs
	   you want to watch - each URL on a new line.
	2. Change the variable period to whatever seconds
	   you want to let the script sleep.
	3. Run the program: python watch.py
"""

import urllib
import re
import sys
import time

def watch():
	urls = "urls.txt" # change this to whatever you want
	separator = " "
	change = 0
	try:
		fp = open(urls,"r")
	except IOError:
		fp = open(urls,"w")
		fp.close()
		sys.exit(0)

	urls_lines = fp.readlines()
	orig_content = ""
	count = 0
	for x in urls_lines:
		orig_content = orig_content + x
	fp.close()

	if len(urls_lines) == 0:
		sys.exit(0)

	rewrite_content = ""
	count = 0
	for url_line in urls_lines:
		url_and_size = re.split(" ", url_line)
		if len(url_and_size) == 1:
			url_and_size = re.split("\n", url_and_size[0])
			rewrite_content = rewrite_content + url_and_size[0] + separator + "0"
			change = 1
		else:
			url = url_and_size[0]
			size = url_and_size[1]
			size2 = len(urllib.urlopen(url).readlines())
			if int(size) != size2:
				print "New comment: " + url
				rewrite_content = rewrite_content + url + separator + str(size2)
				change = 1
			else:
				rewrite_content = rewrite_content + url_line
		#if count < len(urls_lines) - 1:
		#	rewrite_content = rewrite_content + "\n"
		count = count + 1

	if change == 1:
		fp = open(urls,"w")
		fp.write(rewrite_content)
		fp.close()

period = 100 # time in seconds...change it to whatever you want
while 1:
	watch()
	time.sleep(period)

LiveJournal doesn't have the feature to watch a post yet. I figured I should write something that would do something similar. So I wrote Watch That Post, a simple Python script that watches a post.

How To: 1. Create a file (urls.txt or whatever), and add the permalink URLs of the posts you want to watch - each URL on a new line. 2. Make changes to the source - change the value of the variable urls. You might also want to change the value of period 3. Run the program.

Of course, this is not something that everybody would like, but if you have an uninterrupted Internet connection, it'll work just fine.

By default the program alerts you about new posts via the console. You might want to redirect output to a file, if you like:

python watch.py > watch.txt

I wrote this to watch posts on LiveJournal (http://www.livejournal.com/), but you can watch any URL actually.