Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.05 KB | None | 0 0
  1. import praw
  2. global wordsecond
  3.  
  4. app_id = "YOUR ID HERE"
  5. app_secret = "YOUR SECRET HERE"
  6. app_uri = 'http://127.0.0.1:65010/authorize_callback'
  7.  
  8. r = praw.Reddit('/u/mentionhelper reminds people who fail to mention users in the comments')
  9.  
  10. r.set_oauth_app_info(app_id, app_secret, app_uri)
  11. #url = r.get_authorize_url('...', 'account creddits privatemessages edit history identity read save submit', True)
  12. #print(url)
  13.  
  14.  
  15. app_account_code = 'YOUR CODE HERE'
  16.  
  17. #t = r.get_access_information("YOUR CODE HERE")
  18. #print(t)
  19.  
  20. app_refresh = 'YOUR REFRESH TOKEN HERE'
  21.  
  22. r.refresh_access_information('YOUR REFRESH TOKEN HERE')
  23. website = ['http://www.', 'http:', 'www.', '.com', "https:","/r/", "r/","edu/"]
  24. catcher = ['/u/', 'u/']
  25. blacklist = 'blacklist.txt'
  26. already_done_two = 'already_done_two.txt'
  27. def login():
  28.  
  29. r = praw.Reddit(app_ua)
  30. r.set_oauth_app_info(app_id, app_secret, app_uri)
  31. r.refresh_access_information(app_refresh)
  32. return r
  33.  
  34. login()
  35.  
  36. def updateBlacklist():
  37. mail = r.get_inbox()
  38. for message in mail:
  39. if any (key in message.body.lower() for key in ['stop']) and message.id not in already_done_two_content:
  40. fileZero = open(already_done_two, 'a')
  41. fileZero.write(message.id + '\n')
  42. fileZero.close()
  43. mention_auth_this = str(message.author)
  44. mention_auth_final_this = mention_auth_this.lower()
  45. print(mention_auth_final_this)
  46. print(blacklist_content)
  47. if mention_auth_final_this not in blacklist_content:
  48. fileOne = open(blacklist, 'a')
  49. fileOne.write(mention_auth_final_this + '\n')
  50. fileOne.close()
  51. message.reply('You have been successfully blacklisted. I won\'t bother you again!')
  52. else:
  53. message.reply('You have already been blacklisted.')
  54.  
  55.  
  56. def run():
  57.  
  58. limit = False
  59.  
  60. with open(blacklist, 'r') as blacklist_open:
  61. global blacklist_content
  62. blacklist_content = blacklist_open.readlines()
  63. blacklist_content = [x.strip() for x in blacklist_content]
  64.  
  65.  
  66.  
  67. with open(already_done_two, 'r') as already_done_two_open:
  68. global already_done_two_content
  69. already_done_two_content = already_done_two_open.read()
  70.  
  71.  
  72. subreddit = r.get_subreddit('all')
  73. submissions = subreddit.get_new(limit=200)
  74.  
  75.  
  76. for sub in submissions:
  77. to_remove = []
  78. number = 0
  79. mention_auth = str(sub.author)
  80. mention_auth_final = mention_auth.lower()
  81. caught = []
  82. variable = ''
  83. number = 0
  84. if sub.id not in already_done_two_content and any (key in sub.selftext for key in catcher):
  85.  
  86. fileTwo = open(already_done_two, 'a')
  87. fileTwo.write(sub.id + '\n')
  88. fileTwo.close()
  89. p = sub.selftext
  90. pewp = p.replace('#','')
  91. slip = pewp.replace('<', ' ')
  92. prop = slip.replace('>',' ')
  93. wel = prop.replace('- ', ' ')
  94. pra = wel.replace('~',' ')
  95. por = pra.replace(':',' ')
  96. para = por.replace('(',' ')
  97. x = para.replace(')',' ')
  98. y = x.replace('.', ' ')
  99. z = y.replace(',', ' ')
  100. h = z.replace(';', ' ')
  101. j = h.replace('!', ' ')
  102. mb = j.replace('*', ' ')
  103. body = mb.replace("'",' ').replace('\n',' ')
  104. split = body.split(' ')
  105. for word in split:
  106. if any(key in word for key in catcher):
  107. if word not in caught:
  108. wordsecond = word + '\n'
  109. if wordsecond not in caught:
  110. if len(caught) < 3:
  111. print(sub.permalink)
  112. final = word.replace('\n','')
  113. print('FINAL:', final)
  114. if final.replace('u/','').replace('/','').lower() != mention_auth_final:
  115. caught.append(final)
  116. print(caught[:])
  117. print(final)
  118. else:
  119. limit = True
  120.  
  121.  
  122.  
  123. for thing in caught:
  124. if any (key in thing for key in website):
  125. caught.remove(thing)
  126. print('Skipped website' + thing)
  127.  
  128.  
  129. print(len(caught))
  130. while number < len(caught):
  131. print('First letter:' + caught[number][0])
  132. if caught[number][0] != 'u':
  133. if caught[number][0] != '/':
  134. print('match')
  135. to_remove.append(caught[number])
  136. print(number)
  137. number += 1
  138.  
  139. for thingy in to_remove:
  140. caught.remove(thingy)
  141. for name in caught:
  142. if name.replace('u/','').replace('/','').lower() not in blacklist_content:
  143. pass
  144. else:
  145. caught.remove(name)
  146.  
  147. print(caught)
  148. for otherthing in caught:
  149. variable += '- ' + otherthing + '\n\n'
  150. newvar = variable.replace(',', '')
  151. finalvar = newvar.replace(')', '')
  152. otherfinalvar = finalvar.replace('(', '')
  153. if limit == True:
  154. otherfinalvar += '- *Further usernames omitted due to Reddit\'s limit of 3 mentions per comment.* \n\n'
  155. if len(caught) == 1 and mention_auth_final not in blacklist_content:
  156. #print("It looks like you're trying to mention another user, which only works if it's done in the comments, like this: \n\n " + variable + " \n *I am a bot, and this action was performed automatically. For all questions/concerns, please visit /r/smartmodbot.*")
  157. print('Left comment...')
  158. sub.add_comment("It looks like you're trying to mention another user, which only works if it's done in the comments like this (otherwise they don't receive a notification): \n\n" + otherfinalvar + '\n\n---\n\n ^I\'m ^a ^bot. ^Bleep. ^Bloop. ^| ^Visit ^/r/mentionhelper ^for ^discussion/feedback ^| ^Want ^to ^be ^left ^alone? ^Reply ^to ^this ^message ^with ^"stop"')
  159. elif len(caught) > 1 and mention_auth_final not in blacklist_content:
  160. print('Left comment...')
  161. #print("It looks like you're trying to mention other users, which only works if it's done in the comments, like this: ", *caught, sep='\n')
  162. sub.add_comment("It looks like you're trying to mention other users, which only works if it's done in the comments like this (otherwise they don't receive a notification): \n\n" + otherfinalvar + '\n\n---\n\n ^I\'m ^a ^bot. ^Bleep. ^Bloop. ^| ^Visit ^/r/mentionhelper ^for ^discussion/feedback ^| ^Want ^to ^be ^left ^alone? ^Reply ^to ^this ^message ^with ^"stop"')
  163. while True:
  164. try:
  165. run()
  166. updateBlacklist()
  167. except:
  168. pass
  169.  
  170.  
  171. #stop me with pkill -f other_bot.py
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement