@@ -25,6 +25,14 @@ type youtubeVideoTooltipData struct {
25
25
AgeRestricted bool
26
26
}
27
27
28
+ type youtubeStreamTooltipData struct {
29
+ Title string
30
+ ChannelTitle string
31
+ Uptime string
32
+ Viewers string
33
+ LikeCount string
34
+ }
35
+
28
36
type VideoLoader struct {
29
37
youtubeClient * youtubeAPI.Service
30
38
}
@@ -35,11 +43,13 @@ func (r *VideoLoader) Load(ctx context.Context, videoID string, req *http.Reques
35
43
"statistics" ,
36
44
"snippet" ,
37
45
"contentDetails" ,
46
+ "liveStreamingDetails" ,
38
47
}
39
48
40
49
log .Debugw ("[YouTube] Get video" ,
41
50
"videoID" , videoID ,
42
51
)
52
+
43
53
youtubeResponse , err := r .youtubeClient .Videos .List (youtubeVideoParts ).Id (videoID ).Do ()
44
54
if err != nil {
45
55
return resolver .InternalServerErrorf ("YouTube API error: %s" , err )
@@ -58,32 +68,60 @@ func (r *VideoLoader) Load(ctx context.Context, videoID string, req *http.Reques
58
68
59
69
video := youtubeResponse .Items [0 ]
60
70
61
- if video .ContentDetails == nil {
71
+ if video .Snippet == nil {
62
72
return resolver .InternalServerErrorf ("YouTube video unavailable" )
63
73
}
64
74
65
- // Check if a video is age resricted: https://stackoverflow.com/a/33750307
66
- var ageRestricted = false
67
- if video .ContentDetails . ContentRating != nil {
68
- if video .ContentDetails . ContentRating . YtRating == "ytAgeRestricted" {
69
- ageRestricted = true
75
+ var tooltip bytes. Buffer
76
+
77
+ if video .Snippet . LiveBroadcastContent == "live" {
78
+ if video .LiveStreamingDetails == nil {
79
+ return resolver . InternalServerErrorf ( "YouTube livestream unavailable" )
70
80
}
71
- }
72
81
73
- data := youtubeVideoTooltipData {
74
- Title : video .Snippet .Title ,
75
- ChannelTitle : video .Snippet .ChannelTitle ,
76
- Duration : humanize .DurationPT (video .ContentDetails .Duration ),
77
- PublishDate : humanize .CreationDateRFC3339 (video .Snippet .PublishedAt ),
78
- Views : humanize .Number (video .Statistics .ViewCount ),
79
- LikeCount : humanize .Number (video .Statistics .LikeCount ),
80
- CommentCount : humanize .Number (video .Statistics .CommentCount ),
81
- AgeRestricted : ageRestricted ,
82
- }
82
+ startTime , err := time .Parse (time .RFC3339 , video .LiveStreamingDetails .ActualStartTime )
83
+ if err != nil {
84
+ return resolver .InternalServerErrorf ("YouTube time parse error: %s" , err )
85
+ }
83
86
84
- var tooltip bytes.Buffer
85
- if err := youtubeVideoTooltipTemplate .Execute (& tooltip , data ); err != nil {
86
- return resolver .InternalServerErrorf ("YouTube template error: %s" , err )
87
+ data := youtubeStreamTooltipData {
88
+ Title : video .Snippet .Title ,
89
+ ChannelTitle : video .Snippet .ChannelTitle ,
90
+ Uptime : humanize .Duration (time .Since (startTime )),
91
+ Viewers : humanize .Number (video .LiveStreamingDetails .ConcurrentViewers ),
92
+ LikeCount : humanize .Number (video .Statistics .LikeCount ),
93
+ }
94
+
95
+ if err := youtubeStreamTooltipTemplate .Execute (& tooltip , data ); err != nil {
96
+ return resolver .InternalServerErrorf ("YouTube template error: %s" , err )
97
+ }
98
+ } else {
99
+ if video .ContentDetails == nil {
100
+ return resolver .InternalServerErrorf ("YouTube video unavailable" )
101
+ }
102
+
103
+ // Check if a video is age resricted: https://stackoverflow.com/a/33750307
104
+ var ageRestricted = false
105
+ if video .ContentDetails .ContentRating != nil {
106
+ if video .ContentDetails .ContentRating .YtRating == "ytAgeRestricted" {
107
+ ageRestricted = true
108
+ }
109
+ }
110
+
111
+ data := youtubeVideoTooltipData {
112
+ Title : video .Snippet .Title ,
113
+ ChannelTitle : video .Snippet .ChannelTitle ,
114
+ Duration : humanize .DurationPT (video .ContentDetails .Duration ),
115
+ PublishDate : humanize .CreationDateRFC3339 (video .Snippet .PublishedAt ),
116
+ Views : humanize .Number (video .Statistics .ViewCount ),
117
+ LikeCount : humanize .Number (video .Statistics .LikeCount ),
118
+ CommentCount : humanize .Number (video .Statistics .CommentCount ),
119
+ AgeRestricted : ageRestricted ,
120
+ }
121
+
122
+ if err := youtubeVideoTooltipTemplate .Execute (& tooltip , data ); err != nil {
123
+ return resolver .InternalServerErrorf ("YouTube template error: %s" , err )
124
+ }
87
125
}
88
126
89
127
thumbnail := video .Snippet .Thumbnails .Default .Url
0 commit comments