Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. import praw
  2. import os
  3. import re
  4. import time
  5. import requests
  6.  
  7. #set the user agent string
  8.  
  9. user_agent = "Marco Rubio commentor v1"
  10. r = praw.Reddit(user_agent = user_agent)
  11.  
  12. #login
  13. username = 'rubio_bot'
  14. password = 'password'
  15.  
  16. #xXEWmqHYLEux56nXw9tNRa
  17.  
  18. try:
  19. r.login(username, password, disable_warning=True)
  20. except requests.exceptions.ConnectionError:
  21. print requests.exceptions.ConnectionError
  22.  
  23. #get the comments the program as already replied to from a file, else create an empty list
  24.  
  25. if not os.path.isfile("posts_replied_to.txt"):
  26. posts_replied_to = []
  27. else:
  28. # Read the file into a list and remove any empty values
  29. with open("posts_replied_to.txt", "r") as f:
  30. posts_replied_to = f.read()
  31. posts_replied_to = posts_replied_to.split("\n")
  32. posts_replied_to = filter(None, posts_replied_to)
  33.  
  34.  
  35. #get the most recent comments from r/politics
  36.  
  37. subreddit = r.get_subreddit('politics')
  38.  
  39. while(1):
  40. print "Checking for comments about Obama..."
  41. posted = False
  42.  
  43. subreddit_comments = subreddit.get_comments()
  44. flat_comments = praw.helpers.flatten_tree(subreddit_comments)
  45.  
  46. for comment in flat_comments:
  47. if comment.id not in posts_replied_to:
  48. if re.search("Obama ", comment.body, re.IGNORECASE):
  49. # Reply to the post
  50. try:
  51. comment.reply("""Let's dispel once and for all with this fiction that
  52. Barack Obama doesn't know what he's doing, He knows exactly what he's doing.""")
  53. print "Bot replying to: ", comment.id
  54. posted = True
  55.  
  56. except praw.errors.RateLimitExceeded:
  57. print "Rate limit exceeded. Sleeping five minutes."
  58. time.sleep(60 * 5 + 1)
  59.  
  60. # Store the current id in our list
  61. posts_replied_to.append(comment.id)
  62.  
  63. #Write our updated list back to the file and clear the list
  64. with open("posts_replied_to.txt", "a") as f:
  65. for post_id in posts_replied_to:
  66. f.write(post_id + "\n")
  67. print "Writing comment id: " + post_id + " to file"
  68. posts_replied_to = []
  69.  
  70. if posted == True:
  71. print "Posted. Sleeping five minutes"
  72. time.sleep(60 * 5)
  73. else:
  74. print "Didn't find any comments about Obama. Sleeping one minute"
  75. time.sleep(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement