-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfeed.go
72 lines (60 loc) · 1.52 KB
/
feed.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
package gotiktoklive
import (
"encoding/json"
"strconv"
)
// Feed allows you to fetch reccomended livestreams.
type Feed struct {
t *TikTok
// All collected reccomended livestreams
LiveStreams []*LiveStream
HasMore bool
maxTime int64
}
// NewFeed creates a new Feed instance. Start fetching recommended livestreams
// with Feed.Next().
func (t *TikTok) NewFeed() *Feed {
return &Feed{
t: t,
LiveStreams: []*LiveStream{},
HasMore: true,
}
}
// Next fetches the next couple of recommended live streams, if available.
// You can call this as long as Feed.HasMore = true. All items will be added
//
// to the Feed.LiveStreams list.
func (f *Feed) Next() (*FeedItem, error) {
if !f.HasMore {
return nil, ErrNoMoreFeedItems
}
params := copyMap(defaultGETParams)
params["channel"] = "tiktok_web"
params["channel_id"] = "86"
if f.maxTime != 0 {
params["max_time"] = strconv.FormatInt(f.maxTime, 10)
}
body, _, err := f.t.sendRequest(&reqOptions{
Endpoint: urlFeed,
Query: params,
}, nil)
if err != nil {
return nil, err
}
var rsp FeedItem
if err := json.Unmarshal(body, &rsp); err != nil {
return nil, err
}
f.HasMore = rsp.Extra.HasMore
f.maxTime = rsp.Extra.MaxTime
for _, s := range rsp.LiveStreams {
s.t = f.t
}
return &rsp, nil
}
// Track stars tracking the livestream obtained from the Feed, and returns
// a Live instance, just as if you would start tracking the user with
// tiktok.TrackUser(<user>).
func (s *LiveStream) Track() (*Live, error) {
return s.t.TrackRoom(s.Rid)
}