-
Notifications
You must be signed in to change notification settings - Fork 7
/
leech.py
90 lines (62 loc) · 1.87 KB
/
leech.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
import requests
import json
# Fill in these details to be able to send a request, more details in README
HEADERS = {
'x-csrftoken': '<ENTER YOUR CSRF TOKEN',
'Referer': 'https://leetcode.com/problems',
'Cookie': '<ENTER YOUR COOKIE ID>'
}
graphql_url = "https://leetcode.com/graphql"
final_data = {}
# Get a list of all questions on Leetcode,
# we will query details for these question
def get_question_list():
response = requests.get("https://leetcode.com/api/problems/all")
raw_data = response.json()
stat_status_pairs = raw_data["stat_status_pairs"]
question_title_slug = [ i["stat"]["question__title_slug"] for i in stat_status_pairs ]
return question_title_slug
# Post a GraphQL request for a given question,
# Query has the description of all the details we want,
# Query can be modified according to needs
def leech( question ):
query = ("""
query questionData {
question(titleSlug:"%s")
{
questionId
title
titleSlug
content
isPaidOnly
difficulty
likes
dislikes
topicTags {
name
slug
}
stats
}
}
"""% question )
r = requests.post(
graphql_url,
json = {
'query': query
},
headers = HEADERS,
)
r = r.json()
r = r['data']['question']
r['url'] = "https://leetcode.com/problems/" + str(r['titleSlug'])
final_data[r['questionId']] = r
question_list = get_question_list()
# Call the leech function for all questions,
# Prints progress
for i, question in enumerate( question_list ):
leech( question )
print( f"Fetched {i+1}/{len(question_list)} questions" )
# Dump our Python dictionary to a JSON file
with open("data.json", "w") as file:
json.dump( final_data, file )