Created
March 12, 2010 23:50
-
-
Save chromakode/330949 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "BODY:" | |
print comment["body"] | |
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