import praw | |
import sys | |
def get_subreddit_comments(reddit_agent, subreddit, comments_out = [], count = 100): | |
try: | |
sub = reddit_agent.get_subreddit(subreddit) | |
comments_raw = sub.get_comments(sub, limit=count) | |
comments_flat = praw.helpers.flatten_tree(comments_raw) | |
for comment in comments_flat: | |
try: | |
if hasattr(comment, 'comments'): | |
for reply in comment.comments: | |
comments_out.append(reply.body) | |
else: | |
comments_out.append(comment.body) | |
except: | |
pass | |
except: | |
pass | |
def get_reddit_agent(user_agent, client_id, client_secret, redirect='http://127.0.0.1'): | |
reddit_agent = praw.Reddit(user_agent = 'sorry counter') | |
reddit_agent.set_oauth_app_info(client_id = client_id, | |
client_secret = client_secret, | |
redirect_uri = redirect) | |
return reddit_agent | |
def get_rate_and_word_count(comment_list, conditions = ['sorry', 'apologies']): | |
count = 0 | |
word_count = 0 | |
for comment in comment_list: | |
words = comment.split(' ') | |
word_count += int(len(words)) | |
for word in words: | |
for condition in conditions: | |
count += word.lower().count(condition) | |
return count, word_count | |
if __name__ == "__main__": | |
client_id = 'client_id' | |
client_secret = 'client_secret' | |
reddit_agent = get_reddit_agent('custom app name', client_id, client_secret) | |
if len(sys.argv) < 3: | |
print("Please provide reddit and search term arguments") | |
exit() | |
reddits = sys.argv[1].split(',') | |
search_terms = sys.argv[2].split(',') | |
comments = [] | |
for subreddit in reddits: | |
get_subreddit_comments(reddit_agent, subreddit, comments, count = (1000 / len(reddits))) | |
count, total_words = get_rate_and_word_count(comments, search_terms) | |
print("Count {} / {}".format(count, total_words)) | |
print("{}%".format(float(count)/total_words * 100)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment