11import os
22import json
3+ import uuid
34import leetcode
45import leetcode .auth
56from datetime import datetime
67from leetcode .rest import ApiException
8+ from concurrent .futures import ThreadPoolExecutor , as_completed
79
810
911def create_leetcode_api ():
@@ -26,6 +28,7 @@ def get_question_metadata(api, title_slug):
2628 query = '''query questionData($titleSlug: String!) {
2729 question(titleSlug: $titleSlug) {
2830 title
31+ titleSlug
2932 difficulty
3033 companyTagStats
3134 isPaidOnly
@@ -38,13 +41,12 @@ def get_question_metadata(api, title_slug):
3841
3942 try :
4043 response = api .graphql_post (body = graphql_request )
44+ return response
4145 except ApiException as e :
4246 print (
4347 f'Exception occurred when contacting the Leetcode GraphQL API: ${ e } ' )
4448 exit ()
4549
46- return response
47-
4850
4951def construct_company_tag_list (company_tags_json , sections ):
5052 companies = []
@@ -60,13 +62,24 @@ def construct_company_tag_list(company_tags_json, sections):
6062 return sorted (companies , key = lambda d : d ['frequency' ], reverse = True )
6163
6264
63- def update_question_metadata (question , title , difficulty , companies , is_premium ):
64- print (f"🔄 Updating question metadata for { title } " )
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" ])
6578
66- question ["title" ] = title
67- question ["difficulty" ] = difficulty
79+ question ["title" ] = question_title
80+ question ["difficulty" ] = question_difficulty
6881 question ["companies" ] = companies
69- question ["premium" ] = is_premium
82+ question ["premium" ] = question_is_premium
7083
7184
7285def read_questions (file_name ):
@@ -97,30 +110,28 @@ def write_questions(file_name, questions):
97110 exit ()
98111
99112
100- def main (file_name ):
101- api = create_leetcode_api ()
102- questions = read_questions (file_name )
113+ def runners (api , question_list ):
114+ print (f"📡 Retrieving question metadata from Leetcode" )
103115
104- for question in questions ["data" ]:
105- title_slug = question ["slug" ]
116+ threads = []
106117
107- response = get_question_metadata (api , title_slug )
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 ))
108123
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
124+ for task in as_completed (threads ):
125+ update_question_metadata (question , task .result ())
114126
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" ] )
127+ print ( f"✅ Finished retrieving question metadata from Leetcode" )
128+
129+
130+ def main ( file_name ):
131+ api = create_leetcode_api ()
132+ questions = read_questions ( file_name )
121133
122- update_question_metadata (question , question_title , question_difficulty ,
123- companies , question_is_premium )
134+ runners (api , questions ["data" ])
124135
125136 write_questions (file_name , questions )
126137
0 commit comments