Advertisement
Guest User

Untitled

a guest
Jan 12th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. import praw
  2. import time
  3. import os
  4.  
  5. def authenticate():
  6. print("Authenticating...")
  7. r = praw.Reddit('TTBot', user_agent = "TalkieToasterBot By Rubix")
  8. print("Authenticated as {}".format(r.user.me()))
  9. return r
  10.  
  11. def main():
  12. r = authenticate()
  13. comments_replied_to = get_saved_comments()
  14. while True:
  15. run_bot(r, comments_replied_to)
  16. time.sleep(10)
  17.  
  18.  
  19. def get_saved_comments():
  20. if not os.path.isfile("comments.txt"):
  21. comments_replied_to = []
  22. else:
  23. with open("comments.txt", "r") as f:
  24. comments_replied_to = f.read()
  25. comments_replied_to = comments_replied_to.split("\n")
  26. return comments_replied_to
  27.  
  28. words_to_look_for = [ {
  29. 'id': 'toast',
  30. 'target_text': ['toast', 'muffin', 'teacakes', 'teacake', 'buns', 'bun', 'baguettes', 'baguette', 'bagels', 'bagel', 'crumpets', 'crumpet', 'pancakes', 'pancake', 'hot cross buns', 'flapjacks', 'flapjack', 'waffle', 'waffles', 'toaster'],
  31. 'reply': 'Does anyone want toast?'
  32. },
  33. {
  34. 'id': 'talkie',
  35. 'target_text': ['Talkie', 'talkie'],
  36. 'reply': "Talkie's the name toastings the game."
  37. },
  38. {
  39. 'id': 'flapjack',
  40. 'target_text': ['No Flapjacks', 'no flapjacks'],
  41. 'reply': "So you're a waffle man eh?"
  42. },
  43. {
  44. 'id': 'bread',
  45. 'target_text': ['No Bread', 'no bread'],
  46. 'reply': "But I'm a toaster! It is my raison d'etre.. I toast, therefore I am. If you didn't want toast why did you repair me?"
  47. },
  48. {
  49. 'id': 'accident',
  50. 'target_text': ['accident'],
  51. 'reply': "That wasn't an accident! It was first degree toastercide."
  52. }]
  53.  
  54. def run_bot(r, comments_replied_to):
  55. print("Grabbing Subreddit...")
  56. for comment in r.subreddit('test4').comments(limit=25):
  57. comment_text = comment.body.lower()
  58. for item in words_to_look_for:
  59. isMatch = False
  60. isMatch = any(string in comment_text for string in item['target_text'])
  61. if comment.id not in comments_replied_to and not comment.author == r.user.me() and isMatch:
  62. comment.reply(item['reply'])
  63. print('Replying to ' + comment.id)
  64. comments_replied_to.append(comment.id)
  65. with open("comments.txt", "a") as f:
  66. f.write(comment.id + "\n")
  67. print("Successful Reply!")
  68. else:
  69. print("No Comments Found, Sleeping for 10 Seconds")
  70.  
  71.  
  72. if __name__ == '__main__':
  73. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement