-
Notifications
You must be signed in to change notification settings - Fork 4
/
youtube.go
77 lines (68 loc) · 2.36 KB
/
youtube.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
package youtube
import (
"context"
"fmt"
"strings"
"github.com/sugar-cat7/vspo-portal/service/cron/domain/model"
domain_youtube "github.com/sugar-cat7/vspo-portal/service/cron/domain/youtube"
"github.com/sugar-cat7/vspo-portal/service/cron/infra/youtube/internal/dto"
"github.com/sugar-cat7/vspo-portal/service/cron/pkg"
"google.golang.org/api/youtube/v3"
)
var _ domain_youtube.YoutubeClient = (*youtubeServiceImpl)(nil)
type youtubeServiceImpl struct {
service *youtube.Service
}
// NewService returns a new YoutubeClient.
func NewService(apiKey string) domain_youtube.YoutubeClient {
return &youtubeServiceImpl{
service: NewClient(apiKey),
}
}
// GetVideos returns a list of videos by their IDs.
func (y *youtubeServiceImpl) GetVideos(ctx context.Context, param domain_youtube.VideosParam) (model.Videos, error) {
var videos []*youtube.Video
videoIDChunks, err := pkg.Chunk(param.VideoIDs, 50)
if err != nil {
return nil, err
}
for _, chunk := range videoIDChunks {
call := y.service.Videos.List([]string{"snippet", "liveStreamingDetails", "statistics"}).Id(strings.Join(chunk, ","))
response, err := call.Do()
if err != nil {
return nil, fmt.Errorf("error making Videos.List call: %v", err)
}
videos = append(videos, response.Items...)
}
v, err := dto.YtVideosToVideos(videos)
if err != nil {
return nil, err
}
return v, nil
}
// Search returns a list of videos by their search query.
func (y *youtubeServiceImpl) SearchVideos(ctx context.Context, param domain_youtube.SearchVideosParam) (model.Videos, error) {
call := y.service.Search.List([]string{"snippet"}).Q(param.SearchQuery.String()).Type("video").MaxResults(50).EventType(param.EventType.String())
response, err := call.Do()
if err != nil {
return nil, fmt.Errorf("error making Search.List call: %v", err)
}
v, err := dto.YtSearchResultToVideos(response)
if err != nil {
return nil, err
}
return v, nil
}
// Channels returns a list of channels by their IDs.
func (y *youtubeServiceImpl) Channels(ctx context.Context, param domain_youtube.ChannelsParam) (model.Channels, error) {
call := y.service.Channels.List([]string{"snippet"}).Id(strings.Join(param.ChannelIDs, ","))
response, err := call.Do()
if err != nil {
return nil, fmt.Errorf("error making Channels.List call: %v", err)
}
c, err := dto.YtChannelsToChannels(response)
if err != nil {
return nil, err
}
return c, nil
}