my_hat_stinks

Untitled

Feb 1st, 2015
7,674
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. #   alot-of-bot
  3. #   A bot for Alot
  4. #   --------------
  5. import os
  6. import time
  7. import praw
  8. import json
  9. import requests
  10.  
  11. from requests import HTTPError
  12.  
  13. #   Reddit
  14. #   ------
  15. reddit = praw.Reddit('ThanksAlot by /u/my_hat_stinks v1.3')
  16. reddit.set_oauth_app_info(client_id='', client_secret='', redirect_uri='http://127.0.0.1:65010/authorize_callback')
  17.  
  18. access_info = {
  19.   'access_token': '',
  20.   'refresh_token': '',
  21.   'scope': {'history', 'submit', 'edit', 'read', 'identity', 'save'}
  22. }
  23. reddit.refresh_access_information( access_info['refresh_token'] )
  24.  
  25. reddit.set_access_credentials( **access_info )
  26. alotbot = reddit.get_me()
  27.  
  28. subreddits = {}
  29.  
  30. #   Blacklist
  31. #   ---------
  32. blacklist_user = ["alot-of-bot", "AlotOfLoops", "WeylandTheDwarf"]
  33. blacklist_phrase = ["(#bot)", "(#nobots)"] # Don't respond to bots or people that don't want bots
  34.  
  35. #   Phrases
  36. #   -------
  37. phrase = {}
  38.  
  39. # Used when there's no custom subreddit footer
  40. footer_default = """
  41. ___
  42. _I'm just a bot! ^^Don't ^^hurt ^^me!_
  43. """
  44.  
  45. # Non-automated footer
  46. footer_manual = """
  47. ___
  48. _This comment was not automated!_
  49.  
  50. _^^Comment ^^will ^^be ^^removed ^^if ^^downvoted_ ^^| [_^^Bot ^^Source_](http://pastebin.com/JtBuWff4) ^^| ^^[_Confused?_](http://hyperboleandahalf.blogspot.co.uk/2010/04/alot-is-better-than-you-at-everything.html)
  51. """
  52.  
  53. # Generic footer text
  54. response_footer = """
  55. _^^Comment ^^will ^^be ^^removed ^^if ^^downvoted_ ^^| ^^[_Source_](http://pastebin.com/JtBuWff4) ^^| ^^[_Confused?_](http://hyperboleandahalf.blogspot.co.uk/2010/04/alot-is-better-than-you-at-everything.html)
  56.  
  57. """
  58.  
  59. #   Temporary variables
  60. #   -------------------
  61. last_id = {}       # ID of the last comment we checked
  62. last_time = {}     # Time of the last comment
  63. next_post = {}     # Time of next post for limited subreddits
  64.  
  65. for subname in subreddits: # Fill with placeholders
  66.     last_id[subname] = ''
  67.     last_time[subname] = 0
  68.  
  69. #   Search a subreddit
  70. #   ------------------
  71. def search_sub( subname ):
  72.     if 'limit' in subreddits[subname]:
  73.         if subname not in next_post: # We've restarted or something, we need to find our last post
  74.             next_post[subname] = 0 # If we've never posted
  75.             for comment in alotbot.get_comments(limit=100):
  76.                 if (comment.subreddit.display_name.lower())==subname: # It's the right sub
  77.                     next_post[subname] = comment.created_utc + (subreddits[subname]['limit'] * 3600) # Hours to seconds
  78.                     break
  79.         if next_post[subname]>time.time():
  80.             print( "Failed to search subreddit '"+subname+"', limit reached." )
  81.             return
  82.  
  83.     print( "Searching subreddit '" + subname + "'..." )
  84.     sub = reddit.get_subreddit(subname) # Get our subreddit
  85.  
  86.     try:
  87.         for comment in sub.get_comments(limit=100, place_holder=last_id[subname]):
  88.             text = comment.body.lower() # Lower case
  89.             if (comment.author.name in blacklist_user) or (any(string in text for string in blacklist_phrase)): # Blacklisted
  90.                 continue
  91.  
  92.             if (comment.id==last_id[subname]): # We've already checked it (This should be the last in the loop)
  93.                 continue
  94.  
  95.             if comment.saved: # It's saved, that means we've processed it before
  96.                 break
  97.  
  98.             if (comment.created_utc>last_time[subname]): # If this is the latest comment, store the data
  99.                 last_time[subname] = comment.created_utc
  100.                 last_id[subname] = comment.id
  101.  
  102.             for k in phrase: # Search each phrase
  103.                 if any(string in text for string in phrase[k]["trigger"]): # If it matches
  104.                     comment.save() # Simplest way to keep it saved between sessions - Save it on Reddit!
  105.                     print( "MATCH: " + k )
  106.                     print( "RESPOND: " + phrase[k]["response"] )
  107.  
  108.                     footer = ""
  109.                     if 'footer' in subreddits[subname]: # Custom subreddit footer
  110.                         footer = subreddits[subname]['footer'] + response_footer
  111.                     else:
  112.                         footer = footer_default + response_footer # Default footer
  113.  
  114.                     if 'source' in phrase[k]: # Image source
  115.                         footer = footer + phrase[k]['source']
  116.                     else:
  117.                         footer = footer + "^^Image ^^source ^^unknown" # Source unknown
  118.  
  119.                     footer = footer + " [](#bot)" # Tag our post as a bot post for other bots to see
  120.  
  121.                     comment.reply( phrase[k]["response"] + footer ) # Post reply
  122.  
  123.                     if 'limit' in subreddits[subname]:
  124.                         next_post[subname] = time.time() + (subreddits[subname]['limit'] * 3600)
  125.                         break # Break to prevent two replies when subreddit is limited
  126.     except AttributeError:
  127.         print( "Something went wrong! Missing attribute" ) # How did this even happen?
  128.  
  129. #   Scan submissions
  130. #   ----------------
  131. def alot_cleanup(): # Remove downvoted submissions
  132.     print( "Starting  alot-of-bot comment cleanup..." )
  133.     for comment in alotbot.get_comments(limit=100):
  134.         if comment.score<=-1:
  135.             print( "Removing comment!" )
  136.             comment.delete()
  137.         elif not comment.saved:
  138.             text = comment.body.lower()
  139.             if not (("#bot" in text) or ("#nobot" in text)): # Non-automated response
  140.                 comment.save()
  141.                 comment.edit( comment.body + footer_manual )
  142.  
  143. #   Update phrases
  144. #   --------------
  145. def update_phrases():
  146.     try:
  147.         global phrase
  148.         global subreddits
  149.         r = requests.get( "http://pastebin.com/raw/YJLqLbjk" )
  150.         newPhrase = json.loads( r.text )
  151.         if len(newPhrase)>0:
  152.             phrase = newPhrase
  153.        
  154.         r = requests.get( "http://pastebin.com/raw/4QB6mstL" )
  155.         newSubs = json.loads( r.text )
  156.         if len(newSubs)>0:
  157.             subreddits = newSubs
  158.             for subname in subreddits: # Fill with placeholders
  159.                 last_id[subname] = ''
  160.                 last_time[subname] = 0
  161.     except:
  162.         print( "Phrase update: Something went wrong!" )
  163.  
  164. #   Main
  165. #   ----
  166. next_cleanup = 1
  167. next_update = 1
  168. while True:
  169.     next_cleanup=next_cleanup-1
  170.     if next_cleanup<=0:
  171.         try:
  172.             alot_cleanup()
  173.         except HTTPError as err:
  174.             print( "ERROR: HTTP Error in Cleanup." )
  175.         except praw.errors.HTTPException as err:
  176.             print( "ERROR: Praw HTTP Error in Cleanup." )
  177.         except praw.errors.RateLimitExceeded as err:
  178.             print( "ERROR: Rate limit exceeded." )
  179.         next_cleanup=10
  180.        
  181.     next_update=next_update-1
  182.     if next_update<=0:
  183.         update_phrases()
  184.         next_update=1200
  185.  
  186.     print( "Starting checks..." )
  187.     for subname in subreddits:
  188.         try:
  189.             search_sub(subname)
  190.         except HTTPError as err:
  191.             print( "ERROR: HTTP Error in subreddit '" + subname + "'." )
  192.         except requests.exceptions.ReadTimeout as err:
  193.             print( "ERROR: Read timeout in subreddit '" + subname + "'." )
  194.         except praw.errors.RateLimitExceeded as err:
  195.             print( "ERROR: Rate limit exceeded." )
  196.         except praw.errors.HTTPException as err:
  197.             print( "ERROR: Praw HTTP Error in subreddit '" + subname + "'." )
  198.         except:
  199.             print( "Error: Something went wrong!" )
  200.     print( "Done!" )
  201.     time.sleep( 30 )
RAW Paste Data