Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import time, strftime, sleep
- import praw
- source = 'oneplusone+oneplus2+oneplus3+oneplus3t+oneplusx'
- dest = 'oneplussupport'
- log = 'crosspost_log.txt'
- reddit = praw.Reddit(
- client_id='client_id',
- client_secret='client_secret',
- password='password',
- user_agent='user_agent',
- username='username'
- )
- def log_start():
- """Log when the bot starts"""
- s_time = strftime("%Y-%m-%d %H:%M:%S")
- with open(log, 'a') as log_file:
- log_file.write('Started {}\n'.format(s_time))
- def log_error(error):
- """Log an error"""
- e_time = strftime("%Y-%m-%d %H:%M:%S")
- e_msg = 'Reset {} - {}: {}\n'.format(e_time, type(error).__name__, error)
- with open(log, 'a') as log_file:
- log_file.write(e_msg)
- def evaluate(submission):
- """Check for proper flair"""
- flair = submission.link_flair_css_class
- if flair and flair.lower() == 'techsupport':
- return True
- return False
- def cross_post(submission):
- """Create a new thread with the OP's selftext"""
- title = submission.title
- selftext = submission.selftext # Modify to include OP's username and link to original thread
- destination = reddit.subreddit(dest)
- if selftext:
- x_post = destination.submit(title=title, selftext=selftext, send_replies=False)
- else:
- url = submission.url
- x_post = destination.submit(title=title, url=url, send_replies=False)
- return 'http://www.reddit.com{}'.format(x_post.permalink)
- def notify_op(submission, cross_post_link):
- """Send a PM to the OP about the crosspost"""
- author = submission.author.name
- title = 'Your techsupport submission has been cross-posted'
- msg = cross_post_link # body of email
- reddit.redditor(author).message(title, msg)
- def sticky_comment(submission, cross_post_link):
- """Create a comment on the original thread linking to the new thread. Try to sticky"""
- msg = 'This post has been automatically cross-posted.\n\n' \
- 'Please go [here]({}) for all discussion related to ' \
- 'this thread.'.format(cross_post_link)
- comment = submission.reply(msg)
- # Attempt to sticky will 403 fail if not moderator
- try:
- comment.mod.distinguish(sticky=True)
- except Exception as e:
- log_error(e)
- def main():
- while True:
- start = time()
- try:
- subreddit = reddit.subreddit(source)
- submission_stream = subreddit.stream.submissions()
- for submission in submission_stream:
- if submission.created_utc < start:
- continue
- if evaluate(submission):
- cross_post_link = cross_post(submission)
- notify_op(submission, cross_post_link) # Will be notified via comment
- sticky_comment(submission, cross_post_link)
- except Exception as e:
- log_error(e)
- sleep(60)
- continue
- if __name__ == '__main__':
- log_start()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement