-
Notifications
You must be signed in to change notification settings - Fork 0
/
videos.go
54 lines (42 loc) · 1016 Bytes
/
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
package ytube
import (
"context"
"github.com/comunidade-shallom/peristera/pkg/config"
"github.com/pterm/pterm"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
type Service struct {
youtube *youtube.Service
}
func NewService(ctx context.Context, cfg config.YouTube) (Service, error) {
yService, err := youtube.NewService(ctx, option.WithAPIKey(cfg.Token))
if err != nil {
return Service{}, err
}
return Service{
youtube: yService,
}, nil
}
func (s Service) LastVideos(ctx context.Context, channelID string, maxResults int) ([]Video, error) {
res, err := s.youtube.Search.
List([]string{"snippet"}).
MaxResults(int64(maxResults)).
Order("date").
ChannelId(channelID).
Context(ctx).
Do()
vids := []Video{}
if err != nil {
return vids, err
}
for _, item := range res.Items {
vid, err := FromSearchResult(item)
if err != nil {
pterm.Warning.Printfln("Parse error: %s", err.Error())
continue
}
vids = append(vids, vid)
}
return vids, err
}