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 generate_comment(comment): # generate a simple reply that supports a political candidate options=['CANDIDATE is ADVERB the ADJECTIVE NOUN TASK.','ADVERB, CANDIDATE is the ADJECTIVE NOUN TASK.','Do you think CANDIDATE is the ADJECTIVE NOUN?','We ADVERB need CANDIDATE TASK.'] candidate=['Izbicki','Mike Izbicki','Prof Izbicki','Mike'] adverb=['clearly','obviously','in my opinion','IMHO'] adjective=['best','most qualified','least bad'] noun=['president','candidate','presidential candidate','man','person'] task=['if you want someone to help the American poor','to lead the free world','to be president of the United States','to run our economy','for supporting US interests abroad'] text_politics=random.choice(options) text_politics=text_politics.replace('CANDIDATE',random.choice(candidate)) text_politics=text_politics.replace('ADVERB',random.choice(adverb)) text_politics=text_politics.replace('ADJECTIVE',random.choice(adjective)) text_politics=text_politics.replace('NOUN',random.choice(noun)) text_politics=text_politics.replace('TASK',random.choice(task)) text_politics=text_politics[0].upper()+text_politics[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'm a bot created for [CMC's csci040 course](http://www.github.com/mikeizbicki/cmc-csci040/).*) ''' # send the comment to reddit try: text=text_politics+text_disclaimer #comment.lower() comment.reply(text) print('reply sent') # sleep for 20-25 minutes so that we don't overload reddit time.sleep(8*60+random.randrange(3*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=5)) 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(6) 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=[] print('len(submissions)=',len(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: print('no comment generated')