Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import sleep, time
- import logging
- import praw
- import prawcore
- logger = logging.getLogger(__name__)
- logging.basicConfig(filename='error_log.txt',
- filemode='a',
- level=logging.INFO,
- format='%(asctime)s %(levelname)s %(name)s %(message)s')
- reddit = praw.Reddit(client_id='ID',
- client_secret='Secret',
- password='Password',
- user_agent='User Agent',
- username='Username')
- def run_bot():
- start = time()
- tries = 0
- while tries < 5:
- try:
- for submission in reddit.subreddit("subreddit").new(limit=10):
- if submission.created_utc > start:
- handle(submission)
- except Exception as error:
- logger.exception(error)
- tries += 1
- else:
- sleep(30)
- else:
- logger.error('Excessive restarts.')
- def handle(submission):
- print("Match found! Submission ID: " + submission.id)
- print("Written by: " + submission.author.name)
- if respond:
- print("Reply succesful!")
- def respond(submission):
- try:
- reddit.redditor(submission.author.name).message('Title', "Message")
- except prawcore.exceptions.RequestException as error:
- logger.exception(error)
- logger.error('Unable to send message:\t{}'.format(submission.id))
- else:
- try:
- submission.reply("PM'D")
- except prawcore.exceptions.RequestException as error:
- logger.exception(error)
- logger.error('Unable to reply:\t{}'.format(submission.id))
- else:
- return True
- def match(submission):
- words_to_match = ['word 1', 'word 2']
- words_to_match2 = ['word 1', 'word 3']
- if not all(string in submission.title.lower() for string in words_to_match):
- return False
- if not all(string in submission.title.lower() for string in words_to_match2):
- return False
- return True
- if __name__ == '__main__':
- run_bot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement