def main(): try: r = obot.login() subreddit = r.subreddit('test') #fetch top hot 15 submissions from each subreddit for comment in subreddit.stream.comments(): print("hi") process_comments(comment) except Exception as e: print("Going to sleep for 10 minutes") time.sleep(600) main()
cursor.execute('INSERT INTO Processed VALUES (?, ?, ?, ?, ?, ?)', (str(comment.id), int(comment.created_utc), str(comment.body), str(comment.author), str(replied), str(reply))) cursor.execute("DELETE FROM Processed WHERE time <= strftime('%s') - 86400 * 2;") db.commit() return comment.id while True: if shutdown: sys.exit() try: # Reddit login r = obot.login() subreddit = r.get_subreddit('test') # Retrive mods of the subreddit for use in shutdown mods = r.get_moderators(subreddit) subdone = load_previous_submissions() post_search(subreddit, subdone) subreddit_comments = subreddit.get_comments() flat_comments = praw.helpers.flatten_tree(subreddit_comments) already_done = already_processed_comments()
@author: Ryan ''' import praw import re import os import obot def countComments(flat_comments): #function to count number of comments on a post count = 0 for comment in flat_comments: count += 1 return count r = obot.login() #logins to /u/wacomBot using OAuth2 safe_word = "do not delete" #put the phrase OP can comment to have the bot not delete the post here if not os.path.isfile("posts_replied_to.txt"): #creates or opens the file used to store posts replied to posts_replied_to = [] else: with open("posts_replied_to.txt", "r") as f: posts_replied_to = f.read() posts_replied_to = posts_replied_to.split("\n") posts_replied_to = filter(None, posts_replied_to) subreddit = r.get_subreddit('wacomTest') #put the subreddit name here for submission in subreddit.get_new(limit=10): #gets the 10 newest submissions in the subreddit if submission.id not in posts_replied_to: #if the current post is not one we checked already
(str(comment.id), int(comment.created_utc), str( comment.body), str(comment.author), str(replied), str(reply))) cursor.execute( "DELETE FROM Processed WHERE time <= strftime('%s') - 86400 * 2;") db.commit() return comment.id while True: if shutdown == True: sys.exit() try: # Reddit login r = obot.login() subreddit = r.get_subreddit('rwby') # Retrive mods of the subreddit for use in shutdown mods = r.get_moderators(subreddit) subdone = load_previous_submissions() post_search(subreddit, subdone) subreddit_comments = subreddit.get_comments() flat_comments = praw.helpers.flatten_tree(subreddit_comments) already_done = already_processed_comments()
# script to get top 4 comments from 100 top posts of the year import praw, csv, sys, obot reload(sys) sys.setdefaultencoding('utf-8') USERAGENT = "u/odumann test" SUBREDDIT = "technology" MAXPOSTS = None obot.login() r = praw.Reddit(USERAGENT) sub = r.get_subreddit(SUBREDDIT) post_generator = sub.get_top_from_year(limit = MAXPOSTS) def get_comments(): fname = 'comments_%s_none.txt' %SUBREDDIT f = open(fname, "w") print('file created and ready to be written') print('parsing comments') for submission in post_generator: comments = submission.comments count = 0 for comment in comments: if (count < 10): if not isinstance(comment, praw.objects.MoreComments): f.write('{0}\n{1}\n{2}\n'.format(comment.submission, comment.body, comment.score)) count +=1 f.close() print('Done')