From 85283933f1d93bb10d20ff3a0c83918792a4814f Mon Sep 17 00:00:00 2001 From: Rishi Date: Tue, 29 Jan 2013 01:57:22 +0530 Subject: [PATCH] Fixed the bug. --- spojbackup.py | 73 +++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/spojbackup.py b/spojbackup.py index f8e9ae4..18c4f06 100644 --- a/spojbackup.py +++ b/spojbackup.py @@ -1,6 +1,5 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - ''' _____ _____ _____ __ _ _ @@ -8,9 +7,14 @@ |__ | __| | | | | | . | .'| _| '_| | | . | |_____|__| |_____|_____| |___|__,|___|_,_|___| _| |_| + by Abhishek Mishra + Shashwat Anand Keywords: python, tools, algorithms, spoj +Copyright (C) 2003-2004 Free Software Foundation, Inc. + + Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -51,7 +55,6 @@ print "mechanize required but missing" sys.exit(1) - def getSolutions (path_prefix, path_proxy): global br, username, password @@ -74,14 +77,14 @@ def getSolutions (path_prefix, path_proxy): # authenticate the user print "Authenticating " + username - br.open ("http://spoj.pl") + br.open ("http://www.spoj.com") br.select_form (name="login") br["login_user"] = username br["password"] = password # sign in for a day to avoid timeouts br.find_control(name="autologin").items[0].selected = True - br.form.action = "http://www.spoj.pl" + br.form.action = "http://www.spoj.com" response = br.submit() verify = response.read() @@ -91,7 +94,7 @@ def getSolutions (path_prefix, path_proxy): # grab the signed submissions list print "Grabbing siglist for " + username - siglist = br.open("http://www.spoj.pl/status/" + username + "/signedlist") + siglist = br.open("http://www.spoj.com/status/" + username + "/signedlist") # dump first nine useless lines in signed list for formatting for i in xrange(9): @@ -101,9 +104,9 @@ def getSolutions (path_prefix, path_proxy): print "Filtering siglist for AC/Challenge solutions..." mysublist = list() - while True: + while True: temp = siglist.readline() - + if temp=='\------------------------------------------------------------------------------/\n': # reached end of siglist break @@ -112,9 +115,9 @@ def getSolutions (path_prefix, path_proxy): 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) @@ -123,37 +126,33 @@ def getSolutions (path_prefix, path_proxy): 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) - - + source_code = br.open("http://www.spoj.com/submit/id="+entry[1]) + entire_page = source_code.read() + box_start = entire_page.find(">", entire_page.find("", box_start) + selected_lang = entire_page[entire_page.find("selected>", box_end)+9:entire_page.find("", entire_page.find("selected>", box_end))] + extension = " " + if 'C++' in selected_lang: + extension = '.cpp' + elif 'C (gcc 4.3.2)' in selected_lang: + extension = '.c' + elif 'Python' in selected_lang: + extension = '.py' + 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: + filename = entry[3]+"-"+entry[1]+extension + fp = open(os.path.join(path_prefix, filename), "w") + fp.write (entire_page[box_start+1:box_end]) + 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 = optparse.OptionParser()