-
Notifications
You must be signed in to change notification settings - Fork 2
/
requests.go
194 lines (168 loc) · 3.82 KB
/
requests.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package gotiktoklive
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
type reqOptions struct {
// Endpoint is the request path of tiktok api
Endpoint string
// IsPost set to true will send request with POST method.
//
// By default this option is false.
IsPost bool
// Compress post form data with gzip
Gzip bool
// Query is the parameters of the request
//
// This parameters are independents of the request method (POST|GET)
Query map[string]string
// List of headers to ignore
IgnoreHeaders []string
// Extra headers to add
ExtraHeaders map[string]string
// Use base tiktok URi instead of webcast api
OmitAPI bool
// Specifiy base URI
URI string
ExtraTikTokCookies string
}
func (t *TikTok) sendRequest(o *reqOptions, customValidate func(response *http.Response) error) ([]byte, http.Header, error) {
var err error
defer func() {
if err != nil {
err = fmt.Errorf("Failed to send request to %s: %w", o.Endpoint, err)
}
}()
method := "GET"
if o.IsPost {
method = "POST"
}
uri := tiktokAPIUrl
if o.OmitAPI {
uri = tiktokBaseUrl
}
if o.URI != "" {
uri = o.URI
}
uri = uri + o.Endpoint
u, err := url.Parse(uri)
if err != nil {
return nil, nil, err
}
vs := url.Values{}
for k, v := range o.Query {
if v != "" {
vs.Add(k, v)
}
}
reqData := bytes.NewBuffer([]byte{})
if o.IsPost {
reqData.WriteString(vs.Encode())
} else {
u.RawQuery = vs.Encode()
}
ua := userAgent
fullUrl := u.String()
if !o.OmitAPI && o.URI == "" && o.Endpoint == urlRoomData {
t.debugHandler("signing for url ", fullUrl)
return t.signURL(fullUrl, o)
}
var req *http.Request
req, err = http.NewRequest(method, fullUrl, reqData)
if err != nil {
return nil, nil, err
}
ignoreHeader := func(h string) bool {
for _, k := range o.IgnoreHeaders {
if k == h {
return true
}
}
return false
}
setHeaders := func(h map[string]string) {
for k, v := range h {
if v != "" && !ignoreHeader(k) {
req.Header.Set(k, v)
}
}
}
headers := map[string]string{
// "Connection": "keep-alive",
"Connection": "close",
"Cache-Control": "max-age=0",
"User-Agent": ua,
"Accept": "text/html,application/json,application/protobuf",
"Referer": referer,
"Origin": origin,
"Accept-Language": "en-US,en;q=0.9",
"Accept-Encoding": "gzip, deflate",
}
setHeaders(headers)
setHeaders(o.ExtraHeaders)
resp, err := t.c.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
var bb bytes.Buffer
_, err = io.Copy(&bb, resp.Body)
if err != nil {
return nil, nil, err
}
body := bb.Bytes()
if resp.StatusCode == 429 {
err = ErrRateLimitExceeded
return body, nil, err
} else if resp.StatusCode >= 400 {
if resp.StatusCode == 403 {
return nil, nil, &ErrIPBlockedOrBanned{}
}
err = fmt.Errorf("received status code %d", resp.StatusCode)
return body, nil, err
}
if customValidate != nil {
if err = customValidate(resp); err != nil {
return body, nil, err
}
}
// Decode gzip encoded responses
encoding := resp.Header.Get("Content-Encoding")
if encoding == "gzip" {
buf := bytes.NewBuffer(body)
zr, err := gzip.NewReader(buf)
if err != nil {
return nil, nil, err
}
var bb bytes.Buffer
_, err = io.Copy(&bb, zr)
body = bb.Bytes()
if err != nil {
return body, nil, err
}
if err := zr.Close(); err != nil {
return body, nil, err
}
}
ttCookie := resp.Header.Get("X-Set-TT-Cookie")
// Log complete response body
if t.LogRequests {
r := map[string]interface{}{
"status": resp.StatusCode,
"endpoint": o.Endpoint,
"body": string(body),
"tts cookie": ttCookie,
}
b, err := json.MarshalIndent(r, "", " ")
if err != nil {
return nil, nil, err
}
t.debugHandler(string(b))
}
return body, resp.Header, nil
}