-
-
Save DefiPanda/8297202 to your computer and use it in GitHub Desktop.
import cookielib, urllib2, re, sys, HTMLParser, os.path | |
PHPSESSID = "XXXXXXXXXXXXXXXXXXXX" | |
def saveFileByType(code_content, parser, languages=[], postfixes=[]): | |
for index, language in enumerate(languages): | |
code_pattern = re.compile("""scope\.code\."""+language+""" = '.*?'""") | |
matched_code = code_pattern.search(code_content) | |
if(matched_code != None): | |
matched_code = matched_code.group(0) | |
code = matched_code.split("'")[1] | |
code = code.decode('unicode_escape') | |
title_pattern = re.compile("""/problems/.*</a></h4>""") | |
matched_title = title_pattern.search(code_content).group(0) | |
title = matched_title[matched_title.index('>')+1:matched_title.index('<')] | |
filename = "".join([title, "." + postfixes[index]]) | |
if os.path.isfile(filename) == False: | |
myFile = open(filename, 'w') | |
myFile.write(code) | |
myFile.close() | |
print "".join(["File saved with filename: ", title, "." + postfixes[index]]) | |
cj = cookielib.CookieJar() | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | |
parser = HTMLParser.HTMLParser() | |
opener.addheaders = [ | |
('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36'), | |
('Referer', 'http://oj.leetcode.com/accounts/login/'), | |
('Cookie', 'PHPSESSID=' + PHPSESSID + '; _ga=GA1.2.1984831265.1389075465; NRAGENT=tk=26e67c3be080fab2'), | |
] | |
for i in range(1,500): | |
page = 'http://oj.leetcode.com/submissions/' + `i` | |
print page | |
resp = opener.open(page) | |
content = resp.read() | |
error_pattern = re.compile("No more submissions.") | |
if error_pattern.search(content) is not None: | |
print "Program finished with all your submissions have been saved" | |
sys.exit() | |
url_pattern = re.compile("[0-9]*/\"><strong>Accepted</strong></a>") | |
for match in url_pattern.findall(content): | |
url = "".join(["http://oj.leetcode.com/submissions/detail/", match.split('/')[0]]) | |
resp = opener.open(url) | |
code_content = resp.read() | |
saveFileByType(code_content, parser, ["java", "python", "cpp"], ["java", "py", "cpp"]) |
So sorry I just saw your comment. Thanks a lot and I've updated the gist.
😄
Hi jw2013,
That's great help from you.
Your code was not working with the newest version of Leetcoe OJ.
I have updated your code and can be found at https://gist.github.com/rameshgkwd05/df5b09af01442e95607d
There is a minor updation only.
Thanks!
Ramesh
Hi all,
Seems like leetcode changed their page again, so here is updated code for this task. Let's call it Version 2016.02
https://gist.github.com/antonylhz/007c0e2cb73246782015
Thanks for the help!
Hongzhang
I have got a python project to save LeetCode solutions locally, too. I have just made my project compatible with latest Leetcode UI changes which happened a couple of months ago. I also started using LeetCode api wherever possible instead of relying on html contents. Here is the link to my repository. Please feel free to contribute.
Hi jw2013!
Thank you for your great code which helps me not only download my leet codes, but also learn more about python.
As Leetcode OJ has updated, your code may not work well with the newest version. I have changed it a little bit to make it run again :) You can pull my gist if you like. The url is: https://gist.github.com/chenluyao/502209c7654210c9aac2
I have only tested it on my leetcode where all codes are .cpp. So Java and python untested.
Have a good day!
Luyao