Starting in Oct 2019, Leetcode added the recaptcha verification code during login, so it becomes difficult to crawl our own solutions directly using crawler.

Although there are still some alternative methods, like login emulation to bypass the recaptcha verification, they may be technically difficult to achieve. Here I will introduce a manual way to “crawl” your Leetcode solutions legally.

Get our solved problems ID

Actually, Leetcode provides an official API about the statuses of whole problems. The API is:

1
url = "https://leetcode.com/api/problems/all/"

Look at values provided by this API with key “stat_status_pairs”. Here is one piece of these values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"stat":{
"question_id":1467,
"question__article__live":null,
"question__article__slug":null,
"question__title":"Number of Transactions per Visit",
"question__title_slug":"number-of-transactions-per-visit",
"question__hide":false,
"total_acs":227,
"total_submitted":431,
"frontend_question_id":1336,
"is_new_question":true
},
"status":null,
"difficulty":{
"level":3
},
"paid_only":true,
"is_favor":false,
"frequency":0,
"progress":0
}

Here is the meaning for some fields:

Name Meaning
frontend_question_id Problem ID
status Whether user finished this problem: ‘ac’ - Accepted, ‘null’ - Not tried
difficulty Problem difficulty: ‘1’ - Easy, ‘2’ - Medium, ‘3’ - Hard
paid_only Premium problem if true

So we can analyze the fields of the API to get all our solved problems ID. Here are the python code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import requests
import json


user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
url = "https://leetcode.com/api/problems/all/"

headers = {'User-Agent': user_agent, 'Connection': 'keep-alive'}
resp = session.get(url, headers = headers, timeout = 10)

question_list = json.loads(resp.content.decode('utf-8'))

for question in question_list['stat_status_pairs']:
if question["status"] == "ac":
print("Solved problems: {}".format(question['stat']["frontend_question_id"]))

Get solution code with leetcode-cli

leetcode-cli is a third party cli tool for Leetcode. Using this tool, you can get your soultion code directly.

You can use npm to install leetcode-cli easily.

1
2
$ npm install -g leetcode-cli
$ leetcode version

Using command leetcode submission, you can download your former submissions.

1
2
$ leetcode submission 1
$ [ 1 ] Two Sum 1.two-sum.130475090.ac.cpp

Detail installation method and document for leetcode-cli can be find at: installation, document

Using script to download automatically

However, if we manually input the command, it is still time-consuming. We can write a bash script to download submissions automatically.

Make a little change of the original python code to write all commands into get_ac.sh file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import requests
import json


user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
url = "https://leetcode.com/api/problems/all/"

headers = {'User-Agent': user_agent, 'Connection': 'keep-alive'}
resp = session.get(url, headers = headers, timeout = 10)

question_list = json.loads(resp.content.decode('utf-8'))
f = open('get_ac.sh', 'w')

for question in question_list['stat_status_pairs']:
if question["status"] == "ac":
str1 = "leetcode submission {} -o submission/ \n".format(question['stat']["frontend_question_id"])
str2 = "sleep 5s\n"
f.write(str1)
f.write(str2)
f.close()

Then run the bash script:

1
2
$ chomd 777 ./get_ac.sh
$ ./get_ac.sh

Now your can get all your accepted submissions code!