Skip to content

Instantly share code, notes, and snippets.

@yatt
Created May 9, 2011 03:31
Show Gist options
  • Save yatt/962011 to your computer and use it in GitHub Desktop.
Save yatt/962011 to your computer and use it in GitHub Desktop.
get google code jam 2011 qualification round sources
# coding: utf-8
import os
import urllib
import time
import json
import StringIO
import zipfile
# google code jam 2011 qualification round
contest_id = 975485
problem_ids = [1080487, 1026487, 1059486, 992486]
savedir = './data'
cmdurl = 'https://code.google.com/codejam/contest/scoreboard/do'
class Fetcher(object):
def __init__(self):
self.last = None
self.interval = 2
def urlfetch(self, url, interval=None):
# wait interval
if interval is None:
interval = self.interval
if self.last:
wait = interval - (time.time() - self.last)
if wait > 0:
time.sleep(wait)
#self.last = time.time()
# fetch
filename = None
try:
conn = urllib.urlopen(url)
if 'Content-Disposition' in conn.info():
filename = conn.info()['Content-Disposition']
filename = filename[filename.index('=') + 1:]
except Exception, e:
self.last = time.time()
raise e
cont = conn.read()
self.last = time.time()
return filename, cont
fetcher = Fetcher()
class UserIterator(object):
def __init__(self, show_type):
types = {'all': 'all', 'friend': 'fri', 'closest': 'nbr'}
assert show_type in types
self.show_type = types[show_type]
self.param = {
'contest_id': contest_id,
'cmd': 'GetScoreboard',
'show_type': show_type,
'start_pos': 1,
}
self.buf = []
self.contestant = 10e9
def fill(self):
if self.contestant < self.param['start_pos']:
return
p = '&'.join('%s=%s' % (k, str(n)) for k,n in self.param.items())
url = cmdurl + '?' + p
doc = json.loads(fetcher.urlfetch(url)[1])
self.buf.extend(doc['rows'])
self.param['start_pos'] += 20
self.contestant = doc['stat']['nrp']
def __iter__(self):
return self
def next(self):
if self.buf == []:
self.fill()
if self.buf == []:
raise StopIteration()
ret = User(self.buf.pop())
return ret
class SolutionIterator(object):
def __init__(self, u):
self.u = u
self.index = -1
def __iter__(self):
return self
def next(self):
lst = self.u.secsSolved
self.index += 1
while self.index < len(lst) and lst[self.index] < 0:
self.index += 1
if self.index == len(lst):
raise StopIteration()
problem_index, problem_type = self.index // 2, self.index % 2
problem = self.u.problem(problem_index)
ret = getattr(problem, ['small', 'large'][problem_type])()
return ret
class User(object):
def __init__(self, info, problem_index=-1):
self.info = info
self.flag_url = info['fu']
self.secsSolved = info['ss']
self.isFriend = info['fr']
self.open_attempt = info['oa']
self.name = info['n']
self.penalty = info['pen']
self.rank = info['r']
self.attempts = info['att']
self.inContest = info['ic']
self.points = info['pts']
self.problem_index = problem_index
self.problem_id = problem_ids[problem_index] if problem_index != -1 else None
def problem(self, index):
return User(self.info, index)
def fetch(self, problem_type):
user = self.name
problem_id = self.problem_id
ptype = ['small', 'large'].index(problem_type)
if self.secsSolved[self.problem_index * 2 + ptype] < 0:
return None
param = {
'cmd': 'GetSourceCode',
'contest': contest_id,
'problem': problem_id,
'io_set_id': ptype,
'username': user,
}
url = cmdurl + '?' + '&'.join('%s=%s' % (k, str(n)) for k,n in param.items())
filename, cont = fetcher.urlfetch(url)
arv = StringIO.StringIO(cont)
arv = zipfile.ZipFile(arv)
# file emulation
arv.fp.name = filename
arv.fp.read = lambda: arv.fp.getvalue()
arv.ptype = ptype
return arv
def small(self):
return self.fetch('small')
def large(self):
return self.fetch('large')
def allsources(self):
return SolutionIterator(self)
@classmethod
def userlist(cls, show_type='all'):
return UserIterator(show_type)
def main():
for user in User.userlist():
for archive in user.allsources():
d = os.path.join(savedir, user.name)
if not os.path.exists(d):
os.makedirs(d)
print user.name, archive.fp.name
saveto = os.path.join(d, archive.fp.name)
ost = open(saveto, 'wb')
ost.write(archive.fp.read())
ost.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment