Skip to content

Commit

Permalink
Merge pull request #9 from prius/dev-auth
Browse files Browse the repository at this point in the history
New auth model + got rid of duplicating code
  • Loading branch information
fspv authored Oct 22, 2021
2 parents 955b68f + 66bf92d commit 620ac77
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 24 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/build-deck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ jobs:
- name: Test build Anki Deck
run: python generate.py --stop 3
env:
LEETCODE_CSRF_TOKEN: ${{ secrets.LEETCODE_CSRF_TOKEN }}
LEETCODE_SESSION_ID: ${{ secrets.LEETCODE_SESSION_ID }}
- name: Build Anki Deck
run: python generate.py
if: github.ref == 'refs/heads/master'
env:
LEETCODE_CSRF_TOKEN: ${{ secrets.LEETCODE_CSRF_TOKEN }}
LEETCODE_SESSION_ID: ${{ secrets.LEETCODE_SESSION_ID }}
- name: Create Release
id: create_release
Expand Down
29 changes: 8 additions & 21 deletions generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@
import genanki # type: ignore
# https://github.com/prius/python-leetcode
import leetcode # type: ignore
import leetcode.auth # type: ignore
import urllib3
from tqdm import tqdm

cookies = {
"csrftoken": os.environ["LEETCODE_CSRF_TOKEN"],
"LEETCODE_SESSION": os.environ["LEETCODE_SESSION_ID"],
}

LEETCODE_ANKI_MODEL_ID = 4567610856
LEETCODE_ANKI_DECK_ID = 8589798175
OUTPUT_FILE = "leetcode.apkg"
Expand Down Expand Up @@ -77,19 +73,7 @@ class LeetcodeData:
def __init__(self) -> None:

# Initialize leetcode API client
cookies = {
"csrftoken": os.environ["LEETCODE_CSRF_TOKEN"],
"LEETCODE_SESSION": os.environ["LEETCODE_SESSION_ID"],
}

configuration = leetcode.Configuration()

configuration.api_key["x-csrftoken"] = cookies["csrftoken"]
configuration.api_key["csrftoken"] = cookies["csrftoken"]
configuration.api_key["LEETCODE_SESSION"] = cookies["LEETCODE_SESSION"]
configuration.api_key["Referer"] = "https://leetcode.com"
configuration.debug = False
self._api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration))
self._api_instance = get_leetcode_api_client()

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

configuration.api_key["x-csrftoken"] = cookies["csrftoken"]
configuration.api_key["csrftoken"] = cookies["csrftoken"]
configuration.api_key["LEETCODE_SESSION"] = cookies["LEETCODE_SESSION"]
session_id = os.environ["LEETCODE_SESSION_ID"]
csrf_token = leetcode.auth.get_csrf_cookie(session_id)

configuration.api_key["x-csrftoken"] = csrf_token
configuration.api_key["csrftoken"] = csrf_token
configuration.api_key["LEETCODE_SESSION"] = session_id
configuration.api_key["Referer"] = "https://leetcode.com"
configuration.debug = False
api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration))
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
python-leetcode==1.0.6
python-leetcode==1.0.9
setuptools==57.5.0
diskcache
genanki
Expand Down

0 comments on commit 620ac77

Please sign in to comment.