-
Notifications
You must be signed in to change notification settings - Fork 27
/
tiktok.go
213 lines (177 loc) · 4.53 KB
/
tiktok.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package gotiktoklive
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"net/http/cookiejar"
neturl "net/url"
"os"
"os/signal"
"sync"
"syscall"
)
// TikTok allows you to track and discover current live streams.
type TikTok struct {
c *http.Client
wg *sync.WaitGroup
done func() <-chan struct{}
streams int
mu *sync.Mutex
// Pass extra debug messages to debugHandler
Debug bool
// LogRequests when set to true will log all made requests in JSON to debugHandler
LogRequests bool
infoHandler func(...interface{})
warnHandler func(...interface{})
debugHandler func(...interface{})
errHandler func(...interface{})
proxy *neturl.URL
}
// NewTikTok creates a tiktok instance that allows you to track live streams and
// discover current livestreams.
func NewTikTok() *TikTok {
jar, _ := cookiejar.New(nil)
wg := sync.WaitGroup{}
ctx, cancel := context.WithCancel(context.Background())
tiktok := TikTok{
c: &http.Client{Jar: jar},
wg: &wg,
done: ctx.Done,
mu: &sync.Mutex{},
infoHandler: defaultLogHandler,
warnHandler: defaultLogHandler,
debugHandler: defaultLogHandler,
errHandler: routineErrHandler,
}
envs := []string{"HTTP_PROXY", "HTTPS_PROXY"}
for _, env := range envs {
if e := os.Getenv(env); e != "" {
tiktok.SetProxy(e, false)
}
}
setupInterruptHandler(
func(c chan os.Signal) {
<-c
cancel()
wg.Wait()
tiktok.infoHandler("Shutting down...")
os.Exit(0)
})
tiktok.sendRequest(&reqOptions{
OmitAPI: true,
})
return &tiktok
}
// GetUserInfo will fetch information about the user, such as follwers stats,
// their user ID, as well as the RoomID, with which you can tell if they are live.
func (t *TikTok) GetUserInfo(user string) (*UserInfo, error) {
body, err := t.sendRequest(&reqOptions{
Endpoint: fmt.Sprintf(urlUser, user),
OmitAPI: true,
})
if err != nil {
return nil, err
}
// if len(matches) != 0 {
// return nil, ErrCaptcha
// }
// Find json data in HTML page
var matches [][]byte
for _, re := range reJsonData {
matches = re.FindSubmatch(body)
if len(matches) != 0 {
break
}
}
if len(matches) == 0 {
return nil, ErrIPBlocked
}
// Parse json data
var res struct {
UserModule struct {
Users map[string]*UserInfo `json:"users"`
Stats map[string]UserStats `json:"stats"`
} `json:"UserModule"`
}
if err := json.Unmarshal(matches[1], &res); err != nil {
return nil, err
}
if len(res.UserModule.Users) == 0 {
return nil, ErrUserNotFound
}
if res.UserModule.Users == nil {
return nil, ErrUserInfoNotFound
}
userInfo, ok := res.UserModule.Users[user]
if !ok {
return nil, ErrUserInfoNotFound
}
if res.UserModule.Stats == nil {
stats, ok := res.UserModule.Stats[user]
if ok {
userInfo.Stats = stats
}
}
return userInfo, nil
}
// GetPriceList fetches the price list of tiktok coins. Prices will be given in
// USD cents and the cents equivalent of the local currency of the IP location.
// To fetch a different currency, use a VPN or proxy to change your IP to a
// different country.
func (t *TikTok) GetPriceList() (*PriceList, error) {
body, err := t.sendRequest(&reqOptions{
Endpoint: urlPriceList,
Query: defaultGETParams,
})
if err != nil {
return nil, err
}
var rsp PriceList
if err := json.Unmarshal(body, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}
func (t *TikTok) SetInfoHandler(f func(...interface{})) {
t.infoHandler = f
}
func (t *TikTok) SetWarnHandler(f func(...interface{})) {
t.warnHandler = f
}
func (t *TikTok) SetDebugHandler(f func(...interface{})) {
t.debugHandler = f
}
func (t *TikTok) SetErrorHandler(f func(...interface{})) {
t.errHandler = f
}
// SetProxy will set a proxy for both the http client as well as the websocket.
// You can manually set a proxy with this method, or by using the HTTPS_PROXY
// environment variable.
// ALL_PROXY can be used to set a proxy only for the websocket.
func (t *TikTok) SetProxy(url string, insecure bool) error {
uri, err := neturl.Parse(url)
if err != nil {
return err
}
t.proxy = uri
tr := http.DefaultTransport.(*http.Transport).Clone()
tr.TLSClientConfig = &tls.Config{
InsecureSkipVerify: insecure,
}
tr.Proxy = http.ProxyURL(uri)
t.c.Transport = tr
// t.c.Transport = &http.Transport{
// Proxy: http.ProxyURL(uri),
// TLSClientConfig: &tls.Config{
// InsecureSkipVerify: insecure,
// },
// }
return nil
}
func setupInterruptHandler(f func(chan os.Signal)) {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go f(c)
}