MCPcopy
hub / github.com/doocs/leetcode / Spider

Class Spider

solution/main.py:30–275  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28
29
30class Spider:
31 def __init__(self, cookie1: str, cookie2: str):
32 self.cookie_cn = cookie1
33 self.cookie_en = cookie2
34
35 def get_all_questions(self, retry: int = 3) -> List:
36 """获取所有题目"""
37 headers = {
38 "accept": "application/json, text/javascript, */*; q=0.01",
39 "content-type": "application/json",
40 "user-agent": user_agent,
41 "x-requested-with": "XMLHttpRequest",
42 "cookie": self.cookie_en,
43 }
44 try:
45 resp = requests.get(
46 url="https://leetcode.com/api/problems/all/",
47 headers=headers,
48 allow_redirects=False,
49 timeout=10,
50 verify=False,
51 )
52 return resp.json()["stat_status_pairs"]
53 except Exception as e:
54 print("get_all_questions", e)
55 time.sleep(2)
56 return self.get_all_questions(retry - 1) if retry > 0 else []
57
58 def get_all_questions_v2(self, retry: int = 3, limit: int = 10000) -> List:
59 headers = {
60 "Cookie": self.cookie_en,
61 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0",
62 "Content-Type": "application/json",
63 }
64 form = {
65 "query": "\n query problemsetQuestionList($categorySlug: String, $limit: Int, $skip: Int, $filters: QuestionListFilterInput) {\n problemsetQuestionList: questionList(\n categorySlug: $categorySlug\n limit: $limit\n skip: $skip\n filters: $filters\n ) {\n total: totalNum\n questions: data {\n acRate\n difficulty\n freqBar\n frontendQuestionId: questionFrontendId\n isFavor\n paidOnly: isPaidOnly\n status\n title\n titleSlug\n topicTags {\n name\n id\n slug\n }\n hasSolution\n hasVideoSolution\n }\n }\n}\n ",
66 "variables": {
67 "categorySlug": "all-code-essentials",
68 "skip": 0,
69 "limit": limit,
70 "filters": {"orderBy": "FRONTEND_ID", "sortOrder": "DESCENDING"},
71 },
72 "operationName": "problemsetQuestionList",
73 }
74 try:
75 resp = requests.post(
76 "https://leetcode.com/graphql",
77 headers=headers,
78 data=json.dumps(form),
79 timeout=20,
80 )
81 return resp.json()["data"]["problemsetQuestionList"]["questions"]
82 except Exception as e:
83 print("get_all_questions_v2", e)
84 time.sleep(2)
85 return self.get_all_questions_v2(retry - 1, limit) if retry > 0 else []
86
87 def get_question_detail_en(self, question_title_slug: str, retry: int = 3) -> dict:

Callers 1

runFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected