Advertisement
Guest User

Untitled

a guest
Feb 14th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.08 KB | None | 0 0
  1. from time import sleep, time
  2. import logging
  3. import praw
  4. import prawcore
  5.  
  6. logger = logging.getLogger(__name__)
  7. logging.basicConfig(filename='error_log.txt',
  8.                     filemode='a',
  9.                     level=logging.INFO,
  10.                     format='%(asctime)s %(levelname)s %(name)s %(message)s')
  11.  
  12. reddit = praw.Reddit(client_id='ID',
  13.                      client_secret='Secret',
  14.                      password='Password',
  15.                      user_agent='User Agent',
  16.                      username='Username')
  17.  
  18.  
  19. def run_bot():
  20.     start = time()
  21.     tries = 0
  22.     while tries < 5:
  23.         try:
  24.             for submission in reddit.subreddit("subreddit").new(limit=10):
  25.                 if submission.created_utc > start:
  26.                     handle(submission)
  27.         except Exception as error:
  28.             logger.exception(error)
  29.             tries += 1
  30.         else:
  31.             sleep(30)
  32.     else:
  33.         logger.error('Excessive restarts.')
  34.        
  35.  
  36. def handle(submission):
  37.     print("Match found! Submission ID: " + submission.id)
  38.     print("Written by: " + submission.author.name)
  39.     if respond:
  40.         print("Reply succesful!")
  41.  
  42.  
  43. def respond(submission):
  44.     try:
  45.         reddit.redditor(submission.author.name).message('Title', "Message")
  46.     except prawcore.exceptions.RequestException as error:
  47.         logger.exception(error)
  48.         logger.error('Unable to send message:\t{}'.format(submission.id))
  49.     else:
  50.         try:
  51.             submission.reply("PM'D")
  52.         except prawcore.exceptions.RequestException as error:
  53.             logger.exception(error)
  54.             logger.error('Unable to reply:\t{}'.format(submission.id))
  55.         else:
  56.             return True
  57.  
  58.  
  59. def match(submission):
  60.     words_to_match = ['word 1', 'word 2']
  61.     words_to_match2 = ['word 1', 'word 3']
  62.     if not all(string in submission.title.lower() for string in words_to_match):
  63.         return False
  64.     if not all(string in submission.title.lower() for string in words_to_match2):
  65.         return False
  66.     return True
  67.  
  68.  
  69. if __name__ == '__main__':
  70.     run_bot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement