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
  • Select an option

  • Save joeycastillo/5210160 to your computer and use it in GitHub Desktop.

Select an option

Save joeycastillo/5210160 to your computer and use it in GitHub Desktop.

Revisions

  1. joeycastillo revised this gist Mar 21, 2013. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions flair-stats.py
    Original file line number Diff line number Diff line change
    @@ -4,14 +4,14 @@
    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)
    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
    @@ -21,9 +21,11 @@
    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'
    @@ -43,6 +45,7 @@ def flair_stats(subreddit, modhash):
    while True:
    if _next:
    q['after'] = _next
    sleep(2)
    data = json.load(opener.open(url + urlencode(q)))
    if not data:
    break
  2. Justin Harper created this gist Aug 3, 2011.
    77 changes: 77 additions & 0 deletions flair-stats.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    """
    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
    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)