Skip to content

Instantly share code, notes, and snippets.

@joeycastillo
Forked from GigaPatches/flair-stats.py
Last active December 15, 2015 05:39
Show Gist options
  • Save joeycastillo/5210160 to your computer and use it in GitHub Desktop.
Save joeycastillo/5210160 to your computer and use it in GitHub Desktop.
"""
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.
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)
Forked by joeycastillo to add custom user agent and rate limiting.
Tested successfully on a subreddit with ~11,000 flair users.
"""
from __future__ import division
import cookielib
import urllib2
import json
from urllib import urlencode
from getpass import getpass
from time import sleep
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent', 'very_quick_flair_stats_generator.py 0.1a')]
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
sleep(2)
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
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