Skip to content

Instantly share code, notes, and snippets.

@KevOrr
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KevOrr/3315aedcfce7aaceb224 to your computer and use it in GitHub Desktop.
Save KevOrr/3315aedcfce7aaceb224 to your computer and use it in GitHub Desktop.
import sys
import praw
from requests.exceptions import HTTPError
r = praw.Reddit('elabot')
p = r.get_subreddit('periwinkle')
errors = []
seen = set()
players = {}
getters = (('get_new', p.get_new),
('get_hot', p.get_hot),
('get_top_from_all', p.get_top_from_all),
('get_top_from_year', p.get_top_from_year),
('get_controversial_from_all', p.get_controversial_from_all),
('get_controversial_from_year', p.get_controversial_from_year)
)
def handle_thing(thing):
global seen
if thing.fullname not in seen:
seen.add(thing.fullname)
try:
name = thing.author.name.lower()
except AttributeError:
return True
if name not in players:
players[name] = 1
else:
players[name] += 1
return True
else:
return False
# Get new, hot, top, and controversial
for name, getter in getters:
new = 0
skipped = 0
print('Fetching with ' + name)
for link in getter(limit=None):
if handle_thing(link):
new += 1
sys.stdout.write(' Handled {:>4} new submissions, skipped {:>4} \r'.format(new, skipped))
link.replace_more_comments()
for comment in link.comments:
handle_thing(comment)
else:
skipped += 1
print(' Handled {:>4} new submissions, skipped {:>4} '.format(new, skipped))
# Random search
print('\nStarting random search')
try:
for i in range(3000):
try:
link = p.get_random_submission()
if handle_thing(link):
print('Found random submission not already handled. {} left.'.format(2999 - i))
link.replace_more_comments()
for comment in link.comments:
handle_thing(comment)
sys.stdout.write(' Finshed {:>4} random searches \r'.format(i+1))
except KeyboardInterrupt:
print(' Finshed {:>4} random searches'.format(i))
skip = input('Are you sure you want to quit the random search? (y/n)').lower()
if skip == 'y':
break
except HTTPError as e:
print('Encountered HTTPError: ' + repr(str(e)) + '. Skipping random search.')
# sort players from most active to least
players = sorted(players.items(), key=lambda item: item[1], reverse=True)
print('Found ' + str(len(players)) + ' players ')
# Write to file
with open('peri_players.txt', 'w') as f:
for player in players:
f.write(player[0] + ' ' + player[1] + '\n')
# Print top 25
# Score = # of posts + # of comments for that user
print('\nMost Active Players:')
print('\nRank Score Name')
for i,player in enumerate(players[:25]):
print('{:<2} @ {:>4}: {}'.format(i + 1, player[1], player[0]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment