Advertisement
Guest User

crosspost_bot.py

a guest
Feb 2nd, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. from time import time, strftime, sleep
  2. import praw
  3.  
  4. source = 'oneplusone+oneplus2+oneplus3+oneplus3t+oneplusx'
  5. dest = 'oneplussupport'
  6. log = 'crosspost_log.txt'
  7.  
  8. reddit = praw.Reddit(
  9.     client_id='client_id',
  10.     client_secret='client_secret',
  11.     password='password',
  12.     user_agent='user_agent',
  13.     username='username'
  14. )
  15.  
  16.  
  17. def log_start():
  18.     """Log when the bot starts"""
  19.     s_time = strftime("%Y-%m-%d %H:%M:%S")
  20.     with open(log, 'a') as log_file:
  21.         log_file.write('Started {}\n'.format(s_time))
  22.  
  23.  
  24. def log_error(error):
  25.     """Log an error"""
  26.     e_time = strftime("%Y-%m-%d %H:%M:%S")
  27.     e_msg = 'Reset {} - {}: {}\n'.format(e_time, type(error).__name__, error)
  28.     with open(log, 'a') as log_file:
  29.         log_file.write(e_msg)
  30.  
  31.  
  32. def evaluate(submission):
  33.     """Check for proper flair"""
  34.     flair = submission.link_flair_css_class
  35.     if flair and flair.lower() == 'techsupport':
  36.         return True
  37.     return False
  38.  
  39.  
  40. def cross_post(submission):
  41.     """Create a new thread with the OP's selftext"""
  42.     title = submission.title
  43.     selftext = submission.selftext  # Modify to include OP's username and link to original thread
  44.     destination = reddit.subreddit(dest)
  45.     if selftext:
  46.         x_post = destination.submit(title=title, selftext=selftext, send_replies=False)
  47.     else:
  48.         url = submission.url
  49.         x_post = destination.submit(title=title, url=url, send_replies=False)
  50.     return 'http://www.reddit.com{}'.format(x_post.permalink)
  51.  
  52.  
  53. def notify_op(submission, cross_post_link):
  54.     """Send a PM to the OP about the crosspost"""
  55.     author = submission.author.name
  56.     title = 'Your techsupport submission has been cross-posted'
  57.     msg = cross_post_link  # body of email
  58.     reddit.redditor(author).message(title, msg)
  59.  
  60.  
  61. def sticky_comment(submission, cross_post_link):
  62.     """Create a comment on the original thread linking to the new thread. Try to sticky"""
  63.     msg = 'This post has been automatically cross-posted.\n\n' \
  64.           'Please go [here]({}) for all discussion related to ' \
  65.           'this thread.'.format(cross_post_link)
  66.     comment = submission.reply(msg)
  67.  
  68.     # Attempt to sticky will 403 fail if not moderator
  69.     try:
  70.         comment.mod.distinguish(sticky=True)
  71.     except Exception as e:
  72.         log_error(e)
  73.  
  74.  
  75. def main():
  76.     while True:
  77.         start = time()
  78.         try:
  79.             subreddit = reddit.subreddit(source)
  80.             submission_stream = subreddit.stream.submissions()
  81.             for submission in submission_stream:
  82.                 if submission.created_utc < start:
  83.                     continue
  84.                 if evaluate(submission):
  85.                     cross_post_link = cross_post(submission)
  86.                     notify_op(submission, cross_post_link)  # Will be notified via comment
  87.                     sticky_comment(submission, cross_post_link)
  88.         except Exception as e:
  89.             log_error(e)
  90.             sleep(60)
  91.             continue
  92.  
  93.  
  94. if __name__ == '__main__':
  95.     log_start()
  96.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement