|
14 | 14 | import genanki # type: ignore
|
15 | 15 | # https://github.com/prius/python-leetcode
|
16 | 16 | import leetcode # type: ignore
|
| 17 | +import leetcode.auth # type: ignore |
17 | 18 | import urllib3
|
18 | 19 | from tqdm import tqdm
|
19 | 20 |
|
20 |
| -cookies = { |
21 |
| - "csrftoken": os.environ["LEETCODE_CSRF_TOKEN"], |
22 |
| - "LEETCODE_SESSION": os.environ["LEETCODE_SESSION_ID"], |
23 |
| -} |
24 |
| - |
25 | 21 | LEETCODE_ANKI_MODEL_ID = 4567610856
|
26 | 22 | LEETCODE_ANKI_DECK_ID = 8589798175
|
27 | 23 | OUTPUT_FILE = "leetcode.apkg"
|
@@ -77,19 +73,7 @@ class LeetcodeData:
|
77 | 73 | def __init__(self) -> None:
|
78 | 74 |
|
79 | 75 | # Initialize leetcode API client
|
80 |
| - cookies = { |
81 |
| - "csrftoken": os.environ["LEETCODE_CSRF_TOKEN"], |
82 |
| - "LEETCODE_SESSION": os.environ["LEETCODE_SESSION_ID"], |
83 |
| - } |
84 |
| - |
85 |
| - configuration = leetcode.Configuration() |
86 |
| - |
87 |
| - configuration.api_key["x-csrftoken"] = cookies["csrftoken"] |
88 |
| - configuration.api_key["csrftoken"] = cookies["csrftoken"] |
89 |
| - configuration.api_key["LEETCODE_SESSION"] = cookies["LEETCODE_SESSION"] |
90 |
| - configuration.api_key["Referer"] = "https://leetcode.com" |
91 |
| - configuration.debug = False |
92 |
| - self._api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration)) |
| 76 | + self._api_instance = get_leetcode_api_client() |
93 | 77 |
|
94 | 78 | # Init problem data cache
|
95 | 79 | if not os.path.exists(CACHE_DIR):
|
@@ -250,9 +234,12 @@ def guid(self):
|
250 | 234 | def get_leetcode_api_client() -> leetcode.DefaultApi:
|
251 | 235 | configuration = leetcode.Configuration()
|
252 | 236 |
|
253 |
| - configuration.api_key["x-csrftoken"] = cookies["csrftoken"] |
254 |
| - configuration.api_key["csrftoken"] = cookies["csrftoken"] |
255 |
| - configuration.api_key["LEETCODE_SESSION"] = cookies["LEETCODE_SESSION"] |
| 237 | + session_id = os.environ["LEETCODE_SESSION_ID"] |
| 238 | + csrf_token = leetcode.auth.get_csrf_cookie(session_id) |
| 239 | + |
| 240 | + configuration.api_key["x-csrftoken"] = csrf_token |
| 241 | + configuration.api_key["csrftoken"] = csrf_token |
| 242 | + configuration.api_key["LEETCODE_SESSION"] = session_id |
256 | 243 | configuration.api_key["Referer"] = "https://leetcode.com"
|
257 | 244 | configuration.debug = False
|
258 | 245 | api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration))
|
|
0 commit comments