forked from seanprashad/leetcode-patterns
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_questions.py
132 lines (99 loc) · 3.86 KB
/
update_questions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
importos
importjson
importleetcode
importleetcode.auth
fromdatetimeimportdatetime
fromleetcode.restimportApiException
defcreate_leetcode_api():
LEETCODE_SESSION_TOKEN=os.environ.get("LEETCODE_SESSION_TOKEN")
csrf_token=leetcode.auth.get_csrf_cookie(LEETCODE_SESSION_TOKEN)
configuration=leetcode.Configuration()
configuration.api_key["x-csrftoken"] =csrf_token
configuration.api_key["csrftoken"] =csrf_token
configuration.api_key["LEETCODE_SESSION"] =LEETCODE_SESSION_TOKEN
configuration.api_key["Referer"] ="https://leetcode.com"
configuration.debug=False
returnleetcode.DefaultApi(leetcode.ApiClient(configuration))
defget_question_metadata(api, title_slug):
graphql_request=leetcode.GraphqlQuery(
query='''query questionData($titleSlug: String!) {
question(titleSlug: $titleSlug) {
title
difficulty
companyTagStats
isPaidOnly
}
}
''',
variables=leetcode.GraphqlQueryGetQuestionDetailVariables(
title_slug=title_slug)
)
try:
response=api.graphql_post(body=graphql_request)
returnresponse
exceptApiExceptionase:
print(
f'Exception occurred when contacting the Leetcode GraphQL API: ${e}')
exit()
defconstruct_company_tag_list(company_tags_json, sections):
companies= []
forsectioninsections:
forcompanyincompany_tags_json[section]:
companies.append({
"name": company["name"],
"slug": company["slug"],
"frequency": company["timesEncountered"]
})
returnsorted(companies, key=lambdad: d['frequency'], reverse=True)
defupdate_question_metadata(question, response):
print(f'''🔄 Updating question metadata for {question["title"]}''')
question_title=response.data.question.title
question_difficulty=response.data.question.difficulty
question_company_tags=json.loads(
response.data.question.company_tag_stats)
question_is_premium=response.data.question.is_paid_only
# Retrieve companies who have asked this question for the following two
# company_tag_stat sections:
# 1. 0-6 months
# 2. 6 months to 1 year
companies=construct_company_tag_list(
question_company_tags, ["1", "2"])
question["title"] =question_title
question["difficulty"] =question_difficulty
question["companies"] =companies
question["premium"] =question_is_premium
defread_questions(file_name):
print(f"💾 Loading {file_name}")
try:
withopen(file_name, "r") asfile:
questions=json.load(file)
print(f"✅ Finished loading {file_name}")
returnquestions
exceptExceptionase:
print(
f"❌ Exception occurred when reading {file_name}: {e}")
exit()
defwrite_questions(file_name, questions):
print(f"💾 Updating {file_name}")
try:
withopen(file_name, "w") asfile:
questions["updated"] =str(datetime.now().isoformat())
json.dump(questions, file, indent=2)
print(f"✅ Finished updating {file_name}")
exceptExceptionase:
print(
f"❌ Exception occurred when writing {file_name}: {e}")
exit()
defmain(file_name):
api=create_leetcode_api()
questions=read_questions(file_name)
forquestioninquestions["data"]:
title_slug=question["slug"]
response=get_question_metadata(api, title_slug)
update_question_metadata(question, response)
write_questions(file_name, questions)
if__name__=="__main__":
file_name=os.getcwd() +"/src/data/questions.json"
startTime=datetime.now()
main(file_name)
print(f"⏱️ Data updated in {datetime.now() -startTime} seconds")