Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Created October 23, 2016 15:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanplopes/350e6e537601347e44828edca3f08565 to your computer and use it in GitHub Desktop.
Save juanplopes/350e6e537601347e44828edca3f08565 to your computer and use it in GitHub Desktop.
Downloads problems, best submissions and leaderboard from IEEExtreme 10 competition
#!/usr/bin/env python2
from collections import defaultdict
import requests, json, zipfile
PREFIX = 'https://ieee.hackerrank.com/rest/contests/ieeextreme10'
USER='PUT USERNAME HERE'
PASS='PUT PASSWORD HERE'
EXT = {'python': 'py'}
def get(url):
r = requests.get(PREFIX+url, auth=(USER, PASS))
return r.json()
def get_leaderboard_pages():
lastidx = 0
total = None
pages = []
while True:
print 'getting page starting at index {} of {}'.format(lastidx, total or 'UNKNOWN')
page = get('/leaderboard?limit=100&offset={}'.format(lastidx))
if len(page['models']) == 0: break
pages.append(page)
lastidx = page['models'][-1]['index']+1
total = page['total']
return pages
def download_submissions():
print 'caching challenges'
challenges = get('/challenges?limit=1000')['models']
ids = {c['slug']: i+1 for i, c in enumerate(challenges)}
print 'downloading submission index'
submissions = get('/submissions?limit=1000')['models']
print 'downloading leaderboard'
leaderboard = get_leaderboard_pages()
best = defaultdict(lambda: (0, None))
for submission in submissions:
slug = submission['challenge']['slug']
sid = submission['id']
best[slug] = max(best[slug], (submission['score'], sid))
indexstr = ''
with zipfile.ZipFile(USER + '-solutions.zip', 'w') as zipf:
indexstr += '{:4s} {:6s} {:6s} {:7s} {:7s} {}\n'.format('#', 'score', 'real', 'cases', 'lang', 'name')
zipf.writestr('raw/challenges.json', json.dumps(challenges))
zipf.writestr('raw/submissions.json', json.dumps(submissions))
zipf.writestr('raw/leaderboard.json', json.dumps(leaderboard))
for challenge in challenges:
slug = challenge['slug']
cid = ids[slug]
print 'downloading challenge', cid, slug
challenge = get('/challenges/{}'.format(slug))['model']
filename = 'challenges/{:02d}-{}.html'.format(cid, slug)
zipf.writestr(filename, '<!DOCTYPE html><meta charset="UTF-8">' + challenge['body_html'].encode('utf-8'))
zipf.writestr('raw/{:02d}-{}-challenge.json'.format(cid, slug), json.dumps(challenge))
print 'downloaded challenge', slug, 'as', filename
for slug, (score, sid) in sorted(best.items(), key=lambda x:ids.get(x[0])):
if sid is None: continue
cid = ids[slug]
print 'downloading solution for', slug, 'id', sid, 'score=', score
submission = get('/submissions/{}'.format(sid))['model']
lang = submission['language']
score = submission['score']
display = submission['display_score']
cases = submission['testcase_status']
filename = '{:02d}-{}.{}'.format(cid, slug, EXT.get(lang, lang))
print 'downloaded', sid, 'as', filename
indexstr += '{:4d} {:6.2f} {:6.2f} {:3d}/{:3d} {:7s} {}\n'.format(cid, score*100, display, sum(cases), len(cases), lang, slug)
zipf.writestr('raw/{:02d}-{}-submission.json'.format(cid, slug), json.dumps(submission))
zipf.writestr(filename, submission['code'].encode('utf-8'))
zipf.writestr('index.txt', indexstr)
download_submissions()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment