-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLeetcodeCrawler.py
30 lines (26 loc) · 1.66 KB
/
LeetcodeCrawler.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
import requests
def fetch_daily_problem():
data = {"query": "query questionOfToday { activeDailyCodingChallengeQuestion { date link question { acRate difficulty frontendQuestionId: questionFrontendId title } } }"}
response = requests.post("https://leetcode.com/graphql/",
json=data).json()["data"]["activeDailyCodingChallengeQuestion"]
result = ("%s\n%s. %s (%s, AC %.2f%%)\nhttps://leetcode.com%s"%(response["date"],
response["question"]["frontendQuestionId"],
response["question"]["title"],
response["question"]["difficulty"],
response["question"]["acRate"],
response["link"]))
return result
def fetch_random_problem():
# get title
data = {"query":"query randomQuestion($categorySlug: String, $filters: QuestionListFilterInput) { randomQuestion(categorySlug: $categorySlug, filters: $filters) { titleSlug } }","variables":{"categorySlug":"","filters":{}}}
response = requests.post("https://leetcode.com/graphql/",json=data).json()
problem = response["data"]["randomQuestion"]["titleSlug"]
# get info
data = {"operationName":"questionData","variables":{"titleSlug":problem},"query":"query questionData($titleSlug: String!) { question(titleSlug: $titleSlug) { questionFrontendId title titleSlug isPaidOnly difficulty acRate}}"}
response = requests.post("https://leetcode.com/graphql/",json=data).json()["data"]
result = ("%s. %s (%s, AC %.2f%%)\nhttps://leetcode.com/problems/%s"%(response["question"]["questionFrontendId"],
response["question"]["title"],
response["question"]["difficulty"],
response["question"]["acRate"],
response["question"]["titleSlug"]))
return result