Skip to content

Commit 620ac77

Browse files
authored
Merge pull request #9 from prius/dev-auth
New auth model + got rid of duplicating code
2 parents 955b68f + 66bf92d commit 620ac77

File tree

3 files changed

+9
-24
lines changed

3 files changed

+9
-24
lines changed

.github/workflows/build-deck.yml

-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ jobs:
2222
- name: Test build Anki Deck
2323
run: python generate.py --stop 3
2424
env:
25-
LEETCODE_CSRF_TOKEN: ${{ secrets.LEETCODE_CSRF_TOKEN }}
2625
LEETCODE_SESSION_ID: ${{ secrets.LEETCODE_SESSION_ID }}
2726
- name: Build Anki Deck
2827
run: python generate.py
2928
if: github.ref == 'refs/heads/master'
3029
env:
31-
LEETCODE_CSRF_TOKEN: ${{ secrets.LEETCODE_CSRF_TOKEN }}
3230
LEETCODE_SESSION_ID: ${{ secrets.LEETCODE_SESSION_ID }}
3331
- name: Create Release
3432
id: create_release

generate.py

+8-21
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@
1414
import genanki # type: ignore
1515
# https://github.com/prius/python-leetcode
1616
import leetcode # type: ignore
17+
import leetcode.auth # type: ignore
1718
import urllib3
1819
from tqdm import tqdm
1920

20-
cookies = {
21-
"csrftoken": os.environ["LEETCODE_CSRF_TOKEN"],
22-
"LEETCODE_SESSION": os.environ["LEETCODE_SESSION_ID"],
23-
}
24-
2521
LEETCODE_ANKI_MODEL_ID = 4567610856
2622
LEETCODE_ANKI_DECK_ID = 8589798175
2723
OUTPUT_FILE = "leetcode.apkg"
@@ -77,19 +73,7 @@ class LeetcodeData:
7773
def __init__(self) -> None:
7874

7975
# 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()
9377

9478
# Init problem data cache
9579
if not os.path.exists(CACHE_DIR):
@@ -250,9 +234,12 @@ def guid(self):
250234
def get_leetcode_api_client() -> leetcode.DefaultApi:
251235
configuration = leetcode.Configuration()
252236

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
256243
configuration.api_key["Referer"] = "https://leetcode.com"
257244
configuration.debug = False
258245
api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration))

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
python-leetcode==1.0.6
1+
python-leetcode==1.0.9
22
setuptools==57.5.0
33
diskcache
44
genanki

0 commit comments

Comments
 (0)