Skip to content

Bug fixed #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 36 additions & 37 deletions spojbackup.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''

_____ _____ _____ __ _ _
| __| _ | |__| | | |_ ___ ___| |_ _ _ ___
|__ | __| | | | | | . | .'| _| '_| | | . |
|_____|__| |_____|_____| |___|__,|___|_,_|___| _|
|_|
by Abhishek Mishra <[email protected] >
Shashwat Anand <[email protected]>

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:

Expand Down Expand Up @@ -51,7 +55,6 @@
print "mechanize required but missing"
sys.exit(1)


def getSolutions (path_prefix, path_proxy):
global br, username, password

Expand All @@ -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()
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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("<textarea"))
box_end = entire_page.find("</textarea>", box_start)
selected_lang = entire_page[entire_page.find("selected>", box_end)+9:entire_page.find("</option>", 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()
Expand Down