-
Notifications
You must be signed in to change notification settings - Fork 43
/
quake360.go
177 lines (150 loc) · 4.2 KB
/
quake360.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
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
package utils
import (
"bytes"
"crypto/tls"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/tidwall/gjson"
"github.com/yaklang/yaklang/common/log"
)
func newDefaultClient() *http.Client {
return &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
MinVersion: tls.VersionSSL30, // nolint[:staticcheck]
MaxVersion: tls.VersionTLS13,
},
DisableKeepAlives: true,
DisableCompression: true,
MaxConnsPerHost: 50,
Proxy: nil,
},
Timeout: 15 * time.Second,
}
}
type Quake360Client struct {
key string
currentPaginationId string
currentQuery string
client *http.Client
}
func NewQuake360Client(apiKey string) *Quake360Client {
return &Quake360Client{key: apiKey, client: newDefaultClient()}
}
const quakeAPI = "https://quake.360.cn/api/v3/scroll/quake_service"
const quakeUserAPI = "https://quake.360.cn/api/v3/user/info"
type quakeQueryParam struct {
Query string `json:"query"`
Start int `json:"start"`
Size int `json:"size"`
}
type quakeUserInfo struct {
Id string `json:"id"`
IsBaned bool `json:"baned"`
// 当月剩余
MonthRemainingCredit int `json:"month_remaining_credit"`
TotalCredit int `json:"total_credit"`
ConstantCredit int `json:"constant_credit"`
BanStatus string `json:"ban_status"`
}
func (q *Quake360Client) UserInfo() (*quakeUserInfo, error) {
req, err := http.NewRequest("GET", quakeUserAPI, http.NoBody)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-QuakeToken", q.key)
req.Header.Set("User-Agent", "curl/7.64.1")
Debug(func() {
reqRaw, err := DumpHTTPRequest(req, true)
if err != nil {
return
}
log.Infof("req: \n%s", string(reqRaw))
})
rsp, err := q.client.Do(req)
if err != nil {
return nil, err
}
rspBody, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return nil, err
}
result := gjson.ParseBytes(rspBody)
Debug(func() {
log.Infof("results: \n%v", string(rspBody))
})
code := result.Get("code")
if !code.Exists() || code.Int() != 0 {
return nil, Errorf("quake error: %s", result.Get("message").String())
}
data := result.Get("data")
user := &quakeUserInfo{
Id: data.Get("id").String(),
IsBaned: data.Get("baned").Bool(),
MonthRemainingCredit: int(data.Get("month_remaining_credit").Int()),
TotalCredit: int(data.Get("total_credit").Int()),
ConstantCredit: int(data.Get("constant_credit").Int()),
BanStatus: data.Get("ban_status").String(),
}
return user, nil
}
func (q *Quake360Client) QueryNext(start, size int, queries ...string) (*gjson.Result, error) {
query := strings.Join(queries, " ")
if query != "" && q.currentQuery != "" && q.currentQuery != query {
return nil, Errorf("query empty or query changed from initd query")
}
if query == "" {
return nil, Errorf("empty query")
}
q.currentQuery = query
raw, err := json.Marshal(quakeQueryParam{
Query: query,
Start: start,
Size: size,
})
req, err := http.NewRequest("POST", quakeAPI, bytes.NewBuffer(raw))
if err != nil {
return nil, Errorf("create quake request failed: %s", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-QuakeToken", q.key)
req.Header.Set("User-Agent", "curl/7.64.1")
Debug(func() {
reqRaw, err := DumpHTTPRequest(req, true)
if err != nil {
return
}
log.Infof("req: \n%s", string(reqRaw))
})
rsp, err := q.client.Do(req)
if err != nil {
return nil, Errorf("quake request from http client failed: %s", err)
}
if rsp.Body == nil {
return nil, Errorf("empty result")
}
rspBody, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return nil, Errorf("empty result")
}
rspStr := string(rspBody)
result := gjson.Parse(rspStr)
Debug(func() {
log.Infof("results: \n%v", rspStr)
})
code := result.Get("code")
if !code.Exists() || code.Int() != 0 {
return nil, Errorf("quake error: %s", result.Get("message").String())
}
q.currentQuery = query
dataArray := result.Get("data").Array()
if len(dataArray) <= 0 {
return nil, Errorf("empty services / results")
}
return &result, nil
}