Skip to content

Commit e154be1

Browse files
committed
Use chrome headers when querying leetcode APIs
1 parent f905c6a commit e154be1

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This script uses LeetCode REST and GraphQL APIs to download all your LeetCode su
2525

2626
Before running the script, make sure that python3 is installed in your system.
2727

28-
If you prefer, you can use the Docker image to download LeetCode submissions. For further instructions read the
28+
If you prefer, you can use the Docker image to download LeetCode submissions. For more information read the
2929
section [Docker Image](#docker-image).
3030

3131
## 🏁 Getting started <a name="getting-started"></a>

leetcode_export/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def main():
157157

158158
if not leetcode.set_cookies(cookies):
159159
logging.error(
160-
"Cookies not valid. Copy them from the Network tab of your browser by clicking on any leetcode.com request and going in Request Headers > cookie. Check README.md file for further information"
160+
"Cookies not valid. Copy them from the Network tab of your browser by clicking on any leetcode.com request and going in Request Headers > cookie. Check README.md file for more information"
161161
)
162162
exit(1)
163163

leetcode_export/leetcode.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,24 @@
1111

1212
from leetcode_export.leetcode_graphql import GRAPHQL_URL, question_detail_json, Problem
1313
from leetcode_export.leetcode_rest import (
14+
BASE_URL,
1415
LOGIN_URL,
1516
SUBMISSIONS_API_URL,
1617
Submission,
17-
BASE_URL,
1818
)
1919
from leetcode_export.utils import (
20+
dict_camelcase_to_snakecase,
2021
language_to_extension,
2122
remove_special_characters,
22-
dict_camelcase_to_snakecase,
23+
REQUEST_HEADERS,
2324
)
2425

2526

2627
class LeetCode(object):
2728
def __init__(self):
2829
logging.debug("LeetCode class instantiated")
2930
self.session = requests.Session()
31+
self.session.headers.update(REQUEST_HEADERS)
3032
self.user_logged = False
3133
self.user_logged_expiration = datetime.datetime.now()
3234

@@ -104,6 +106,7 @@ def is_user_logged(self) -> bool:
104106
cookie_dict = self.session.cookies.get_dict()
105107
if "csrftoken" in cookie_dict and "LEETCODE_SESSION" in cookie_dict:
106108
get_request = self.session.get(SUBMISSIONS_API_URL.format(0, 1))
109+
logging.debug(get_request.text)
107110
sleep(1) # cooldown time for get request
108111
if "detail" not in get_request.json():
109112
logging.debug("User is logged in")
@@ -145,9 +148,9 @@ def get_submissions(self) -> Iterator[Submission]:
145148
and response_json["has_next"]
146149
):
147150
logging.debug(f"Exporting submissions from {current} to {current + 20}")
148-
response_json = self.session.get(
149-
SUBMISSIONS_API_URL.format(current, 20)
150-
).json()
151+
response = self.session.get(SUBMISSIONS_API_URL.format(current, 20))
152+
logging.debug(response.content)
153+
response_json = response.json()
151154
if "submissions_dump" in response_json:
152155
for submission_dict in response_json["submissions_dump"]:
153156
submission_dict["runtime"] = submission_dict["runtime"].replace(

leetcode_export/utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@
6363
_regex_camelcase_to_snakecase1 = re.compile(r"(.)([A-Z][a-z]+)")
6464
_regex_camelcase_to_snakecase2 = re.compile("([a-z0-9])([A-Z])")
6565

66+
REQUEST_HEADERS = {
67+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
68+
"Sec-Ch-Ua": '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
69+
"Sec-Ch-Ua-Mobile": "?0",
70+
"Sec-Ch-Ua-Platform": '"Windows"',
71+
"Sec-Fetch-Dest": "document",
72+
"Sec-Fetch-Mode": "navigate",
73+
"Sec-Fetch-Site": "cross-site",
74+
"Sec-Fetch-User": "?1",
75+
"Upgrade-Insecure-Requests": "1",
76+
}
77+
6678

6779
def language_to_extension(language: str) -> str:
6880
"""

0 commit comments

Comments
 (0)