import praw import random import time import datetime # this is the login information for our bot username='' password='' client_id='' client_secret='' # connect to reddit reddit = praw.Reddit( client_id=client_id, client_secret=client_secret, user_agent=username, username=username, password=password, ) def refresh_comments(): print('refresh_comments... ') global current_comment global long_comments all_comments = [] for subreddit in [reddit.subreddit('politics'), reddit.subreddit('conservative')]: for submission in subreddit.top(time_filter='day', limit=10): submission_comments = submission.comments.list() real_comments = [comment for comment in submission_comments if isinstance(comment, praw.models.Comment)] all_comments += real_comments all_comments.sort(key=lambda comment: comment.score, reverse=True) long_comments=[] for comment in all_comments: if len(str(comment.body))>500 and '>' not in comment.body: long_comments.append(comment) current_comment=0 print(" len(long_comments)=",len(long_comments)) print(" current_comment=",current_comment) def generate_comment(comment): global current_comment global long_comments # generate a simple reply that supports a political candidate try: plagiarized_comment=long_comments[current_comment] except (NameError,KeyError): refresh_comments() return text_politics=plagiarized_comment.body current_comment+=1 # add a message explaining that this is a bot # this text uses a special language called reddit-flavored markdown to stylize # a message declaring that it was sent by a bot. All messages you post should # include this text at the bottom. # For more details about reddit-flavored markdown, see # https://www.reddit.com/r/reddit.com/comments/6ewgt/reddit_markdown_primer_or_how_do_you_do_all_that/ text_disclaimer=''' ***** BEEP. BOOP. I plagiarized this comment from '''+plagiarized_comment.permalink+''' ''' # send the comment to reddit try: text=text_politics +text_disclaimer print('text=',text) comment.reply(text) print('reply sent') # sleep for 5-10 minutes so that we don't overload reddit time.sleep(10*60+random.randrange(5*60)) except Exception as e: print('ERROR:',e) time.sleep(10*60) # in an infinite loop, we will look for comment in the post that we can reply to while True: print() print('new iteration at:',datetime.datetime.now()) # get list of the newest submissions submissions=list(reddit.subreddit('csci040').new(limit=10)) print('len(submissions)=',len(submissions)) ''' # check if there's a submission without a comment from us for submission in submissions: has_comment=False for comment in submission.comments: if comment.author==username: has_comment=True if not has_comment: generate_comment(submission) # FIXME: delete this line #submissions = [reddit.submission(url='https://www.reddit.com/r/csci040/comments/dw53wt/political_discussion_thread/')] time.sleep(3) continue ''' # get a list of all of the comments in the newest submissions # we also include the submissions in this list so that we can make # top-level comments all_comments=[] submission = random.choice(submissions) #for submission in submissions: print(' submission=',submission) all_comments+=submission.comments.list() print('len(all_comments)=',len(all_comments)) # next filter that list to remove comments that were generated by this bot not_my_comments=[] for comment in all_comments: try: if comment.author!=username: not_my_comments.append(comment) except AttributeError: pass print('len(not_my_comments)=',len(not_my_comments)) # next filter the list to also remove comments that we've already replied to comments_without_replies=[] for comment in not_my_comments: replies=list(comment.replies) have_replied=False for reply in replies: try: if reply.author==username: have_replied=True except AttributeError: pass if not have_replied: comments_without_replies.append(comment) print('len(comments_without_replies)=',len(comments_without_replies)) # randomly select one of the comments that we haven't replied to yet # and reply try: comment=random.choice(comments_without_replies) generate_comment(comment) except IndexError: if len(all_comments)==len(not_my_comments): generate_comment(submission) else: print('no comment generated')