forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspider.py
279 lines (258 loc) · 12.4 KB
/
spider.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import json
import os.path
import random
import re
import time
from typing import List
from urllib.parse import quote
import requests
import urllib3
urllib3.disable_warnings()
user_agent = (
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/77.0.3865.120 Safari/537.36'
)
sub_folders = [
str(i * 100).zfill(4) + '-' + str(i * 100 + 99).zfill(4) for i in range(100)
]
cn_graph_url = 'https://leetcode.cn/graphql'
difficulty = dict(Easy='简单', Medium='中等', Hard='困难')
"""
{
"sub_folder":"2200-2299",
"question_id":"2385",
"frontend_question_id":"2237",
"paid_only":true,
"paid_only_cn":true,
"category":"Algorithms",
"url_cn":"https://leetcode.cn/problems/count-positions-on-street-with-required-brightness",
"url_en":"https://leetcode.com/problems/count-positions-on-street-with-required-brightness",
"relative_path_cn":"/solution/2200-2299/2237.Count%20Positions%20on%20Street%20With%20Required%20Brightness/README.md",
"relative_path_en":"/solution/2200-2299/2237.Count%20Positions%20on%20Street%20With%20Required%20Brightness/README_EN.md",
"title_cn":"Count Positions on Street With Required Brightness",
"title_en":"Count Positions on Street With Required Brightness",
"question_title_slug":"count-positions-on-street-with-required-brightness",
"content_en":"Description",
"content_cn":"问题描述[此处省略]",
"tags_en":[
],
"tags_cn":[
],
"difficulty_en":"Medium",
"difficulty_cn":"中等",
"code_snippets":[
{
"lang":"C++",
"langSlug":"cpp",
"code":"class Solution {
public:
int meetRequirement(int n, vector<vector<int>>& lights, vector<int>& requirement) {
}
};",
"__typename":"CodeSnippetNode"
},
{
"lang":"Java",
"langSlug":"java",
"code":"class Solution {
public int meetRequirement(int n, int[][] lights, int[] requirement) {
}
}",
"__typename":"CodeSnippetNode"
}
],
"md_table_row_cn":[
"[2237](https://leetcode.cn/problems/count-positions-on-street-with-required-brightness)",
"[Count Positions on Street With Required Brightness](/solution/2200-2299/2237.Count%20Positions%20on%20Street%20With%20Required%20Brightness/README.md)",
"",
"中等",
"🔒"
],
"md_table_row_en":[
"[2237](https://leetcode.com/problems/count-positions-on-street-with-required-brightness)",
"[Count Positions on Street With Required Brightness](/solution/2200-2299/2237.Count%20Positions%20on%20Street%20With%20Required%20Brightness/README_EN.md)",
"",
"Medium",
"🔒"
]
}
"""
class Spider:
def __init__(self, cookie_cn: str, cookie_en: str):
self.cookie_cn = cookie_cn
self.cookie_en = cookie_en
self.session = requests.session()
self.raw_data = {}
self.contest = {}
if os.path.exists('./contest.json'):
with open('contest.json', 'r', encoding='utf-8') as f:
self.contest = json.loads(f.read())
def get_all_questions(self) -> List:
"""获取所有题目"""
headers = {
'accept': 'application/json, text/javascript, */*; q=0.01',
'content-type': 'application/json',
'user-agent': user_agent,
'x-requested-with': 'XMLHttpRequest',
'cookie': self.cookie_en,
}
resp = self.session.get(
url='https://leetcode.com/api/problems/all/',
headers=headers,
allow_redirects=False,
timeout=10,
verify=False,
)
return resp.json()['stat_status_pairs']
def _get_question_detail(self, question_title_slug) -> dict:
"""获取题目详情"""
form_data = {
'operationName': 'globalData',
'query': 'query globalData {\n feature {\n questionTranslation\n subscription\n signUp\n '
'discuss\n mockInterview\n contest\n store\n book\n chinaProblemDiscuss\n '
'socialProviders\n studentFooter\n cnJobs\n enableLsp\n enableWs\n '
'enableDebugger\n enableDebuggerAdmin\n enableDarkMode\n tasks\n '
'leetbook\n __typename\n }\n userStatus {\n isSignedIn\n isAdmin\n '
'isStaff\n isSuperuser\n isTranslator\n isPremium\n isVerified\n '
'isPhoneVerified\n isWechatVerified\n checkedInToday\n username\n '
'realName\n userSlug\n groups\n avatar\n optedIn\n '
'requestRegion\n region\n activeSessionId\n permissions\n notificationStatus {\n '
'lastModified\n numUnread\n __typename\n }\n completedFeatureGuides\n '
'useTranslation\n accountStatus {\n isFrozen\n inactiveAfter\n __typename\n '
'}\n __typename\n }\n siteRegion\n chinaHost\n websocketUrl\n userBannedInfo {\n '
'bannedData {\n endAt\n bannedType\n __typename\n }\n __typename\n }\n}\n',
'variables': {},
}
headers = {
'User-Agent': user_agent,
'Connection': 'keep-alive',
'Content-Type': 'application/json',
'Referer': 'https://leetcode.cn/problems/' + question_title_slug,
'cookie': self.cookie_cn,
}
self.session.post(
url=cn_graph_url,
data=json.dumps(form_data),
headers=headers,
timeout=10,
verify=False,
)
form_data = {
'operationName': 'questionData',
'variables': {'titleSlug': question_title_slug},
'query': 'query questionData($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n '
'questionId\n questionFrontendId\n categoryTitle\n boundTopicId\n title\n '
'titleSlug\n content\n translatedTitle\n translatedContent\n isPaidOnly\n '
'difficulty\n likes\n dislikes\n isLiked\n similarQuestions\n '
'contributors {\n username\n profileUrl\n avatarUrl\n __typename\n '
'}\n langToValidPlayground\n topicTags {\n name\n slug\n '
'translatedName\n __typename\n }\n companyTagStats\n codeSnippets {\n '
'lang\n langSlug\n code\n __typename\n }\n stats\n hints\n '
'solution {\n id\n canSeeDetail\n __typename\n }\n status\n '
'sampleTestCase\n metaData\n judgerAvailable\n judgeType\n mysqlSchemas\n '
'enableRunCode\n envInfo\n book {\n id\n bookName\n pressName\n '
'source\n shortDescription\n fullDescription\n bookImgUrl\n '
'pressImgUrl\n productUrl\n __typename\n }\n isSubscribed\n '
'isDailyQuestion\n dailyRecordStatus\n editorType\n ugcQuestionId\n style\n '
'exampleTestcases\n __typename\n }\n}\n',
}
# get question detail
resp = self.session.post(
url=cn_graph_url,
data=json.dumps(form_data).encode('utf-8'),
headers=headers,
timeout=10,
verify=False,
)
res = resp.json()
return res['data']['question']
def get_question_detail(self, question_title_slug, retry_times=0):
time.sleep(random.random())
for _ in range(retry_times + 1):
try:
question_detail = self._get_question_detail(question_title_slug)
if question_detail:
return question_detail
except Exception as e:
print(f'error:{str(e)}')
time.sleep(random.choice(range(2, 5)))
return None
def handle(self, question: dict) -> dict:
question_title_slug = question['stat']['question__title_slug']
question_detail = self.get_question_detail(question_title_slug, retry_times=5)
if not question_detail:
return {}
url_cn = f'https://leetcode.cn/problems/{question_title_slug}'
url_en = f'https://leetcode.com/problems/{question_title_slug}'
frontend_question_id = str(question['stat']['frontend_question_id']).zfill(4)
self.raw_data[frontend_question_id] = question_detail
no = int(frontend_question_id) // 100
question_title_en = question['stat']['question__title']
question_title_en = re.sub(r'[\\/:*?"<>|]', '', question_title_en).strip()
path_cn = f'/solution/{sub_folders[no]}/{frontend_question_id}.{quote(question_title_en)}/README.md'
path_en = f'/solution/{sub_folders[no]}/{frontend_question_id}.{quote(question_title_en)}/README_EN.md'
print(f'{frontend_question_id}. {question_title_en}')
topic_tags = question_detail.get('topicTags')
item = {
'sub_folder': sub_folders[no],
'question_id': str(question['stat']['question_id']).zfill(4),
'frontend_question_id': frontend_question_id,
'paid_only': question['paid_only'],
'paid_only_cn': question_detail.get('isPaidOnly'),
# Shell Database Algorithms Concurrency
'category': question_detail.get('categoryTitle'),
'url_cn': url_cn,
'url_en': url_en,
'relative_path_cn': path_cn,
'relative_path_en': path_en,
'title_cn': question_detail.get('translatedTitle')
or question_title_en
or '',
'title_en': question_title_en or '',
'question_title_slug': question_title_slug,
'content_en': question_detail.get('content'),
'content_cn': question_detail.get('translatedContent')
or question_detail.get('content'),
'tags_en': [e['name'] for e in topic_tags if e['name']] or [],
'tags_cn': [e['translatedName'] for e in topic_tags if e['translatedName']]
or [],
'difficulty_en': question_detail.get('difficulty'),
'difficulty_cn': difficulty.get(question_detail.get('difficulty')),
'code_snippets': question_detail.get('codeSnippets') or [],
}
col1_cn = frontend_question_id
col2_cn = (
f'[{item["title_cn"]}]({path_cn})'
if item["title_cn"]
else f'[{item["title_en"]}]({path_en})'
)
col3_cn = ','.join([f'`{tag}`' for tag in item['tags_cn']])
col3_cn = '' if (col3_cn == 'None' or not col3_cn) else col3_cn
col4_cn = item['difficulty_cn']
col5_cn = '🔒' if item['paid_only_cn'] else ''
if not col5_cn and question_title_slug in self.contest:
col5_cn = self.contest[question_title_slug]['contest_title']
col1_en = frontend_question_id
col2_en = f'[{item["title_en"]}]({path_en})'
col3_en = ','.join([f'`{tag}`' for tag in item['tags_en']])
col3_en = '' if (col3_en == 'None' or not col3_en) else col3_en
col4_en = item['difficulty_en']
col5_en = '🔒' if item['paid_only'] else ''
if not col5_en and question_title_slug in self.contest:
col5_en = self.contest[question_title_slug]['contest_title_en']
item['md_table_row_cn'] = [col1_cn, col2_cn, col3_cn, col4_cn, col5_cn]
item['md_table_row_en'] = [col1_en, col2_en, col3_en, col4_en, col5_en]
return item
def save(self):
with open('./raw.json', 'w', encoding='utf-8') as f:
f.write(json.dumps(self.raw_data))
def run(self):
questions = self.get_all_questions()
details = [self.handle(question) for question in questions]
failed_questions = [
question for i, question in enumerate(questions) if not details[i]
]
details += [self.handle(question) for question in failed_questions]
details = [detail for detail in details if detail]
self.save()
return details