-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetadata.go
46 lines (34 loc) · 1.2 KB
/
metadata.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
package main
import (
"context"
"fmt"
"os"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
func GetVideoMetadata(videoId string) (*youtube.Video, error) {
service, err := youtube.NewService(context.Background(), option.WithAPIKey(os.Getenv("GOOGLE_API_KEY")))
if err != nil {
return nil, err
}
videoService := youtube.NewVideosService(service)
list, err := videoService.List([]string{"contentDetails", "liveStreamingDetails", "snippet"}).Id(videoId).Do()
if err != nil {
return nil, err
}
if length := len(list.Items); length != 1 {
return nil, fmt.Errorf("didn't get items, length was %d", length)
}
return list.Items[0], nil
}
func IsLivestream(video *youtube.Video) bool {
return video != nil && video.LiveStreamingDetails != nil && video.Snippet.LiveBroadcastContent != "none"
}
// IsLivestreamStarted checks if the livestream is currently live
func IsLivestreamStarted(video *youtube.Video) bool {
return IsLivestream(video) && video.Snippet.LiveBroadcastContent == "live"
}
// IsLivestreamEnded checks if the livestream has ended
func IsLivestreamEnded(video *youtube.Video) bool {
return IsLivestream(video) && video.Snippet.LiveBroadcastContent == "completed"
}