Last active
December 26, 2023 00:34
-
-
Save feyhi/b7f28a61150b717700871073706e44e5 to your computer and use it in GitHub Desktop.
Get your Leetcode submission history
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 time | |
import requests | |
import datetime | |
import cPickle as pickle | |
from bs4 import BeautifulSoup | |
LOGIN = "login" | |
PASSWD = "password" | |
LOGIN_URL = "https://leetcode.com/accounts/login/" | |
now = datetime.datetime.now() | |
session = requests.Session() | |
# Get cookies | |
session.get(LOGIN_URL) | |
csrftoken = session.cookies['csrftoken'] | |
login_data = {'login': LOGIN, 'password': PASSWD, 'csrfmiddlewaretoken': csrftoken} | |
session.post("https://leetcode.com/accounts/login/", data=login_data, headers=dict(Referer=LOGIN_URL)) | |
def getSubmissionPage(pageNum): | |
s = session.get("https://leetcode.com/submissions/%d/" % (pageNum)) | |
soup = BeautifulSoup(s.text) | |
table = soup.find('table') | |
rows = table.find_all('tr') | |
records = [] | |
for row in rows[1:]: | |
cols = row.find_all('td') | |
time = cols[0].text | |
question = cols[1].text | |
link = cols[1].find('a')['href'] | |
status = cols[2].text | |
submission = cols[2].find('a')['href'] | |
runTime = cols[3].text | |
language = cols[4].text | |
record = [time, question, link, status, submission, runTime, language] | |
record = map(lambda x: x.strip().replace(u'\xa0', u' '), record) | |
records.append(record) | |
return records | |
submissions = [] | |
page = 1 | |
while True: | |
try: | |
submissions += getSubmissionPage(page) | |
page += 1 | |
print "Retrived page %d" % (page) | |
# Adjust to whatever you like | |
time.sleep(5) | |
except AttributeError: | |
# Reaches the end | |
break | |
# Save "now" for calcualting the timestamp | |
# [u'2 years, 11 months ago', u'Two Sum', u'/problems/two-sum/', u'Accepted', u'/submissions/detail/xxxx/', u'40 ms', u'cpp'] | |
pickle.dump((now, submissions), open("submissions.p", "wb")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment