Skip to content

Instantly share code, notes, and snippets.

@ImSingee
Last active August 11, 2022 05:57
Show Gist options
  • Select an option

  • Save ImSingee/e08166aaa08d2c658738156ea88b2880 to your computer and use it in GitHub Desktop.

Select an option

Save ImSingee/e08166aaa08d2c658738156ea88b2880 to your computer and use it in GitHub Desktop.

Revisions

  1. ImSingee revised this gist Aug 11, 2022. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions KM_LeetCode_ID.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    #!/opt/bin/python3

    # pip install markdownify

    import os
    import requests

  2. ImSingee created this gist Aug 11, 2022.
    93 changes: 93 additions & 0 deletions KM_LeetCode_ID.py
    Original 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)