Created
November 26, 2009 20:18
-
-
Save ashashwat/243641 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 sys, getpass, os, glob | |
try: | |
import argparse | |
except ImportError: | |
print "argparse required but missing" | |
sys.exit(1) | |
try: | |
from mechanize import Browser | |
except ImportError: | |
print "mechanize is required but missing" | |
sys.exit(1) | |
def getSolutions (path_prefix): | |
global br | |
br = Browser() | |
br.set_proxies({"http":"iit2006069:hukunQk007@172.31.1.4:8080"}) | |
# make me a browser that makes ass of robots.txt | |
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; \ | |
rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')] | |
br.set_handle_robots(False) | |
print "Enter yout SPOJ username :", | |
username = raw_input() | |
password = getpass.getpass() | |
# Authenticate the user | |
print "Authenticating " + username | |
br.open ("http://spoj.pl") | |
br.select_form (name="login") | |
br["login_user"] = username | |
br["password"] = password | |
# sign in for a day for avoiding timeouts | |
br.find_control(name="autologin").items[0].selected = True | |
br.form.action = "http://www.spoj.pl" | |
response = br.submit() | |
verify = response.read() | |
if (verify.find("Authentication failed!") != -1): | |
print "Error authenticating - " + username | |
exit(0) | |
# grab the signed list of submissions | |
print "Grabbing siglist for " + username | |
siglist = br.open("http://www.spoj.pl/status/" + username + "/signedlist") | |
# dump first 9 lines | |
for i in xrange(9): | |
siglist.readline() | |
# make a list of all AC's and challenges | |
print "Filtering siglist for AC/Challenge solutions..." | |
mysublist = list() | |
while True: | |
temp = siglist.readline() | |
if temp=='\------------------------------------------------------------------------------/\n': | |
# reached end of siglist | |
break | |
if not len(temp) : | |
print "Reached EOF, siglist format has probably changed," + \ | |
" contact author." | |
exit(1) | |
entry = [x.strip() for x in temp.split('|')] | |
if entry[4] == 'AC' or entry[4].isdigit(): | |
mysublist.append (entry) | |
#totalsubmissions = len(mysublist) | |
print "Done !!!" | |
return mysublist | |
def downloadSolutions(mysublist): | |
totalsubmissions = len(mysublist) | |
print "Fetching sources into " + path_prefix | |
progress = 0 | |
for entry in mysublist: | |
existing_files = glob.glob(os.path.join(path_prefix, "%s-%s*" % \ | |
(entry[3],entry[1]))) | |
progress += 1 | |
if len(existing_files) == 1: | |
print "%d/%d - %s skipped." % (progress, totalsubmissions, entry[3]) | |
else: | |
source_code = br.open("http://www.spoj.pl/files/src/save/" + \ | |
entry[1]) | |
header = dict(source_code.info()) | |
filename = "" | |
try: | |
filename = header['content-disposition'].split('=')[1] | |
filename = entry[3] + "-" + filename | |
except: | |
filename = entry[3] + "-" + entry[1] | |
fp = open( os.path.join(path_prefix, filename), "w") | |
fp.write (source_code.read()) | |
fp.close | |
print "%d/%d - %s done." % (progress, totalsubmissions, filename) | |
print "Created a backup of %d submission for %s" % \ | |
(totalsubmissions, username) | |
if __name__=="__main__": | |
parser = argparse.ArgumentParser(prog="spojbackup", | |
description = "Creates a backup of all your Submissions on SPOJ " +\ | |
"(Sphere Online Judge) http://spoj.pl in a desired place") | |
parser.add_argument("-o", "--outputpath",default="./", type=str, | |
help="Directory to store all fetched solutions") | |
args = parser.parse_args() | |
path_prefix = args.outputpath | |
print downloadSolutions (getSolutions (path_prefix) ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment