Created
April 12, 2012 00:50
-
-
Save aperson/2363852 to your computer and use it in GitHub Desktop.
Does simple comparison of who moderates what other gaming subreddits.
This file contains 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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import json | |
import urllib.request | |
import time | |
import re | |
import sys | |
class Grabber: | |
def __init__(self): | |
self.opener = urllib.request.build_opener() | |
self.opener.addheaders = [('User-agent', 'gaming_stats.py')] | |
def get(self, url): | |
try: | |
with self.opener.open(url) as o: | |
out = o.read().decode('utf-8') | |
except urllib.error.HTTPError: | |
out = None | |
time.sleep(2) | |
return out | |
def subscribers(self, subreddit): | |
print('Getting subscriber count for: {}'.format(subreddit)) | |
http = self.get('http://reddit.com/r/{}/about.json'.format(subreddit)) | |
if http: | |
return json.loads(http)['data']['subscribers'] | |
else: | |
return None | |
def moderators(self, subreddit): | |
print('Getting moderator list for: {}'.format(subreddit)) | |
http = self.get('http://reddit.com/r/{}/about/moderators/.json'.format(subreddit)) | |
if http: | |
return [i['name'] for i in json.loads(http)['data']['children']] | |
else: | |
return None | |
def get_stats(): | |
g = Grabber() | |
subreddits = set(re.findall(r'reddit\.com\/r\/([a-zA-Z0-9_]*)', g.get( | |
'http://code.reddit.com/wiki/help/faqs/gaming'))) | |
stats = {i : {'subscribers' : g.subscribers(i), 'moderators' : g.moderators(i)} | |
for i in subreddits} | |
for i in dict(stats): | |
if not stats[i]['subscribers']: | |
del(stats[i]) | |
return stats | |
def subreddit_stats(stats, subreddit): | |
print('{} has {} subcribers and {} moderators'.format(subreddit, | |
stats[subreddit]['subscribers'], len(stats[subreddit]['moderators']))) | |
mods = dict() | |
for x in stats[subreddit]['moderators']: | |
mods[x] = list() | |
for y in stats: | |
if x in stats[y]['moderators'] and y != subreddit: | |
mods[x].append(y) | |
for i in mods: | |
print('{} is a moderator in {} other subreddits:'.format(i, len(mods[i]))) | |
print(', '.join(mods[i])) | |
if __name__ == '__main__': | |
args = sys.argv[1:] | |
if args[0] == '--dump': | |
with open('gaming_stats.json', 'w') as f: | |
f.write(json.dumps(get_stats())) | |
elif args[0] == '--parse' and len(args) == 2: | |
with open('gaming_stats.json', 'r') as f: | |
subreddit_stats(json.loads(f.read()), args[1]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment