Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import time
from urllib2 import urlopen, quote
import json
def fetch_comments(after=None):
url = "http://reddit.com/comments.json"
if after:
url += "?after={0}".format(quote(after))
#print "... Fetching {0} ...".format(url)
data = json.load(urlopen(url))["data"]
comments = [w["data"] for w in data["children"]]
return comments, data["after"]
def comment_link(comment):
return "http://reddit.com/comments/{0}/comment/{1}".format(comment["link_id"], comment["parent_id"])
class CommentFetcher(object):
def __init__(self, last=None):
self.last = last
def fetch(self):
if self.last is None:
comments, after = fetch_comments()
else:
comments = []
after = None
done = False
while not done:
cs, after = fetch_comments(after)
for comment in cs:
if comment["id"] == self.last:
done = True
break
else:
comments.append(comment)
time.sleep(2)
if comments:
self.last = comments[0]["id"]
return comments
import time
import re
from optparse import OptionParser
from reddit_comments import CommentFetcher, comment_link
alert_re = re.compile("wil wheaton wil wheaton wil wheaton", re.I)
def alert(comment):
print "================== WIL WHEATON ALERT =================="
print "FROM: " + comment["author"]
print
print "BODY:"
print comment["body"]
print
print "URL: " + comment_link(comment)
print "======================================================="
def run(verbose=False):
def log(text):
if verbose:
print text
cf = CommentFetcher()
while True:
log("Fetching new comments...")
comments = cf.fetch()
for comment in comments:
log("Got comment {0}".format(comment["id"]))
if alert_re.search(comment["body"]):
alert(comment)
time.sleep(35)
def main():
parser = OptionParser()
parser.add_option("-v", "--verbose",
action="store_true", dest="verbose", default=False,
help="enable verbose status messages")
(options, args) = parser.parse_args()
run(options.verbose)
if __name__=="__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.