Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. import praw
  2. import time
  3.  
  4. r = praw.Reddit(user_agent='INSERT YOURS',
  5. client_id = 'INSERT YOURS',
  6. client_secret = 'INSERT YOURS',
  7. username = 'INSERT YOURS',
  8. password = 'INSERT YOURS')
  9. print('Logged in!')
  10.  
  11. haters = []
  12.  
  13. file = open("haters.txt","r")
  14.  
  15. line = "\n"
  16.  
  17. while "\n" in line:
  18. line = file.readline()
  19. haters.append(line.replace("\n",""))
  20. print(line.replace("\n",""))
  21.  
  22. file.close()
  23.  
  24. print(haters)
  25.  
  26. def reply(comment, message):
  27. print(str(comment.author.name in haters) + " "+ comment.author.name)
  28. if comment.author.name in haters:
  29. print("Good")
  30. return
  31.  
  32. try:
  33. print("Attempting to reply...")
  34. comment.reply(message+' \n ^^*beep* ^^*bop* ^^if ^^you ^^hate ^^me, ^^reply ^^with ^^\"stop\". ^^If ^^you ^^just ^^got ^^smart, ^^reply ^^with ^^"start".')
  35. print("Success! Searching for another..")
  36. except Exception as err:
  37. print("EXCEPTION HANDLED: ",err)
  38. error=str(err)
  39. if error[0:9]=="RATELIMIT":
  40.  
  41. index = error.index(" in ")
  42. num = error[index+4]
  43. isMinutes = error[index+4+2] == "m" or error[index+4+3] == "m"
  44.  
  45. if isMinutes:
  46. num=int(num)*60+30
  47. print("waiting %s seconds..." % (num))
  48. time.sleep(num) #bonus 30 gives the API some leeway
  49. else:
  50. num = (int(num)+1)*10+30
  51. print("waiting %s seconds..." % (num))
  52. time.sleep(int(num))
  53.  
  54. reply(comment,message)
  55. else:
  56. print("Crazy error! Abandon ship!\n"+error+"\nSearching for another...")
  57.  
  58. def removeHater(author):
  59. name = author.name
  60.  
  61. haters.remove(name)
  62.  
  63. file = open("haters.txt","r")
  64.  
  65. line = "\n"
  66.  
  67. contents = ""
  68.  
  69. while "\n" in line:
  70. line = file.readline()
  71. nl = line.replace("\n","")
  72. if(nl!=name):
  73. contents+=line
  74.  
  75. file.close()
  76.  
  77. hate = open("haters.txt","w")
  78. hate.write(contents)
  79. hate.close()
  80.  
  81. print("Added "+name)
  82.  
  83. r.redditor(name).message("You love me. :)",
  84. "You are off of the hater list. If this is a mistake, PM /u/Allons-why \n^^If ^^you ^^like ^^to ^^have ^^fun ^^a ^^lot, ^^go ^^to ^^/r/checks_out_bot",)
  85.  
  86. def addHater(author):
  87. name = author.name
  88. if name not in haters:
  89. haters.append(name)
  90. hate = open("haters.txt","r")
  91. contents = hate.read()
  92. hate.close()
  93. hate = open("haters.txt","w")
  94. hate.write(contents+"\n"+name)
  95. hate.close()
  96. print(name)
  97. r.redditor(name).message("You hate me. :(",
  98. "I have put you on my hater list at your request. I will no longer bother you. If this is a mistake, PM /u/Allons-why",)
  99.  
  100. negatives = ["don't","no","never"]
  101.  
  102. def run_bot():
  103. print("Grabbing sub...")
  104. subreddit = r.subreddit("all")
  105. print("Grabbing comments...")
  106. comments = subreddit.stream.comments()
  107. print("Searching...")
  108. for comment in comments:
  109. comment_text = " "+comment.body.lower().replace("\"","")
  110.  
  111. if "stop" in comment_text and not any(string in comment_text for string in negatives):
  112. try:
  113. parent = comment.parent()
  114. if parent.author.name == "checks_out_bot":
  115. addHater(comment.author)
  116. except:
  117. continue
  118. elif ("start" in comment_text) and (comment.author.name in haters):
  119. try:
  120. parent = comment.parent()
  121. if parent.author.name == "checks_out_bot":
  122. removeHater(comment.author)
  123. except Exception as err:
  124. print("start: "+str(err))
  125. continue
  126.  
  127. elif "it's funny because checks_out_bot's username is very applicable to their comment." in comment_text:
  128. reply(comment,"\\*beep\\* \\*boop\\* \\*beeeep\\* That is *my* thing!")
  129.  
  130. elif "username checks out" in comment_text or " name checks out" in comment_text:
  131. parent = comment.parent()
  132. if parent.author.name == "checks_out_bot":
  133. reply(comment, "Yes it does.")
  134. else:
  135. reply(comment,"It's funny because %s's username is very applicable to their comment."%(parent.author.name.replace("_","\\_")))
  136.  
  137. while True:
  138. try:
  139. run_bot()
  140. except Exception as err:
  141. print(err)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement