Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw
- import os
- import re
- import time
- import requests
- #set the user agent string
- user_agent = "Marco Rubio commentor v1"
- r = praw.Reddit(user_agent = user_agent)
- #login
- username = 'rubio_bot'
- password = 'password'
- #xXEWmqHYLEux56nXw9tNRa
- try:
- r.login(username, password, disable_warning=True)
- except requests.exceptions.ConnectionError:
- print requests.exceptions.ConnectionError
- #get the comments the program as already replied to from a file, else create an empty list
- if not os.path.isfile("posts_replied_to.txt"):
- posts_replied_to = []
- else:
- # Read the file into a list and remove any empty values
- with open("posts_replied_to.txt", "r") as f:
- posts_replied_to = f.read()
- posts_replied_to = posts_replied_to.split("\n")
- posts_replied_to = filter(None, posts_replied_to)
- #get the most recent comments from r/politics
- subreddit = r.get_subreddit('politics')
- while(1):
- print "Checking for comments about Obama..."
- posted = False
- subreddit_comments = subreddit.get_comments()
- flat_comments = praw.helpers.flatten_tree(subreddit_comments)
- for comment in flat_comments:
- if comment.id not in posts_replied_to:
- if re.search("Obama ", comment.body, re.IGNORECASE):
- # Reply to the post
- try:
- comment.reply("""Let's dispel once and for all with this fiction that
- Barack Obama doesn't know what he's doing, He knows exactly what he's doing.""")
- print "Bot replying to: ", comment.id
- posted = True
- except praw.errors.RateLimitExceeded:
- print "Rate limit exceeded. Sleeping five minutes."
- time.sleep(60 * 5 + 1)
- # Store the current id in our list
- posts_replied_to.append(comment.id)
- #Write our updated list back to the file and clear the list
- with open("posts_replied_to.txt", "a") as f:
- for post_id in posts_replied_to:
- f.write(post_id + "\n")
- print "Writing comment id: " + post_id + " to file"
- posts_replied_to = []
- if posted == True:
- print "Posted. Sleeping five minutes"
- time.sleep(60 * 5)
- else:
- print "Didn't find any comments about Obama. Sleeping one minute"
- time.sleep(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement