Example #1
0
def main(subreddit):
        print "Subreddit :", subreddit
        rsub = url_data("http://www.reddit.com/r/%s/new/.json?sort=new"%subreddit, json=True)
        children = rsub['data']['children']
        r = Reddit(USERNAME, PASSWORD)
        session = r.login()
        f = open('history.txt', 'r')
        history = f.read()
        f.close()
        for child in children:
                is_self = child['data']['is_self']
                thread_id = child['data']['name']
                print thread_id
                if thread_id in history:
                        print "Thread: %s already in history"%thread_id
                        pass

                else:
                        if not is_self:
                                img_url = child['data']['url']
                                thread_id = child['data']['name']
                                repost = karmadecay(img_url)
                                if repost:
                                        text = form_comment(repost)
                                        r_resp = r.post(session, thread_id, text) 
                                        if r_resp != None:
                                                error = r_resp['json']['errors']
                                                delay = find_digit(error[0][1])
                                                print "waiting: %s seconds" %delay*60
                                                time.sleep(delay*60) 
                                                r.post(session, thread_id, text) 
                                        f = open('history.txt', 'a')
                                        f.write("\n%s"%thread_id)
                                        print text
                                        f.close()
                                        time.sleep(1)
                                        print "Comment Posted:", thread_id 
                                else:
                                        pass
                        else:
                                pass
        print "Finished"
        return