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 | { |
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 | import requests |
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 | $ npm install -g leetcode-cli |
Using command leetcode submission
, you can download your former submissions.
1 | $ leetcode submission 1 |
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 | import requests |
Then run the bash script:
1 | $ chomd 777 ./get_ac.sh |
Now your can get all your accepted submissions code!