forked from Davincible/gotiktoklive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.go
186 lines (160 loc) · 3.39 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
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
}
func (t *TikTok) sendRequest(o *reqOptions) (body []byte, 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
}
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 {
signed, err := t.signURL(fullUrl)
if err != nil {
return nil, err
}
ua = signed.UserAgent
fullUrl = signed.SignedURL
}
var req *http.Request
req, err = http.NewRequest(method, fullUrl, reqData)
if err != nil {
return
}
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
}
defer resp.Body.Close()
var bb bytes.Buffer
_, err = io.Copy(&bb, resp.Body)
if err != nil {
return
}
body = bb.Bytes()
if resp.StatusCode == 429 {
err = ErrRateLimitExceeded
return
} else if resp.StatusCode >= 400 {
err = fmt.Errorf("received status code %d", resp.StatusCode)
return
}
// Decode gzip encoded responses
encoding := resp.Header.Get("Content-Encoding")
if encoding != "" && encoding == "gzip" {
buf := bytes.NewBuffer(body)
zr, err := gzip.NewReader(buf)
if err != nil {
return nil, err
}
var bb bytes.Buffer
_, err = io.Copy(&bb, zr)
body = bb.Bytes()
if err != nil {
return body, err
}
if err := zr.Close(); err != nil {
return body, err
}
}
// Log complete response body
if t.LogRequests {
r := map[string]interface{}{
"status": resp.StatusCode,
"endpoint": o.Endpoint,
"body": string(body),
}
b, err := json.MarshalIndent(r, "", " ")
if err != nil {
return nil, err
}
t.debugHandler(string(b))
}
return
}