1
1
import json
2
- import leetcode
3
2
from bs4 import BeautifulSoup
4
3
5
4
from color import Colors
6
5
7
6
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
7
def process_api_response (api_response ):
22
8
if api_response .data :
23
9
pass
@@ -30,54 +16,14 @@ def read_json_file(file_path):
30
16
return json .load (json_file )
31
17
32
18
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
50
24
51
25
52
26
# 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
27
82
28
83
29
# for --solve
@@ -130,9 +76,9 @@ def fetch_code_content(api_instance, title_slug):
130
76
# return python3_snippet
131
77
132
78
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
+ )
136
82
if python3_snippet :
137
83
return python3_snippet
138
84
0 commit comments