Created
December 18, 2015 04:32
-
-
Save zsrinivas/4190400de3e7e2b274e6 to your computer and use it in GitHub Desktop.
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
import os | |
import re | |
from getpass import getpass | |
from requests import Session | |
from contextlib import contextmanager | |
STATUS_PAGE = "http://www.spoj.com/status/{problem},{username}/" | |
MYACCOUNT_PAGE = "http://www.spoj.com/myaccount/" | |
SOLUTION_PAGE = "http://www.spoj.com/files/src/save/{sol_id}" | |
HOME_PAGE = "https://www.spoj.com/" | |
@contextmanager | |
def login(username, password): | |
ses = Session() | |
ses.post(HOME_PAGE, data={ | |
'login_user': username, | |
'password': password | |
}) | |
yield ses | |
@contextmanager | |
def takedir(path): | |
oldloc = os.path.abspath(os.curdir) | |
newloc = path | |
try: | |
if not os.path.exists(newloc): | |
os.mkdir(newloc) | |
os.chdir(newloc) | |
yield newloc | |
finally: | |
os.chdir(oldloc) | |
def getSolvedProblems(doc, username): | |
pattern = '/status/(.{3,8}),' + username + '/' | |
return re.findall(pattern, doc) | |
def getSolutions(doc): | |
pattern = '/files/src/([0-9]*?)/' | |
return re.findall(pattern, doc) | |
def download(): | |
username = raw_input("username: ") | |
password = getpass("password: ") | |
with login(username, password) as session: | |
for prob_id in getSolvedProblems(session.get(MYACCOUNT_PAGE).content, username): | |
print 'downloading {} solutions'.format(prob_id) | |
with takedir(os.path.abspath(prob_id)): | |
for sol_id in getSolutions(session.get( | |
STATUS_PAGE.format(problem=prob_id, username=username)).content): | |
print ' downloading {}'.format(sol_id) | |
with open(sol_id, 'w') as solution: | |
solution.write(session.get(SOLUTION_PAGE.format(sol_id=sol_id)).content) | |
if __name__ == '__main__': | |
download() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment