1
1
import os
2
2
import json
3
- import uuid
4
3
import leetcode
5
4
import leetcode .auth
6
5
from datetime import datetime
7
6
from leetcode .rest import ApiException
8
- from concurrent .futures import ThreadPoolExecutor , as_completed
9
7
10
8
11
9
def create_leetcode_api ():
@@ -28,7 +26,6 @@ def get_question_metadata(api, title_slug):
28
26
query = '''query questionData($titleSlug: String!) {
29
27
question(titleSlug: $titleSlug) {
30
28
title
31
- titleSlug
32
29
difficulty
33
30
companyTagStats
34
31
isPaidOnly
@@ -41,12 +38,13 @@ def get_question_metadata(api, title_slug):
41
38
42
39
try :
43
40
response = api .graphql_post (body = graphql_request )
44
- return response
45
41
except ApiException as e :
46
42
print (
47
43
f'Exception occurred when contacting the Leetcode GraphQL API: ${ e } ' )
48
44
exit ()
49
45
46
+ return response
47
+
50
48
51
49
def construct_company_tag_list (company_tags_json , sections ):
52
50
companies = []
@@ -62,24 +60,13 @@ def construct_company_tag_list(company_tags_json, sections):
62
60
return sorted (companies , key = lambda d : d ['frequency' ], reverse = True )
63
61
64
62
65
- def update_question_metadata (question , response ):
66
- question_title = response .data .question .title
67
- question_difficulty = response .data .question .difficulty
68
- question_company_tags = json .loads (
69
- response .data .question .company_tag_stats )
70
- question_is_premium = response .data .question .is_paid_only
71
-
72
- # Retrieve companies who have asked this question within the following two
73
- # company_tag_stat sections:
74
- # 1. 0-6 months
75
- # 2. 6 months to 1 year
76
- companies = construct_company_tag_list (
77
- question_company_tags , ["1" , "2" ])
63
+ def update_question_metadata (question , title , difficulty , companies , is_premium ):
64
+ print (f"🔄 Updating question metadata for { title } " )
78
65
79
- question ["title" ] = question_title
80
- question ["difficulty" ] = question_difficulty
66
+ question ["title" ] = title
67
+ question ["difficulty" ] = difficulty
81
68
question ["companies" ] = companies
82
- question ["premium" ] = question_is_premium
69
+ question ["premium" ] = is_premium
83
70
84
71
85
72
def read_questions (file_name ):
@@ -110,28 +97,30 @@ def write_questions(file_name, questions):
110
97
exit ()
111
98
112
99
113
- def runners (api , question_list ):
114
- print (f"📡 Retrieving question metadata from Leetcode" )
115
-
116
- threads = []
117
-
118
- with ThreadPoolExecutor (max_workers = 3 ) as executor :
119
- for question in question_list :
120
- title_slug = question ["slug" ]
121
- threads .append (executor .submit (
122
- get_question_metadata , api , title_slug ))
100
+ def main (file_name ):
101
+ api = create_leetcode_api ()
102
+ questions = read_questions (file_name )
123
103
124
- for task in as_completed ( threads ) :
125
- update_question_metadata ( question , task . result ())
104
+ for question in questions [ "data" ] :
105
+ title_slug = question [ "slug" ]
126
106
127
- print ( f"✅ Finished retrieving question metadata from Leetcode" )
107
+ response = get_question_metadata ( api , title_slug )
128
108
109
+ question_title = response .data .question .title
110
+ question_difficulty = response .data .question .difficulty
111
+ question_company_tags = json .loads (
112
+ response .data .question .company_tag_stats )
113
+ question_is_premium = response .data .question .is_paid_only
129
114
130
- def main (file_name ):
131
- api = create_leetcode_api ()
132
- questions = read_questions (file_name )
115
+ # Retrieve companies who have asked this question within the following two
116
+ # company_tag_stat sections:
117
+ # 1. 0-6 months
118
+ # 2. 6 months to 1 year
119
+ companies = construct_company_tag_list (
120
+ question_company_tags , ["1" , "2" ])
133
121
134
- runners (api , questions ["data" ])
122
+ update_question_metadata (question , question_title , question_difficulty ,
123
+ companies , question_is_premium )
135
124
136
125
write_questions (file_name , questions )
137
126
0 commit comments