Skip to content

Commit f92090e

Browse files
committed
Introduce multithreading for requests
Fixes #217
1 parent b377606 commit f92090e

File tree

2 files changed

+40
-27
lines changed

2 files changed

+40
-27
lines changed

cron/update_questions.py

+39-26
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import os
22
import json
3+
import uuid
34
import leetcode
45
import leetcode.auth
56
from datetime import datetime
67
from leetcode.rest import ApiException
8+
from concurrent.futures import ThreadPoolExecutor, as_completed
79

810

911
def 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

4951
def 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

7285
def read_questions(file_name):
@@ -97,30 +110,30 @@ 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 runner(api, question_list):
114+
print(f"📡 Retrieving question metadata from Leetcode")
115+
116+
threads = []
103117

104-
for question in questions["data"]:
105-
title_slug = question["slug"]
118+
with ThreadPoolExecutor(max_workers=5) 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))
106123

107-
response = get_question_metadata(api, title_slug)
124+
for task in as_completed(threads):
125+
update_question_metadata(question, task.result())
108126

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
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)
114133

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"])
134+
runner(api, questions["data"])
121135

122-
update_question_metadata(question, question_title, question_difficulty,
123-
companies, question_is_premium)
136+
# print(responses)
124137

125138
write_questions(file_name, questions)
126139

src/data/questions.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"updated": "2022-08-07T17:24:54.842630",
2+
"updated": "2022-08-10T23:42:42.022735",
33
"data": [
44
{
55
"id": 0,

0 commit comments

Comments
 (0)