-
Notifications
You must be signed in to change notification settings - Fork 20
/
dataapi.go
95 lines (86 loc) · 2.46 KB
/
dataapi.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
package service
import (
"context"
"errors"
"regexp"
"github.com/snowie2000/livetv/global"
"github.com/sosodev/duration"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
func getYoutubeService() (*youtube.Service, error) {
apiKey, err := global.GetConfig("apiKey")
if err != nil {
return nil, err
}
if apiKey == "" {
return nil, errors.New("API not set")
}
ctx := context.Background()
return youtube.NewService(ctx, option.WithAPIKey(apiKey))
}
// regex from https://stackoverflow.com/questions/5830387/how-do-i-find-all-youtube-video-ids-in-a-string-using-a-regex?lq=1
func GetYouTubeVideoID(url string) string {
regex := regexp.MustCompile(`(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*?[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|<\/a>))[?=&+%\w.-]*`)
match := regex.FindStringSubmatch(url)
if match != nil && len(match) > 0 {
return match[1]
}
return ""
}
func GetYouTubeChannelID(url string) string {
regex := regexp.MustCompile(`youtu((\.be)|(be\..{2,5}))\/((user)|(channel)|(c)|(@))\/?([a-zA-Z0-9\-_]{1,})`)
match := regex.FindStringSubmatch(url)
if match != nil && len(match) > 9 {
return match[9]
}
return ""
}
/* not possible right now
func GetManifestHLS(vid string) (string, error) {
service, err := getYoutubeService()
if err != nil {
return "", err
}
response, err := service.Videos.List([]string{"contentDetails"}).Id(vid).Do()
if err!=nil {
return "", err
}
if len(response.Items) >0 {
return response.Items[0].ContentDetails
}
}
*/
func GetChannelLiveStream(channelName string) (string, error) {
service, err := getYoutubeService()
if err != nil {
return "", err
}
response, err := service.Search.List([]string{"snippet"}).ChannelId(channelName).EventType("live").Type("video").Do()
if err != nil {
return "", err
}
if len(response.Items) > 0 {
return response.Items[0].Id.VideoId, nil
}
return "", errors.New("This channel is not currently live")
}
func GetVideoDuration(url string) (float64, error) {
vid := GetYouTubeVideoID(url)
if vid == "" {
return 0, errors.New("not a valid video url")
}
service, err := getYoutubeService()
if err != nil {
return 0, err
}
// Call the videos.list method with the video ID
resp, err := service.Videos.List([]string{"contentDetails"}).Id(vid).Do()
if err == nil && len(resp.Items) > 0 {
d, err := duration.Parse(resp.Items[0].ContentDetails.Duration)
if err == nil {
return d.ToTimeDuration().Seconds(), nil
}
}
return 0, err
}