11import os
22import json
3- import uuid
43import leetcode
54import leetcode .auth
65from datetime import datetime
76from leetcode .rest import ApiException
8- from concurrent .futures import ThreadPoolExecutor , as_completed
97
108
119def create_leetcode_api ():
@@ -28,7 +26,6 @@ def get_question_metadata(api, title_slug):
2826 query = '''query questionData($titleSlug: String!) {
2927 question(titleSlug: $titleSlug) {
3028 title
31- titleSlug
3229 difficulty
3330 companyTagStats
3431 isPaidOnly
@@ -41,12 +38,13 @@ def get_question_metadata(api, title_slug):
4138
4239 try :
4340 response = api .graphql_post (body = graphql_request )
44- return response
4541 except ApiException as e :
4642 print (
4743 f'Exception occurred when contacting the Leetcode GraphQL API: ${ e } ' )
4844 exit ()
4945
46+ return response
47+
5048
5149def construct_company_tag_list (company_tags_json , sections ):
5250 companies = []
@@ -62,24 +60,13 @@ def construct_company_tag_list(company_tags_json, sections):
6260 return sorted (companies , key = lambda d : d ['frequency' ], reverse = True )
6361
6462
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 } " )
7865
79- question ["title" ] = question_title
80- question ["difficulty" ] = question_difficulty
66+ question ["title" ] = title
67+ question ["difficulty" ] = difficulty
8168 question ["companies" ] = companies
82- question ["premium" ] = question_is_premium
69+ question ["premium" ] = is_premium
8370
8471
8572def read_questions (file_name ):
@@ -110,28 +97,30 @@ def write_questions(file_name, questions):
11097 exit ()
11198
11299
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 )
123103
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" ]
126106
127- print ( f"✅ Finished retrieving question metadata from Leetcode" )
107+ response = get_question_metadata ( api , title_slug )
128108
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
129114
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" ])
133121
134- runners (api , questions ["data" ])
122+ update_question_metadata (question , question_title , question_difficulty ,
123+ companies , question_is_premium )
135124
136125 write_questions (file_name , questions )
137126
0 commit comments