Last active
August 11, 2022 05:57
-
-
Save ImSingee/e08166aaa08d2c658738156ea88b2880 to your computer and use it in GitHub Desktop.
Revisions
-
ImSingee revised this gist
Aug 11, 2022 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,5 @@ #!/opt/bin/python3 import os import requests -
ImSingee created this gist
Aug 11, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,93 @@ #!/opt/bin/python3 # pip install markdownify import os import requests question_id = os.environ.get('KMVAR_LeetCodeID', 'add-two-numbers').strip() cookies={ "LEETCODE_SESSION": '' } def get_csrf_cookie() -> str: response = requests.get( "https://leetcode.com/", cookies=cookies, ) return response.cookies["csrftoken"] HEADERS = { 'x-csrftoken': get_csrf_cookie(), } def leetcode_id_to_slug(num_id: int): query = '''query problemsetQuestionList($categorySlug: String, $limit: Int, $skip: Int, $filters: QuestionListFilterInput) { problemsetQuestionList( categorySlug: $categorySlug limit: $limit skip: $skip filters: $filters ) { hasMore total questions { acRate difficulty freqBar frontendQuestionId isFavor paidOnly solutionNum status title titleCn titleSlug topicTags { name nameTranslated id slug } extra { hasVideoSolution topCompanyTags { imgUrl slug numSubscribed } } } } }''' request_body = { 'query': query, 'variables': { 'categorySlug': '', 'filters': { 'searchKeywords': '{}.'.format(num_id) }, } } response = requests.post('https://leetcode-cn.com/graphql/', headers=HEADERS, cookies=cookies, json=request_body) questions = response.json()['data']['problemsetQuestionList']['questions'] for question in questions: if str(question['frontendQuestionId']) == str(num_id): return question['titleSlug'] return questions[0]['titleSlug'] if question_id.isnumeric(): question_id = leetcode_id_to_slug(int(question_id)) print(question_id)