-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube_api.go
108 lines (90 loc) · 2.95 KB
/
youtube_api.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 main
import (
"context"
"fmt"
"github.com/moezakura/youlive-capture/utils"
"golang.org/x/xerrors"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
"time"
)
type YoutubeAPI struct {
apiKey string
}
func NewYoutubeAPI(apiKey string) *YoutubeAPI {
y := new(YoutubeAPI)
y.apiKey = apiKey
return y
}
func (y *YoutubeAPI) NewYoutubeService(ctx context.Context) (*youtube.Service, error) {
service, err := youtube.NewService(ctx, option.WithAPIKey(y.apiKey))
if err != nil {
return nil, xerrors.Errorf("YoutubeAPI.NewYoutubeService youtube.NewService error: %w", err)
}
return service, nil
}
func (y *YoutubeAPI) GetLiveTime(ctx context.Context, channelID string) (time.Time, string, error) {
service, err := y.NewYoutubeService(ctx)
if err != nil {
return utils.GetZeroTime(), "", xerrors.Errorf("YoutubeAPI.GetLiveTime y.NewYoutubeService error: %w", err)
}
searchService := youtube.NewSearchService(service)
req := searchService.List([]string{"snippet"}).
ChannelId(channelID).
MaxResults(3).
Order("date")
res, err := req.Do()
if err != nil {
return utils.GetZeroTime(), "", xerrors.Errorf("YoutubeAPI.GetLiveTime req.Do error: %w", err)
}
if len(res.Items) == 0 {
return utils.GetZeroTime(), "", nil
}
for _, item := range res.Items {
videoID := item.Id.VideoId
// upcoming 配信前, none 配信後/動画, live 配信中
if item.Snippet.LiveBroadcastContent == "none" {
continue
}
if item.Snippet.LiveBroadcastContent == "live" {
return time.Now(), videoID, nil
}
if item.Snippet.LiveBroadcastContent == "upcoming" {
startTime, err := y.GetLiveStartTime(service, videoID)
if err != nil {
return utils.GetZeroTime(), "",
xerrors.Errorf("YoutubeAPI.GetLiveTime y.GetLiveStartTime error: %w", err)
}
return startTime, videoID, nil
}
}
return utils.GetZeroTime(), "", nil
}
func (y *YoutubeAPI) GetLiveStartTime(service *youtube.Service, videoID string) (time.Time, error) {
liveInfo, err := y.getLiveInfo(service, videoID)
if err != nil {
return utils.GetZeroTime(), xerrors.Errorf("YoutubeAPI.GetLiveStartTime y.getLiveInfo error: %w", err)
}
if len(liveInfo.Items) == 0 {
return utils.GetZeroTime(), nil
}
liveInfoItem := liveInfo.Items[0]
startTimeStr := liveInfoItem.LiveStreamingDetails.ScheduledStartTime
startTime, err := time.Parse("2006-01-02T15:04:05Z(MST)", fmt.Sprintf("%s(UTC)", startTimeStr))
if err != nil {
return utils.GetZeroTime(), xerrors.Errorf("YoutubeAPI.GetLiveStartTime time.Parse(%s) error: %w",
startTimeStr, err)
}
return startTime.In(time.UTC), nil
}
func (y *YoutubeAPI) getLiveInfo(service *youtube.Service, videoID string) (*youtube.VideoListResponse, error) {
listService := youtube.NewVideosService(service)
req := listService.List([]string{"liveStreamingDetails"}).
Id(videoID).
MaxResults(1)
res, err := req.Do()
if err != nil {
return nil, xerrors.Errorf("YoutubeAPI.getLiveInfo req.Do error: %w", err)
}
return res, nil
}