Created
August 11, 2022 05:39
-
-
Save ImSingee/ca50e8acf1562ba135f314263c50bbd5 to your computer and use it in GitHub Desktop.
This file contains 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 characters
#!/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 get_leetcode_question_title(title_slug: str): | |
query = '''query questionData($titleSlug: String!) { | |
question(titleSlug: $titleSlug) { | |
questionId | |
questionFrontendId | |
translatedTitle | |
__typename | |
} | |
} | |
''' | |
request_body = { | |
'operationName': "questionData", | |
'query': query, | |
'variables': { | |
'titleSlug': title_slug, | |
} | |
} | |
response = requests.post('https://leetcode-cn.com/graphql/', headers=HEADERS, cookies=cookies, json=request_body) | |
return response.json()['data']['question']['translatedTitle'] | |
print(get_leetcode_question_title(question_id).strip()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment