/
youtube.go
465 lines (377 loc) · 12.1 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package builder
import (
"context"
"fmt"
"net/http"
"sort"
"strconv"
"strings"
"time"
"github.com/BrianHicks/finch/duration"
"github.com/mxpv/podsync/pkg/feed"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"google.golang.org/api/youtube/v3"
"github.com/mxpv/podsync/pkg/model"
)
const (
maxYoutubeResults = 50
hdBytesPerSecond = 350000
ldBytesPerSecond = 100000
lowAudioBytesPerSecond = 48000 / 8
highAudioBytesPerSecond = 128000 / 8
)
type apiKey string
func (key apiKey) Get() (string, string) {
return "key", string(key)
}
type YouTubeBuilder struct {
client *youtube.Service
key apiKey
}
// Cost: 5 units (call method: 1, snippet: 2, contentDetails: 2)
// See https://developers.google.com/youtube/v3/docs/channels/list#part
func (yt *YouTubeBuilder) listChannels(ctx context.Context, linkType model.Type, id string, parts string) (*youtube.Channel, error) {
req := yt.client.Channels.List(parts)
switch linkType {
case model.TypeChannel:
req = req.Id(id)
case model.TypeUser:
req = req.ForUsername(id)
default:
return nil, errors.New("unsupported link type")
}
resp, err := req.Context(ctx).Do(yt.key)
if err != nil {
return nil, errors.Wrapf(err, "failed to query channel")
}
if len(resp.Items) == 0 {
return nil, model.ErrNotFound
}
item := resp.Items[0]
return item, nil
}
// Cost: 3 units (call method: 1, snippet: 2)
// See https://developers.google.com/youtube/v3/docs/playlists/list#part
func (yt *YouTubeBuilder) listPlaylists(ctx context.Context, id, channelID string, parts string) (*youtube.Playlist, error) {
req := yt.client.Playlists.List(parts)
if id != "" {
req = req.Id(id)
} else {
req = req.ChannelId(channelID)
}
resp, err := req.Context(ctx).Do(yt.key)
if err != nil {
return nil, errors.Wrapf(err, "failed to query playlist")
}
if len(resp.Items) == 0 {
return nil, model.ErrNotFound
}
item := resp.Items[0]
return item, nil
}
// Cost: 3 units (call: 1, snippet: 2)
// See https://developers.google.com/youtube/v3/docs/playlistItems/list#part
func (yt *YouTubeBuilder) listPlaylistItems(ctx context.Context, feed *model.Feed, pageToken string) ([]*youtube.PlaylistItem, string, error) {
count := maxYoutubeResults
if count > feed.PageSize {
// If we need less than 50
count = feed.PageSize
}
req := yt.client.PlaylistItems.List("id,snippet").MaxResults(int64(count)).PlaylistId(feed.ItemID)
if pageToken != "" {
req = req.PageToken(pageToken)
}
resp, err := req.Context(ctx).Do(yt.key)
if err != nil {
return nil, "", errors.Wrap(err, "failed to query playlist items")
}
return resp.Items, resp.NextPageToken, nil
}
func (yt *YouTubeBuilder) parseDate(s string) (time.Time, error) {
date, err := time.Parse(time.RFC3339, s)
if err != nil {
return time.Time{}, errors.Wrapf(err, "failed to parse date: %s", s)
}
return date, nil
}
func (yt *YouTubeBuilder) selectThumbnail(snippet *youtube.ThumbnailDetails, quality model.Quality, videoID string) string {
if snippet == nil {
if videoID != "" {
return fmt.Sprintf("https://img.youtube.com/vi/%s/default.jpg", videoID)
}
// TODO: use Podsync's preview image if unable to retrieve from YouTube
return ""
}
// Use high resolution thumbnails for high quality mode
// https://github.com/mxpv/Podsync/issues/14
if quality == model.QualityHigh {
if snippet.Maxres != nil {
return snippet.Maxres.Url
}
if snippet.High != nil {
return snippet.High.Url
}
if snippet.Medium != nil {
return snippet.Medium.Url
}
}
return snippet.Default.Url
}
func (yt *YouTubeBuilder) GetVideoCount(ctx context.Context, info *model.Info) (uint64, error) {
switch info.LinkType {
case model.TypeChannel, model.TypeUser:
// Cost: 3 units
if channel, err := yt.listChannels(ctx, info.LinkType, info.ItemID, "id,statistics"); err != nil {
return 0, err
} else { // nolint:golint
return channel.Statistics.VideoCount, nil
}
case model.TypePlaylist:
// Cost: 3 units
if playlist, err := yt.listPlaylists(ctx, info.ItemID, "", "id,contentDetails"); err != nil {
return 0, err
} else { // nolint:golint
return uint64(playlist.ContentDetails.ItemCount), nil
}
default:
return 0, errors.New("unsupported link format")
}
}
func (yt *YouTubeBuilder) queryFeed(ctx context.Context, feed *model.Feed, info *model.Info) error {
var (
thumbnails *youtube.ThumbnailDetails
)
switch info.LinkType {
case model.TypeChannel, model.TypeUser:
// Cost: 5 units for channel or user
channel, err := yt.listChannels(ctx, info.LinkType, info.ItemID, "id,snippet,contentDetails")
if err != nil {
return err
}
feed.Title = channel.Snippet.Title
feed.Description = channel.Snippet.Description
if channel.Kind == "youtube#channel" {
feed.ItemURL = fmt.Sprintf("https://youtube.com/channel/%s", channel.Id)
feed.Author = "<notfound>"
} else {
feed.ItemURL = fmt.Sprintf("https://youtube.com/user/%s", channel.Snippet.CustomUrl)
feed.Author = channel.Snippet.CustomUrl
}
feed.ItemID = channel.ContentDetails.RelatedPlaylists.Uploads
if date, err := yt.parseDate(channel.Snippet.PublishedAt); err != nil {
return err
} else { // nolint:golint
feed.PubDate = date
}
thumbnails = channel.Snippet.Thumbnails
case model.TypePlaylist:
// Cost: 3 units for playlist
playlist, err := yt.listPlaylists(ctx, info.ItemID, "", "id,snippet")
if err != nil {
return err
}
feed.Title = fmt.Sprintf("%s: %s", playlist.Snippet.ChannelTitle, playlist.Snippet.Title)
feed.Description = playlist.Snippet.Description
feed.ItemURL = fmt.Sprintf("https://youtube.com/playlist?list=%s", playlist.Id)
feed.ItemID = playlist.Id
feed.Author = "<notfound>"
if date, err := yt.parseDate(playlist.Snippet.PublishedAt); err != nil {
return err
} else { // nolint:golint
feed.PubDate = date
}
thumbnails = playlist.Snippet.Thumbnails
default:
return errors.New("unsupported link format")
}
if feed.Description == "" {
feed.Description = fmt.Sprintf("%s (%s)", feed.Title, feed.PubDate)
}
feed.CoverArt = yt.selectThumbnail(thumbnails, feed.CoverArtQuality, "")
return nil
}
// Video size information requires 1 additional call for each video (1 feed = 50 videos = 50 calls),
// which is too expensive, so get approximated size depending on duration and definition params
func (yt *YouTubeBuilder) getSize(duration int64, feed *model.Feed) int64 {
if feed.Format == model.FormatAudio {
if feed.Quality == model.QualityHigh {
return highAudioBytesPerSecond * duration
}
return lowAudioBytesPerSecond * duration
}
// Video format
if feed.Quality == model.QualityHigh {
return duration * hdBytesPerSecond
}
return duration * ldBytesPerSecond
}
// Cost: 5 units (call: 1, snippet: 2, contentDetails: 2)
// See https://developers.google.com/youtube/v3/docs/videos/list#part
func (yt *YouTubeBuilder) queryVideoDescriptions(ctx context.Context, playlist map[string]*youtube.PlaylistItemSnippet, feed *model.Feed) error {
// Make the list of video ids
ids := make([]string, 0, len(playlist))
for _, s := range playlist {
ids = append(ids, s.ResourceId.VideoId)
}
// Init a list that will contains the aggregated strings of videos IDs (capped at 50 IDs per API Calls)
idsList := make([]string, 0, 1)
// Chunk the list of IDs by slices limited to maxYoutubeResults
for i := 0; i < len(ids); i += maxYoutubeResults {
end := i + maxYoutubeResults
if end > len(ids) {
end = len(ids)
}
// Save each slice as comma-delimited string
idsList = append(idsList, strings.Join(ids[i:end], ","))
}
// Show how many API calls will be required
log.Debugf("Expected to make %d API calls to get the descriptions for %d episode(s).", len(idsList), len(ids))
// Loop in each slices of 50 (or less) IDs and query their description
for _, idsI := range idsList {
req, err := yt.client.Videos.List("id,snippet,contentDetails").Id(idsI).Context(ctx).Do(yt.key)
if err != nil {
return errors.Wrap(err, "failed to query video descriptions")
}
for _, video := range req.Items {
var (
snippet = video.Snippet
videoID = video.Id
videoURL = fmt.Sprintf("https://youtube.com/watch?v=%s", video.Id)
image = yt.selectThumbnail(snippet.Thumbnails, feed.Quality, videoID)
)
// Parse date added to playlist / publication date
dateStr := ""
playlistItem, ok := playlist[video.Id]
if ok {
dateStr = playlistItem.PublishedAt
} else {
dateStr = snippet.PublishedAt
}
pubDate, err := yt.parseDate(dateStr)
if err != nil {
return errors.Wrapf(err, "failed to parse video publish date: %s", dateStr)
}
// Sometimes YouTube retrun empty content defailt, use arbitrary one
var seconds int64 = 1
if video.ContentDetails != nil {
// Parse duration
d, err := duration.FromString(video.ContentDetails.Duration)
if err != nil {
return errors.Wrapf(err, "failed to parse duration %s", video.ContentDetails.Duration)
}
seconds = int64(d.ToDuration().Seconds())
}
var (
order = strconv.FormatInt(playlistItem.Position, 10)
size = yt.getSize(seconds, feed)
)
feed.Episodes = append(feed.Episodes, &model.Episode{
ID: video.Id,
Title: snippet.Title,
Description: snippet.Description,
Thumbnail: image,
Duration: seconds,
Size: size,
VideoURL: videoURL,
PubDate: pubDate,
Order: order,
Status: model.EpisodeNew,
})
}
}
return nil
}
// Cost:
// ASC mode = (3 units + 5 units) * X pages = 8 units per page
// DESC mode = 3 units * (number of pages in the entire playlist) + 5 units
func (yt *YouTubeBuilder) queryItems(ctx context.Context, feed *model.Feed) error {
var (
token string
count int
allSnippets []*youtube.PlaylistItemSnippet
)
for {
items, pageToken, err := yt.listPlaylistItems(ctx, feed, token)
if err != nil {
return err
}
token = pageToken
if len(items) == 0 {
break
}
// Extract playlist snippets
for _, item := range items {
allSnippets = append(allSnippets, item.Snippet)
count++
}
if (feed.PlaylistSort != model.SortingDesc && count >= feed.PageSize) || token == "" {
break
}
}
if len(allSnippets) > feed.PageSize {
if feed.PlaylistSort != model.SortingDesc {
allSnippets = allSnippets[:feed.PageSize]
} else {
allSnippets = allSnippets[len(allSnippets)-feed.PageSize:]
}
}
snippets := map[string]*youtube.PlaylistItemSnippet{}
for _, snippet := range allSnippets {
snippets[snippet.ResourceId.VideoId] = snippet
}
// Query video descriptions from the list of ids
if err := yt.queryVideoDescriptions(ctx, snippets, feed); err != nil {
return err
}
return nil
}
func (yt *YouTubeBuilder) Build(ctx context.Context, cfg *feed.Config) (*model.Feed, error) {
info, err := ParseURL(cfg.URL)
if err != nil {
return nil, err
}
_feed := &model.Feed{
ItemID: info.ItemID,
Provider: info.Provider,
LinkType: info.LinkType,
Format: cfg.Format,
Quality: cfg.Quality,
CoverArtQuality: cfg.Custom.CoverArtQuality,
PageSize: cfg.PageSize,
PlaylistSort: cfg.PlaylistSort,
PrivateFeed: cfg.PrivateFeed,
UpdatedAt: time.Now().UTC(),
}
if _feed.PageSize == 0 {
_feed.PageSize = maxYoutubeResults
}
// Query general information about feed (title, description, lang, etc)
if err := yt.queryFeed(ctx, _feed, &info); err != nil {
return nil, err
}
if err := yt.queryItems(ctx, _feed); err != nil {
return nil, err
}
// YT API client gets 50 episodes per query.
// Round up to page size.
if len(_feed.Episodes) > _feed.PageSize {
_feed.Episodes = _feed.Episodes[:_feed.PageSize]
}
sort.Slice(_feed.Episodes, func(i, j int) bool {
item1, _ := strconv.Atoi(_feed.Episodes[i].Order)
item2, _ := strconv.Atoi(_feed.Episodes[j].Order)
return item1 < item2
})
return _feed, nil
}
func NewYouTubeBuilder(key string) (*YouTubeBuilder, error) {
if key == "" {
return nil, errors.New("empty YouTube API key")
}
yt, err := youtube.New(&http.Client{})
if err != nil {
return nil, errors.Wrap(err, "failed to create youtube client")
}
return &YouTubeBuilder{client: yt, key: apiKey(key)}, nil
}