Created
August 25, 2017 08:29
-
-
Save thundergolfer/612a97b6f51fdf36c4a0b497540f0f18 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arrow | |
import praw | |
import collections | |
import argparse | |
import logging | |
parser = argparse.ArgumentParser(description='Manage relative comment ranking.') | |
parser.add_argument("-v", "--verbose", help="enable logging", action="store_true") | |
args = parser.parse_args() | |
if args.verbose: | |
logging.basicConfig(level=logging.INFO) | |
reddit = praw.Reddit(client_id='51SqtrEzmCTnuA', | |
client_secret='xehsxI2Sm81wHZpSa_PcmKcv5_k', | |
user_agent='me-on-reddit') | |
def get_comment_score_distibution( subreddit, weeks_in_past=25 ): | |
scores = [] | |
end = arrow.utcnow() | |
start = end.replace(weeks=-weeks_in_past) | |
submissions = reddit.subreddit(subreddit).submissions(start.timestamp, end.timestamp) | |
for s in submissions: | |
submissions.replace_more() # expand comment sub-trees | |
for c in s.comments: | |
scores.append(c.score) | |
logging.info('Submission completed.') | |
return collections.Counter(scores) | |
def percentile_rank( score, scores_and_their_frequencies ): | |
less_than = 0 | |
N = len(scores_and_their_frequencies) | |
for s in scores_and_their_frequencies: | |
if s < score: | |
less_than_count += scores_and_their_frequencies[s] | |
elif s == score: | |
equal_count = scores_and_their_frequencies[score] | |
return ((less_than_count + (0.5 * equal_count)) / N ) * 100 | |
if __name__ == '__main__': | |
d = get_comment_score_distibution('samharris', weeks_in_past=2) | |
print(d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment