forked from halfrost/LeetCode-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
110 lines (95 loc) · 3.02 KB
/
request.go
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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"github.com/mozillazg/request"
)
const (
// AllProblemURL define
AllProblemURL = "https://leetcode.com/api/problems/all/"
// QraphqlURL define
QraphqlURL = "https://leetcode.com/graphql"
// LoginPageURL define
LoginPageURL = "https://leetcode.com/accounts/login/"
// AlgorithmsURL define
AlgorithmsURL = "https://leetcode.com/api/problems/Algorithms/"
// ArrayProblemURL define
ArrayProblemURL = "https://leetcode.com/tag/array/"
)
var req *request.Request
func newReq() *request.Request {
if req == nil {
req = signin()
}
return req
}
func signin() *request.Request {
cfg := getConfig()
req := request.NewRequest(new(http.Client))
req.Headers = map[string]string{
"Content-Type": "application/json",
"Accept-Encoding": "",
"cookie": cfg.Cookie,
"x-csrftoken": cfg.CSRFtoken,
"Referer": "https://leetcode.com/accounts/login/",
"origin": "https://leetcode.com",
}
return req
}
func getRaw(URL string) []byte {
req := newReq()
resp, err := req.Get(URL)
if err != nil {
fmt.Printf("getRaw: Get Error: " + err.Error())
return []byte{}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("getRaw: Read Error: " + err.Error())
return []byte{}
}
if resp.StatusCode == 200 {
fmt.Println("Get problem Success!")
}
return body
}
func getProblemAllList() []byte {
return getRaw(AllProblemURL)
}
// Variables define
type Variables struct {
slug string
}
func getQraphql(payload string) []byte {
req := newReq()
resp, err := req.PostForm(QraphqlURL, bytes.NewBuffer([]byte(payload)))
if err != nil {
fmt.Printf("getRaw: Get Error: " + err.Error())
return []byte{}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("getRaw: Read Error: " + err.Error())
return []byte{}
}
if resp.StatusCode == 200 {
fmt.Println("Get problem Success!")
}
return body
}
func getTopicTag(variable string) string {
return fmt.Sprintf(`{
"operationName": "getTopicTag",
"variables": {
"slug": "%s"
},
"query": "query getTopicTag($slug: String!) { topicTag(slug: $slug) { name translatedName slug questions { status questionId questionFrontendId title titleSlug translatedTitle stats difficulty isPaidOnly topicTags { name translatedName slug __typename } companyTags { name translatedName slug __typename } __typename } frequencies __typename } favoritesLists { publicFavorites { ...favoriteFields __typename } privateFavorites { ...favoriteFields __typename } __typename }}fragment favoriteFields on FavoriteNode { idHash id name isPublicFavorite viewCount creator isWatched questions { questionId title titleSlug __typename } __typename}"
}`, variable)
}
func getTagProblemList(tag string) []byte {
return getQraphql(getTopicTag(tag))
}