Guest User

Untitled

a guest
Aug 1st, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. def bot_login():
  2. print "Loggin in..."
  3. r = praw.Reddit(username = config.username,
  4. password = config.password,
  5. client_id = config.client_id,
  6. client_secret = config.client_secret,
  7. user_agent = "phylohelper v0.1")
  8. print "Logged in!"
  9.  
  10. return r
  11.  
  12. def run_bot(r, comments_replied_to):
  13. print "Obtaining 10 comments..."
  14.  
  15. for comment in r.subreddit('whatsthissnaketest').comments(limit=10):
  16. # we still want all these checks other than the one checking if the keyword is in the comment
  17. if comment.author.name.lower() in list_of_names and comment.id not in comments_replied_to and comment.author != r.user.me() and datetime.utcfromtimestamp(comment.created_utc) > startTime:
  18. # now if it's a good comment, let's loop over all of our words
  19. for species in specieslist:
  20. # check if this word is in the comment
  21. if "*"+species+"*" in comment.body:
  22. print "String with \"+ species""\" found in comment " + comment.id
  23. # assuming your text file is the species name
  24. with open(species + ".txt", "r") as f:
  25. comment_reply = f.read()
  26. comment.reply(comment_reply + "\n\n" + sig)
  27. print "Replied to comment " + comment.id
  28.  
  29. comments_replied_to.append(comment.id)
  30.  
  31. with open ("comments_replied_to.txt", "a") as f:
  32. f.write(comment.id + "\n")
  33.  
  34. # assuming you only want to reply once to each comment, even if it has multiple keywords, so let's break out of the loop
  35. #break
  36.  
  37. # checking if there's brackets in the title with text in them is a little bit tricky to do normally, so we're going to use something called a regex
  38. # this is basically a fancy way of finding a certain string inside another string and it lets us do all kinds of fancy stuff like use wildcards
  39. for submission in r.subreddit('whatsthissnaketest').new(limit=10):
  40. # we're doing a couple things here. First we're calling the function re.findall, which is our fancy search function. We pass in our regex and the submission title
  41. # The regex is '\[.+\]'. We want to first find [, but that already has a special meaning in regex, so we need to escape it with the '\'. Then '.' means any character at all
  42. # and we modify it with '+', which means at least one, but then any number. Then we close it with another '\]' bracket. So something like '[test]' matches
  43. # but '[]' doesn't. And '[test' doesn't. And no brackets at all doesn't either. This function returns a list of the matches, so we can check the length of the list.
  44. # If that length is 0, there are no matches, and we should reply with our comment.
  45. if len(re.findall('\[.+\]', submission.title)) == 0:
  46. submission.reply("Please provide a geographic location")
  47.  
  48.  
  49. print "Sleeping for 30 seconds..."
  50. #Sleep for 30 seconds...
  51. time.sleep(30)
  52.  
  53.  
  54. def get_saved_comments():
  55. if not os.path.isfile("comments_replied_to.txt"):
  56. comments_replied_to = []
  57. else:
  58. with open("comments_replied_to.txt", "r") as f:
  59. comments_replied_to = f.read()
  60. comments_replied_to = comments_replied_to.split("\n")
  61. comments_replied_to = filter(None, comments_replied_to)
  62.  
  63. return comments_replied_to
  64.  
  65. r = bot_login()
  66. comments_replied_to = get_saved_comments()
  67. print comments_replied_to
  68.  
  69. while True:
  70. run_bot(r, comments_replied_to)
Add Comment
Please, Sign In to add comment