Skip to content

Instantly share code, notes, and snippets.

@thundergolfer
Created August 25, 2017 08:27
Show Gist options
  • Save thundergolfer/6a3b7175223b1bb2f344d56e4a7c06ca to your computer and use it in GitHub Desktop.
Save thundergolfer/6a3b7175223b1bb2f344d56e4a7c06ca to your computer and use it in GitHub Desktop.
import praw
import arrow
import heapq
reddit = praw.Reddit(client_id='51SqtrEzmCTnuA',
client_secret='xehsxI2Sm81wHZpSa_PcmKcv5_k',
user_agent='me-on-reddit')
# for submission in reddit.subreddit('samharris').hot(limit=10):
# if(len(submission.comments) < 10):
# for comm in submission.comments:
# print(comm.body)
subreddit = reddit.subreddit('samharris')
def tally_user_details( sub, weeks_in_past=25 ):
subreddit = reddit.subreddit(sub)
end = arrow.utcnow()
user_scores = {}
for week in range(weeks_in_past):
start = end.replace(weeks=-1)
submissions = subreddit.submissions(start.timestamp, end.timestamp)
for sub in submissions:
comments = sub.comments
for c in comments:
if c.author: # Not interested in authorless comments for THIS bit.
if c.author not in user_scores:
user_scores[c.author.name] = c.score
else:
user_scores[c.author.name] += c.score
end = start
print("Week ", week + 1, " completed. ", start.humanize(), " to ", end.humanize())
return user_scores
def top_X_of_dict( dictionary, x=5):
top_X = heapq.nlargest(x, dictionary, key=dictionary.get)
return {k: dictionary[k] for k in top_X}
def display_users_dict( user_dict ):
for key in user_dict:
print(key, ": ", user_dict[key])
if __name__ == '__main__':
user_scores = tally_user_details( 'samharris', weeks_in_past=2 )
best_user = max(user_scores, key=user_scores.get)
top = top_X_of_dict( user_scores )
display_users_dict( top )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment