|
| 1 | +import json |
| 2 | +import leetcode |
| 3 | +from bs4 import BeautifulSoup |
| 4 | + |
| 5 | +from color import Colors |
| 6 | + |
| 7 | + |
| 8 | +# configuration |
| 9 | +def configure_api_instance(csrf_token, leetcode_session): |
| 10 | + configuration = leetcode.Configuration() |
| 11 | + configuration.api_key["x-csrftoken"] = csrf_token |
| 12 | + configuration.api_key["csrftoken"] = csrf_token |
| 13 | + configuration.api_key["LEETCODE_SESSION"] = leetcode_session |
| 14 | + configuration.api_key["Referer"] = "https://leetcode.com" |
| 15 | + configuration.debug = False |
| 16 | + |
| 17 | + api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration)) |
| 18 | + return api_instance |
| 19 | + |
| 20 | + |
| 21 | +def process_api_response(api_response): |
| 22 | + if api_response.data: |
| 23 | + pass |
| 24 | + else: |
| 25 | + print("No data found in the API response.") |
| 26 | + |
| 27 | + |
| 28 | +def read_json_file(file_path): |
| 29 | + with open(file_path, "r") as json_file: |
| 30 | + return json.load(json_file) |
| 31 | + |
| 32 | + |
| 33 | +# for get question data by question id |
| 34 | +def get_question_data_by_id(json_data, q): |
| 35 | + questions = ( |
| 36 | + json_data.get("data", {}).get("problemsetQuestionList", {}).get("questions", []) |
| 37 | + ) |
| 38 | + |
| 39 | + for question in questions: |
| 40 | + if q == question.get("frontendQuestionId"): |
| 41 | + return question |
| 42 | + return None |
| 43 | + |
| 44 | + |
| 45 | +# for graphql excution |
| 46 | +def execute_graphql_query(api_instance, graphql_query, query_variables): |
| 47 | + graphql_request = leetcode.GraphqlQuery(query=graphql_query, variables=query_variables) |
| 48 | + api_response = api_instance.graphql_post(body=graphql_request) |
| 49 | + return api_response |
| 50 | + |
| 51 | + |
| 52 | +# this is for print the question list in order |
| 53 | +def print_question_data(question): |
| 54 | + question_id = question.get("frontendQuestionId") |
| 55 | + title = question.get("title") |
| 56 | + difficulty = question.get("difficulty") |
| 57 | + ac_rate = question.get("acRate") |
| 58 | + |
| 59 | + # Fix ac_rate position regardless of the length of difficulty |
| 60 | + difficulty_color = "" |
| 61 | + if difficulty == "Easy": |
| 62 | + difficulty_color = Colors.GREEN |
| 63 | + elif difficulty == "Medium": |
| 64 | + difficulty_color = Colors.ORANGE |
| 65 | + elif difficulty == "Hard": |
| 66 | + difficulty_color = Colors.RED |
| 67 | + |
| 68 | + # Set fixed widths for the title and difficulty columns |
| 69 | + title_width = 50 |
| 70 | + difficulty_width = 10 |
| 71 | + |
| 72 | + # Align and pad the title and difficulty columns |
| 73 | + title_formatted = title.ljust(title_width)[:title_width] |
| 74 | + difficulty_formatted = ( |
| 75 | + f"{difficulty_color}{difficulty.ljust(difficulty_width)}{Colors.RESET}" |
| 76 | + ) |
| 77 | + |
| 78 | + print( |
| 79 | + f"[{str(question_id).rjust(3)}] {title_formatted} {difficulty_formatted} ({ac_rate:.2f}%)" |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +# for --solve |
| 84 | +def get_question_content(api_instance, title_slug): |
| 85 | + graphql_query = "query questionContent($titleSlug: String!) { question(titleSlug: $titleSlug) { content } }" |
| 86 | + query_variables = {"titleSlug": title_slug} |
| 87 | + api_response = execute_graphql_query(api_instance, graphql_query, query_variables) |
| 88 | + content = api_response.data.question.content |
| 89 | + return content |
| 90 | + |
| 91 | + |
| 92 | +def html_to_text(html_content): |
| 93 | + soup = BeautifulSoup(html_content, 'html.parser') |
| 94 | + plain_text = soup.get_text() |
| 95 | + return plain_text |
| 96 | + |
| 97 | + |
| 98 | +def fetch_code_content(api_instance, title_slug): |
| 99 | + graphql_query = """ |
| 100 | + query questionEditorData($titleSlug: String!) { |
| 101 | + question(titleSlug: $titleSlug) { |
| 102 | + questionId |
| 103 | + questionFrontendId |
| 104 | + codeSnippets { |
| 105 | + lang |
| 106 | + langSlug |
| 107 | + code |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + """ |
| 112 | + |
| 113 | + query_variables = { |
| 114 | + "titleSlug": title_slug |
| 115 | + } |
| 116 | + |
| 117 | + api_response = execute_graphql_query(api_instance, graphql_query, query_variables) |
| 118 | + |
| 119 | + # if api_response and api_response.data and api_response.data.question: |
| 120 | + # question_data = api_response.data.question |
| 121 | + # |
| 122 | + # if question_data.code_snippets: |
| 123 | + # code_snippets = question_data.code_snippets |
| 124 | + # python3_snippet = next( |
| 125 | + # (snippet for snippet in code_snippets if snippet.lang_slug == "python3"), |
| 126 | + # None |
| 127 | + # ) |
| 128 | + # |
| 129 | + # if python3_snippet: |
| 130 | + # return python3_snippet |
| 131 | + |
| 132 | + python3_snippet = next( |
| 133 | + (snippet for snippet in api_response.data.question.code_snippets if snippet.lang_slug == "python3"), |
| 134 | + None |
| 135 | + ) |
| 136 | + if python3_snippet: |
| 137 | + return python3_snippet |
| 138 | + |
| 139 | + return None |
0 commit comments