Skip to content

Instantly share code, notes, and snippets.

@ianchesal
Created April 3, 2012 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ianchesal/2288184 to your computer and use it in GitHub Desktop.
Save ianchesal/2288184 to your computer and use it in GitHub Desktop.
Calculate Winners for the Apple.SE iPad Contest
#!/usr/local/bin/python
import json
import subprocess
from pprint import pprint
import time
import sys
import operator
# The Level 3 Crowd
users = {
292: 'Ian C.',
13414: 'gentmatt',
9388: 'daniel-l',
9495: 'stuffe',
8318: 'jtbandes',
4408: 'mathias-bynens',
3117: 'adam-eberbach',
219: 'adam-davis',
13: 'kyle-cronin',
5472: 'bmike',
218: 'senseful',
}
# Start date of the contest...
from_date = '1331856000'
# Answers: curl --compressed "http://api.apple.stackexchange.com/1.1/users/292/answers?sort=votes&body=false&comments=false&order=desc&fromdate=1331856000&pagesize=36&page=2"
# Questions: curl --compressed "http://api.apple.stackexchange.com/1.1/users/292/questions?sort=votes&body=false&answers=true&fromdate=1331856000&comments=false&order=desc&body=true&pagesize=36&page=2"
def fetch_questions(uid):
questions = list()
url = 'http://api.apple.stackexchange.com/1.1/users/%s/questions?sort=votes&body=false&answers=true&fromdate=1331856000&comments=false&order=desc&body=true&pagesize=36' % (uid)
print 'Calling: %s' % url
output = subprocess.check_output(['curl', '--compress', url])
time.sleep(1)
if output:
query = json.loads(output)
if len(query['questions']) > 0:
for q in query['questions']:
questions.append(q)
else:
print "Pulled %s questions. Done." % len(questions)
else:
print 'Error: No output from call...'
return questions
def fetch_answers(uid):
answers = list()
url = 'http://api.apple.stackexchange.com/1.1/users/%s/answers?sort=votes&body=false&comments=false&order=desc&fromdate=1331856000&pagesize=36' % (uid)
print 'Calling: %s' % url
output = subprocess.check_output(['curl', '--compress', url])
time.sleep(1)
if output:
query = json.loads(output)
if len(query['answers']) > 0:
for q in query['answers']:
answers.append(q)
else:
print "Pulled %s answers. Done." % len(answers)
else:
print 'Error: No output from call...'
return answers
print '----------------------------'
print 'Scores:'
scores = dict()
for uid in users.keys():
s = list()
print 'Gathering data for %s...' % users[uid]
for q in fetch_questions(uid):
s.append(int(q['score']))
for a in fetch_answers(uid):
s.append(int(a['score']))
s.sort(reverse=True)
scores[uid] = s
print '----------------------------'
print 'Top 35 for each user:'
# I should probably sort this in reverse order by score but meh...
sums = dict()
for uid in scores:
s = scores[uid]
sums[uid] = sum(s[0:35])
for (uid, score) in sorted(sums.iteritems(), key=operator.itemgetter(1), reverse=True):
print '%s = %s' % (users[uid], score)
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment