-
Notifications
You must be signed in to change notification settings - Fork 31
/
crawler_cn.py
307 lines (267 loc) · 16.3 KB
/
crawler_cn.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import json
import os.path
import pickle
import re
from sys import exit
import requests
from requests.cookies import RequestsCookieJar
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from database_cn import Problem, ProblemTag, Tag, Submission, create_tables, Solution
from utils import destructure, random_wait, do, get
COOKIE_PATH = "./cookies_cn.dat"
class LeetCodeCrawler:
def __init__(self):
# create an http session
self.session = requests.Session()
self.session.headers.update(
{
'Host': 'leetcode.cn',
'Cache-Control': 'max-age=0',
'Upgrade-Insecure-Requests': '1',
'Referer': 'https://leetcode.cn/accounts/login/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6',
'Connection': 'keep-alive'
}
)
def login(self):
browser_cookies = {}
if os.path.isfile(COOKIE_PATH):
with open(COOKIE_PATH, 'rb') as f:
browser_cookies = pickle.load(f)
else:
print("😎 Starting browser login..., please fill the login form")
browser = webdriver.Chrome(executable_path="./vendor/chromedriver")
try:
# browser login
login_url = "https://leetcode.cn/accounts/login"
browser.get(login_url)
WebDriverWait(browser, 24 * 60 * 3600).until(
lambda driver: driver.current_url.find("login") < 0
)
browser_cookies = browser.get_cookies()
with open(COOKIE_PATH, 'wb') as f:
pickle.dump(browser_cookies, f)
print("🎉 Login successfully")
except Exception as e:
print(f"🤔 Login Failed: {e}, please try again")
exit()
cookies = RequestsCookieJar()
for item in browser_cookies:
cookies.set(item['name'], item['value'])
if item['name'] == 'csrftoken':
self.session.headers.update({
"x-csrftoken": item['value']
})
self.session.cookies.update(cookies)
def fetch_accepted_problems(self):
response = self.session.get("https://leetcode.cn/api/problems/all/")
all_problems = json.loads(response.content.decode('utf-8'))
# filter AC problems
counter = 0
for item in all_problems['stat_status_pairs']:
if item['status'] == 'ac' and item['paid_only'] == False:
id, slug = destructure(item['stat'], "question_id", "question__title_slug")
# only update problem if not exists
if Problem.get_or_none(Problem.id == id) is None:
counter += 1
# fetch problem
do(self.questionData, args=[slug, True])
# fetch solution
do(self.fetch_questionSolutionArticles, args=[slug])
# always try to update submission
# do(self.fetch_submission, args=[slug])
do(self.fetch_lastSubmission, args=[slug])
print(f"🤖 Updated {counter} problems")
def questionData(self, slug, accepted=False):
print(f"🤖 Fetching problem: https://leetcode.cn/problems/{slug}/...")
query_params = {
"operationName":"questionData",
"variables":{
"titleSlug":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"
}
resp = self.session.post(
"https://leetcode.cn/graphql",
data=json.dumps(query_params).encode('utf8'),
headers={
"content-type": "application/json",
})
body = json.loads(resp.content)
# parse data
question = get(body, 'data.question')
Problem.replace(
id=question['questionId'], display_id=question['questionFrontendId'], title=question["translatedTitle"],
level=question["difficulty"], slug=slug, description=question['translatedContent'],
accepted=accepted
).execute()
for item in question['topicTags']:
if Tag.get_or_none(Tag.slug == item['slug']) is None:
Tag.replace(
name=item['translatedName'],
slug=item['slug']
).execute()
ProblemTag.replace(
problem=question['questionId'],
tag=item['slug']
).execute()
# random_wait(10, 15)
def fetch_lastSubmission(self,slug):
query_params = {
"operationName":"lastSubmission",
"variables":{
"lang":"java",
"questionSlug":slug
},
"query":"query lastSubmission($questionSlug: String!, $lang: String!) {\n lastSubmission(questionSlug: $questionSlug, lang: $lang) {\n id\n statusDisplay\n lang\n runtime\n timestamp\n url\n isPending\n memory\n submissionComment {\n comment\n flagType\n __typename\n }\n __typename\n }\n}\n"
}
resp = self.session.post("https://leetcode.cn/graphql",
data=json.dumps(query_params).encode('utf8'),
headers={
"content-type": "application/json",
})
body = json.loads(resp.content)
# parse data
solutionid = get(body, "data.lastSubmission")['id']
self.fetch_mySubmissionDetail(solutionid,slug)
def fetch_mySubmissionDetail(self,solutionid,slug):
query_params = {
"operationName":"mySubmissionDetail",
"variables":{
"id":solutionid
},
"query":"query mySubmissionDetail($id: ID!) {\n submissionDetail(submissionId: $id) {\n id\n code\n runtime\n memory\n rawMemory\n statusDisplay\n timestamp\n lang\n passedTestCaseCnt\n totalTestCaseCnt\n sourceUrl\n question {\n titleSlug\n title\n translatedTitle\n questionId\n __typename\n }\n ... on GeneralSubmissionNode {\n outputDetail {\n codeOutput\n expectedOutput\n input\n compileError\n runtimeError\n lastTestcase\n __typename\n }\n __typename\n }\n submissionComment {\n comment\n flagType\n __typename\n }\n __typename\n }\n}\n"
}
resp = self.session.post("https://leetcode.cn/graphql",
data=json.dumps(query_params).encode('utf8'),
headers={
"content-type": "application/json",
})
body = json.loads(resp.content)
# parse data
solution = get(body, "data.submissionDetail")
# Solution.replace(
# problem=solution['id'],
# url=f"https://leetcode.cn/articles/{slug}/",
# content=solution['code']
# ).execute()
Submission.insert(
id=solution['id'],
slug=slug,
language=solution['lang'],
created=solution['timestamp'],
source=solution['code']
).execute()
# random_wait(10, 15)
def fetch_questionSolutionArticles(self, slug):
print(f"🤖 Fetching solution for problem: {slug}")
query_params = {
"operationName":"questionSolutionArticles",
"variables":{
"questionSlug":slug,
"first":10,
"skip":0,
"orderBy":"DEFAULT"
},
"query":"query questionSolutionArticles($questionSlug: String!, $skip: Int, $first: Int, $orderBy: SolutionArticleOrderBy, $userInput: String, $tagSlugs: [String!]) {\n questionSolutionArticles(questionSlug: $questionSlug, skip: $skip, first: $first, orderBy: $orderBy, userInput: $userInput, tagSlugs: $tagSlugs) {\n totalNum\n edges {\n node {\n ...solutionArticle\n __typename\n }\n __typename\n }\n __typename\n }\n}\n\nfragment solutionArticle on SolutionArticleNode {\n rewardEnabled\n canEditReward\n uuid\n title\n slug\n sunk\n chargeType\n status\n identifier\n canEdit\n canSee\n reactionType\n reactionsV2 {\n count\n reactionType\n __typename\n }\n tags {\n name\n nameTranslated\n slug\n tagType\n __typename\n }\n createdAt\n thumbnail\n author {\n username\n profile {\n userAvatar\n userSlug\n realName\n __typename\n }\n __typename\n }\n summary\n topic {\n id\n commentCount\n viewCount\n __typename\n }\n byLeetcode\n isMyFavorite\n isMostPopular\n isEditorsPick\n hitCount\n videosInfo {\n videoId\n coverUrl\n duration\n __typename\n }\n __typename\n}\n"
}
resp = self.session.post("https://leetcode.cn/graphql",
data=json.dumps(query_params).encode('utf8'),
headers={
"content-type": "application/json",
})
body = json.loads(resp.content)
# parse data
edges = get(body, "data.questionSolutionArticles.edges")
if edges != None and len(edges) > 0:
for edge in edges:
# if Solution.get_or_none(Solution.problemid == edge['uuid']) is not None:
# continue
if edge!=None and edge["node"]!=None and edge["node"]["byLeetcode"] and edge["node"]["slug"]!=None:
return self.fetch_solutionDetailArticle(edge["node"]["slug"])
def fetch_solutionDetailArticle(self, slug):
query_params = {
"operationName":"solutionDetailArticle",
"variables":{
"slug":slug,
"orderBy":"DEFAULT"
},
"query":"query solutionDetailArticle($slug: String!, $orderBy: SolutionArticleOrderBy!) {\n solutionArticle(slug: $slug, orderBy: $orderBy) {\n ...solutionArticle\n content\n question {\n questionTitleSlug\n __typename\n }\n position\n next {\n slug\n title\n __typename\n }\n prev {\n slug\n title\n __typename\n }\n __typename\n }\n}\n\nfragment solutionArticle on SolutionArticleNode {\n rewardEnabled\n canEditReward\n uuid\n title\n slug\n sunk\n chargeType\n status\n identifier\n canEdit\n canSee\n reactionType\n reactionsV2 {\n count\n reactionType\n __typename\n }\n tags {\n name\n nameTranslated\n slug\n tagType\n __typename\n }\n createdAt\n thumbnail\n author {\n username\n profile {\n userAvatar\n userSlug\n realName\n __typename\n }\n __typename\n }\n summary\n topic {\n id\n commentCount\n viewCount\n __typename\n }\n byLeetcode\n isMyFavorite\n isMostPopular\n isEditorsPick\n hitCount\n videosInfo {\n videoId\n coverUrl\n duration\n __typename\n }\n __typename\n}\n"
}
resp = self.session.post("https://leetcode.cn/graphql",
data=json.dumps(query_params).encode('utf8'),
headers={
"content-type": "application/json",
})
body = json.loads(resp.content)
# parse data
solution = get(body, "data.solutionArticle")
if solution!=None:
questionTitleSlug = solution['question']["questionTitleSlug"]
Solution.replace(
problem=Problem.get(Problem.slug==questionTitleSlug),
url=f"https://leetcode.com/articles/{questionTitleSlug}/",
content=solution['content']
).execute()
def fetch_submission(self, slug):
print(f"🤖 Fetching submission for problem: {slug}")
query_params = {
'operationName': "Submissions",
'variables': {"offset": 0, "limit": 20, "lastKey": '', "questionSlug": slug},
'query': '''query Submissions($offset: Int!, $limit: Int!, $lastKey: String, $questionSlug: String!) {
submissionList(offset: $offset, limit: $limit, lastKey: $lastKey, questionSlug: $questionSlug) {
lastKey
hasNext
submissions {
id
statusDisplay
lang
runtime
timestamp
url
isPending
__typename
}
__typename
}
}'''
}
resp = self.session.post("https://leetcode.cn/graphql",
data=json.dumps(query_params).encode('utf8'),
headers={
"content-type": "application/json",
})
body = json.loads(resp.content)
# parse data
submissions = get(body, "data.submissionList.submissions")
if len(submissions) > 0:
for sub in submissions:
if Submission.get_or_none(Submission.id == sub['id']) is not None:
continue
if sub['statusDisplay'] == 'Accepted':
url = sub['url']
html = self.session.get(f'https://leetcode.cn{url}').text
pattern = re.compile(
r'submissionCode: \'(?P<code>.*)\',\n editCodeUrl', re.S
)
matched = pattern.search(html)
code = matched.groupdict().get('code') if matched else None
if code:
Submission.insert(
id=sub['id'],
slug=slug,
language=sub['lang'],
created=sub['timestamp'],
source=code.encode('utf-8')
).execute()
else:
raise Exception(f"Cannot get submission code for problem: {slug}")
random_wait(10, 15)
if __name__ == '__main__':
create_tables()
crawler = LeetCodeCrawler()
crawler.login()
crawler.fetch_accepted_problems()