Created
August 3, 2011 02:38
-
-
Save GigaPatches/1121788 to your computer and use it in GitHub Desktop.
Quick and dirty script to get flair css class usage statistics on reddit
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
""" | |
A quick and dirty script to show some flair statistics. | |
Currently it only shows how many users have flair, and how many use | |
each css class. | |
(success not guaranteed, tested on Python 2.6.6 under Debian 6.0 on a | |
subreddit with 2 flair users) | |
Usage: | |
1) run flair-stats.py | |
2) enter username and password when asked | |
3) enter subreddit name (eg: starcraft) | |
4) Wait for statistics to load (could take a while for big subreddits) | |
""" | |
from __future__ import division | |
import cookielib | |
import urllib2 | |
import json | |
from urllib import urlencode | |
from getpass import getpass | |
cj = cookielib.CookieJar() | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | |
def login(user, passwd): | |
url = 'http://www.reddit.com/api/login' | |
r = opener.open(url, urlencode({'user': user, 'passwd': passwd})) | |
def get_modhash(): | |
url = 'http://www.reddit.com/api/me.json' | |
data = json.load(opener.open(url)) | |
return data['data']['modhash'] if 'data' in data else False | |
def flair_stats(subreddit, modhash): | |
url = 'http://www.reddit.com/r/%s/api/flairlist.json?' % (subreddit,) | |
stats = {'total': 0, 'class': {} } | |
q = {'limit': 1000, 'uh': modhash} | |
_next = None | |
while True: | |
if _next: | |
q['after'] = _next | |
data = json.load(opener.open(url + urlencode(q))) | |
if not data: | |
break | |
for user in data['users']: | |
c = user['flair_css_class'] | |
if c not in stats['class']: | |
stats['class'][c] = 0 | |
stats['class'][c] += 1 | |
stats['total'] += 1 | |
if 'next' not in data: | |
break | |
_next = data['next'] | |
return stats | |
if __name__ == '__main__': | |
import sys | |
username = raw_input('username: ') | |
passwd = getpass('password: ') | |
login(username, passwd) | |
modhash = get_modhash() | |
if not modhash: | |
print 'Unable to get modhash (invalid login?)' | |
sys.exit(1) | |
subreddit = raw_input('subreddit: ') | |
print 'Loading stats...' | |
stats = flair_stats(subreddit, modhash) | |
print 'Total flair users: %d' % (stats['total'],) | |
for k, v in stats['class'].items(): | |
print '\t%s: %d (%.2f%%)' % (k, v, v / stats['total'] * 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment