-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideos.go
108 lines (89 loc) · 2.49 KB
/
videos.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
package twch
import (
"fmt"
)
type Videos struct {
client *Client
}
type videosResponse struct {
Videos []Video `json:"videos"`
*listLinks
}
type Video struct {
ID *string `json:"_id"`
Title *string `json:"title"`
URL *string `json:"url"`
Views *int `json:"views"`
Description *string `json:"description"`
Length *int `json:"length"`
Game *string `json:"game"`
Preview *string `json:"preview"`
RecordedAt *string `json:"recorded_at"`
Channel *Channel `json:"channel"`
}
type VideoRequestOptions struct {
Game string `url:"game,omitempty"`
Period string `url:"period,omitempty"`
ListOptions
}
type VideoChannelOptions struct {
Broadcasts bool `url:"broadcasts,omitempty"`
ListOptions
}
// GetVideo returns a video object via its ID
func (v *Videos) GetVideo(id int) (video *Video, resp *Response, err error) {
url := fmt.Sprintf("videos/%d", id)
req, err := v.client.NewRequest("GET", url)
if err != nil {
return
}
video = new(Video)
resp, err = v.client.Do(req, video)
if err != nil {
return
}
return
}
// ListTop returns a list of the top videos on twitch for the specified period of time,
// ordered by most popular first. Defined time periods are "week", "month", or "all".
// By default, the top videos of the "week" are returned.
// Videos belonging to a specific game can be returned by passing the name of the game in the
// `Game` VideoRequestOption value. Otherwise, all games will be included in the result.
func (v *Videos) ListTop(opts *VideoRequestOptions) (videos []Video, resp *Response, err error) {
url, err := appendOptions("videos/top", opts)
if err != nil {
return
}
req, err := v.client.NewRequest("GET", url)
if err != nil {
return
}
r := new(videosResponse)
resp, err = v.client.Do(req, r)
if err != nil {
return
}
videos = r.Videos
return
}
// ListChannelVideos returns videos belonging to the target channel.
// Only broadcasts will be returned when the `VideoChannelOptions.Broadcasts` field
// is true. Otherwise only highlights are returned by default.
func (v *Videos) ListChannelVideos(channel string, opts *VideoChannelOptions) (videos []Video, resp *Response, err error) {
url := fmt.Sprintf("channels/%s/videos", channel)
url, err = appendOptions(url, opts)
if err != nil {
return
}
req, err := v.client.NewRequest("GET", url)
if err != nil {
return
}
r := new(videosResponse)
resp, err = v.client.Do(req, r)
if err != nil {
return
}
videos = r.Videos
return
}