Last active
December 24, 2020 17:26
-
-
Save DefiPanda/8297202 to your computer and use it in GitHub Desktop.
Python script to save your Leetcode submissions locally.Simply sign in to your leetcode account, replace XXXX in my code to your sessionid and run "leetcode.py", and please don't log out on your browser meanwhile
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 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"]) |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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