2021-11-02 21:46:03 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2021-11-04 21:28:28 +01:00
|
|
|
"github.com/lbryio/ytsync/v5/downloader/ytdl"
|
2021-11-02 21:46:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type YtdlVideoSource struct {
|
|
|
|
downloader Ytdl
|
2021-11-04 21:28:28 +01:00
|
|
|
enrichers []YouTubeVideoEnricher
|
2021-11-02 21:46:03 +01:00
|
|
|
}
|
|
|
|
|
2021-11-04 21:28:28 +01:00
|
|
|
func NewYtdlVideoSource(downloadDir string, config *YouTubeSourceConfig) (*YtdlVideoSource, error) {
|
2021-11-02 21:46:03 +01:00
|
|
|
ytdl, err := NewYtdl(downloadDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
source := YtdlVideoSource {
|
|
|
|
downloader: *ytdl,
|
|
|
|
}
|
|
|
|
|
2021-11-04 21:28:28 +01:00
|
|
|
if config.YouTubeAPIKey != "" {
|
|
|
|
ytapiEnricher := NewYouTubeAPIVideoEnricher(config.YouTubeAPIKey)
|
|
|
|
source.enrichers = append(source.enrichers, ytapiEnricher)
|
|
|
|
}
|
|
|
|
|
2021-11-02 21:46:03 +01:00
|
|
|
return &source, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *YtdlVideoSource) GetVideo(id string) (*SourceVideo, error) {
|
|
|
|
metadata, err := s.downloader.GetVideoMetadata(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
videoPath, err := s.downloader.GetVideoFile(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-04 21:28:28 +01:00
|
|
|
var bestThumbnail *ytdl.Thumbnail = nil
|
|
|
|
for i, thumbnail := range metadata.Thumbnails {
|
|
|
|
if i == 0 || bestThumbnail.Width < thumbnail.Width {
|
|
|
|
bestThumbnail = &thumbnail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 21:46:03 +01:00
|
|
|
sourceVideo := SourceVideo {
|
|
|
|
ID: id,
|
|
|
|
Title: &metadata.Title,
|
|
|
|
Description: &metadata.Description,
|
|
|
|
SourceURL: "\nhttps://www.youtube.com/watch?v=" + id,
|
|
|
|
Languages: []string{},
|
|
|
|
Tags: metadata.Tags,
|
2021-11-04 21:28:28 +01:00
|
|
|
ReleaseTime: nil,
|
|
|
|
ThumbnailURL: &bestThumbnail.URL,
|
2021-11-02 21:46:03 +01:00
|
|
|
FullLocalPath: videoPath,
|
|
|
|
}
|
|
|
|
|
2021-11-04 21:28:28 +01:00
|
|
|
for _, enricher := range s.enrichers {
|
|
|
|
err = enricher.EnrichMissing(&sourceVideo)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Error enriching video %s, continuing enrichment: %v", id, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 21:46:03 +01:00
|
|
|
log.Debugf("Source video retrieved via ytdl: %v", sourceVideo)
|
|
|
|
|
|
|
|
return &sourceVideo, nil
|
|
|
|
}
|