Skip to content

Instantly share code, notes, and snippets.

@montycheese
Created October 21, 2014 02:53
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 montycheese/15e38c9fb10fd6656140 to your computer and use it in GitHub Desktop.
Save montycheese/15e38c9fb10fd6656140 to your computer and use it in GitHub Desktop.
Subreddit ranking tracker
import requests, webbrowser
def get_frontpage_info():
url = "http://www.reddit.com/.json"
r = requests.get(url)
json_data = r.json()
try:
if json_data['error'] == 404:
print "404 error, website is down"
exit()
except KeyError:
pass
try:
all_posts = json_data["data"]['children']
except KeyError:
all_posts = json_data['data']['children']
#for key in json_data['data']['children'][0]['data'].keys():
# print json_data['data']['children'][0]['data'][key]
# max post on front page = 25 as of 10/20/14
for post in all_posts:
try:
subreddit_rankings[post['data']['subreddit']] += 1
# if the subreddit has never been in before, set default count to 1
except KeyError:
subreddit_rankings[post['data']['subreddit']] = 1
return subreddit_rankings
def store_stats():
file = open("stats.txt", "wb")
file.write(str(subreddit_rankings))
file.close()
def retrieve_stats():
s = open('stats.txt', 'r').read()
updated_dict = eval(s)
return updated_dict
if __name__ == "__main__":
subreddit_rankings = retrieve_stats()
get_frontpage_info()
store_stats()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment