11import json
2- import leetcode
32from bs4 import BeautifulSoup
43
54from color import Colors
65
76
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-
217def process_api_response (api_response ):
228 if api_response .data :
239 pass
@@ -30,54 +16,14 @@ def read_json_file(file_path):
3016 return json .load (json_file )
3117
3218
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
19+ # # for graphql excution
20+ # def execute_graphql_query(api_instance, graphql_query, query_variables):
21+ # graphql_request = leetcode.GraphqlQuery(query=graphql_query, variables=query_variables)
22+ # api_response = api_instance.graphql_post(body=graphql_request)
23+ # return api_response
5024
5125
5226# 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- )
8127
8228
8329# for --solve
@@ -130,9 +76,9 @@ def fetch_code_content(api_instance, title_slug):
13076 # return python3_snippet
13177
13278 python3_snippet = next (
133- (snippet for snippet in api_response .data .question .code_snippets if snippet .lang_slug == "python3" ),
134- None
135- )
79+ (snippet for snippet in api_response .data .question .code_snippets if snippet .lang_slug == "python3" ),
80+ None
81+ )
13682 if python3_snippet :
13783 return python3_snippet
13884
0 commit comments