From dab8ea9f1a3d97532fc05775f41b3ff2b529798e Mon Sep 17 00:00:00 2001 From: James Mills Date: Sat, 28 Mar 2020 13:11:54 +1000 Subject: [PATCH] Add vimeo video importer --- go.mod | 1 + go.sum | 6 +++++ importers/importer.go | 2 +- importers/vimeo_importer.go | 44 +++++++++++++++++++++++++++++++++++-- 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index fcf5215..473becd 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/mitchellh/mapstructure v1.2.2 // indirect github.com/mutschler/mt v0.0.0-20200120124658-d48aed259ff6 // indirect github.com/prologic/bitcask v0.3.5 + github.com/prologic/vimeodl v0.0.0-20200328030915-d2b0e6272c23 github.com/renstrom/shortuuid v3.0.0+incompatible github.com/rs/zerolog v1.18.0 // indirect github.com/rylio/ytdl v0.6.3-0.20200220142242-f3a87da86fb8 diff --git a/go.sum b/go.sum index b77c9a2..208285d 100644 --- a/go.sum +++ b/go.sum @@ -137,6 +137,12 @@ github.com/plar/go-adaptive-radix-tree v1.0.1/go.mod h1:Ot8d28EII3i7Lv4PSvBlF8ej github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prologic/bitcask v0.3.5 h1:o5PekS/LTRXQvLmY/5oQxIgjdT5bwcxPLsrGmnyo3Yo= github.com/prologic/bitcask v0.3.5/go.mod h1:gl5FAhs5GhvmV6tEIQWwk9d/FD9vc8NC8Hs24/zU/4w= +github.com/prologic/vimeodl v0.0.0-20200328022514-f93b4783a93d h1:z928AMwVg+Zrk4KoA+J47bMp3V9vxRl8t+izSGMA218= +github.com/prologic/vimeodl v0.0.0-20200328022514-f93b4783a93d/go.mod h1:uG4Pdd9lZNNkvQHFr70+Qi7im3/YYnBOtUCrqyZKwlw= +github.com/prologic/vimeodl v0.0.0-20200328023734-d27c4c341c4f h1:JXB97k4DcG3NxKEk6+WVUlpJzQvcuqoviHd1OUPky5g= +github.com/prologic/vimeodl v0.0.0-20200328023734-d27c4c341c4f/go.mod h1:uG4Pdd9lZNNkvQHFr70+Qi7im3/YYnBOtUCrqyZKwlw= +github.com/prologic/vimeodl v0.0.0-20200328030915-d2b0e6272c23 h1:2iUEG1ZXpjBoq11gFdlWOJStBN/3aW3MlY0V5QONZas= +github.com/prologic/vimeodl v0.0.0-20200328030915-d2b0e6272c23/go.mod h1:uG4Pdd9lZNNkvQHFr70+Qi7im3/YYnBOtUCrqyZKwlw= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= diff --git a/importers/importer.go b/importers/importer.go index c74d621..f5d4b36 100644 --- a/importers/importer.go +++ b/importers/importer.go @@ -25,7 +25,7 @@ type Importer interface { func NewImporter(url string) (Importer, error) { if strings.Contains(url, "youtube.com") || strings.HasPrefix(url, "youtube:") { return &YoutubeImporter{}, nil - } else if strings.Contains(url, "youtube.com") || strings.HasPrefix(url, "youtube:") { + } else if strings.Contains(url, "vimeo.com") || strings.HasPrefix(url, "vimeo:") { return &VimeoImporter{}, nil } else { return nil, ErrUnsupportedVideoURL diff --git a/importers/vimeo_importer.go b/importers/vimeo_importer.go index 5d5ed02..9983a69 100644 --- a/importers/vimeo_importer.go +++ b/importers/vimeo_importer.go @@ -1,10 +1,50 @@ package importers -import "fmt" +import ( + "fmt" + "strings" + + "github.com/prologic/vimeodl" +) type VimeoImporter struct{} func (i *VimeoImporter) GetVideoInfo(url string) (videoInfo VideoInfo, err error) { - err = fmt.Errorf("Not Implemented") + if strings.HasPrefix(url, "vimeo:") { + url = strings.TrimPrefix(url, "vimeo:") + } + + if !strings.HasPrefix(url, "http") { + url = "https://player.vimeo.com/video/" + url + } + + if !strings.HasPrefix(url, "https://player.vimeo.com/video/") { + playerURL, err := vimeodl.GetPlayerURL(url) + if err != nil { + err := fmt.Errorf("error finding player url: %w", err) + return VideoInfo{}, err + } + url = playerURL + } + + if !strings.HasSuffix(url, "/") { + url += "/" + } + + url += "config" + + config, err := vimeodl.GetVideoConfig(url) + if err != nil { + err := fmt.Errorf("error retrieving video config: %w", err) + return VideoInfo{}, err + } + + videoInfo.VideoURL = vimeodl.PickBestVideo(config) + + videoInfo.ThumbnailURL = vimeodl.PickBestThumbnail(config) + + videoInfo.ID = string(config.Video.Id) + videoInfo.Title = config.Video.Title + return }