Skip to content

File Extension #3

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 2 commits 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
259 changes: 136 additions & 123 deletions spojbackup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@

'''

_____ _____ _____ __ _ _
| __| _ | |__| | | |_ ___ ___| |_ _ _ ___
|__ | __| | | | | | . | .'| _| '_| | | . |
|_____|__| |_____|_____| |___|__,|___|_,_|___| _|
|_|
_____ _____ _____ __ _ _
| __| _ | |__| | | |_ ___ ___| |_ _ _ ___
|__ | __| | | | | | . | .'| _| '_| | | . |
|_____|__| |_____|_____| |___|__,|___|_,_|___| _|
|_|

Keywords: python, tools, algorithms, spoj

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the Secret Labs AB nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the Secret Labs AB nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Expand All @@ -46,122 +46,135 @@


try:
from mechanize import Browser
from mechanize import Browser
except ImportError:
print "mechanize required but missing"
sys.exit(1)

print "mechanize required but missing"
sys.exit(1)

#List of global variables
url = "http://spoj.com"
lang = {}

#Functions begin here
def setLangs ():
#Add new language codes here
lang["C++"] = "cpp"
lang["JAV"] = "java"
lang["C"] = "c"
lang["TEX"] = "txt"
lang["PYT"] = "py"
lang["BF"] = "b"
lang["PAS"] = "pas"
lang["PER"] = ".pl"
lang["C99"] = "cpp"
lang["ADA"] = "ada"
lang["PHP"] = "php"
lang["C#"] = "cs"
lang["RUB"] = "rb"
lang["WSP"] = "wsp"

def getExtension (language):
language = str(language)
return "." + lang.get( language, "bak")

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

# create a browser object
br = Browser()

# add proxy support to browser
if len(path_proxy) != 0:
protocol,proxy = options.proxy.split("://")
br.set_proxies({protocol:proxy})

# let browser fool 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 to avoid 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 submissions list
print "Grabbing siglist for " + username
siglist = br.open("http://www.spoj.pl/status/" + username + "/signedlist")

# dump first nine useless lines in signed list for formatting
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)

print "Done !!!"
return mysublist
global br, username, password

# create a browser object
br = Browser()

# add proxy support to browser
if len(path_proxy) != 0:
protocol,proxy = options.proxy.split("://")
br.set_proxies({protocol:proxy})

# let browser fool 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 ( url )
br.select_form (nr=0)
br['login_user'] = username
br['password'] = password

# sign in for a day to avoid timeouts
br.find_control("autologin").items[0].selected = True
response = br.submit()

verify = response.read()
if (verify.find("Authentication failed!") != -1):
print "Error authenticating - " + username
exit(0)

# grab the signed submissions list
print "Grabbing siglist for " + username
siglist = br.open( url + "/status/" + username + "/signedlist")

# dump first nine useless lines in signed list for formatting
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)

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)
totalsubmissions = len(mysublist)

print "Fetching sources into " + path_prefix
progress = 0

if __name__=="__main__":
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( url + "/files/src/save/" + entry[1] )
filename = entry[3] + "-" + entry[1] + getExtension(entry[7])
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)

parser = optparse.OptionParser()
parser.add_option("-o", "--outputpath", default="./", type=str, help="Destination directory to store solutions")
parser.add_option("-p", "--proxy", default = "", type = str, help = "Proxy , ex- http://username:password@proxy:port")

(options, args) = parser.parse_args()
path_prefix = options.outputpath
path_proxy = options.proxy

downloadSolutions(getSolutions(path_prefix, path_proxy))
if __name__=="__main__":

parser = optparse.OptionParser()
parser.add_option("-o", "--outputpath", default="./", type=str, help="Destination directory to store solutions")
parser.add_option("-p", "--proxy", default = "", type = str, help = "Proxy , ex- http://username:password@proxy:port")

(options, args) = parser.parse_args()
path_prefix = options.outputpath
path_proxy = options.proxy

#getSolutions(path_prefix, path_proxy)
setLangs()
downloadSolutions(getSolutions(path_prefix, path_proxy))