python reddit_comments.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python reddit_comments.py相关的知识,希望对你有一定的参考价值。

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()
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

以上是关于python reddit_comments.py的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python