summaryrefslogtreecommitdiff
path: root/importers
diff options
context:
space:
mode:
authorJames Mills <prologic@shortcircuit.net.au>2020-03-28 12:16:45 +1000
committerJames Mills <prologic@shortcircuit.net.au>2020-03-28 12:16:45 +1000
commit88b96784ab4fd19b13d0076d64a95300483363ca (patch)
tree4e86be713c367f37dad1906849ec7a15ebbe7a9b /importers
parent5100a108634cb63ae508669878b05ed5fa20c31a (diff)
Refactored video importers
Diffstat (limited to 'importers')
-rw-r--r--importers/importer.go33
-rw-r--r--importers/vimeo_importer.go10
-rw-r--r--importers/youtube_importer.go37
3 files changed, 80 insertions, 0 deletions
diff --git a/importers/importer.go b/importers/importer.go
new file mode 100644
index 0000000..c74d621
--- /dev/null
+++ b/importers/importer.go
@@ -0,0 +1,33 @@
+package importers
+
+import (
+ "errors"
+ "strings"
+)
+
+var (
+ ErrUnsupportedVideoURL = errors.New("error: unsupported video url")
+)
+
+type VideoInfo struct {
+ ID string `json:"id"`
+ Title string `json:"title"`
+ Description string `json:"description"`
+
+ VideoURL string `json:"video_url"`
+ ThumbnailURL string `json:"thumbnail_url"`
+}
+
+type Importer interface {
+ GetVideoInfo(url string) (VideoInfo, error)
+}
+
+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:") {
+ return &VimeoImporter{}, nil
+ } else {
+ return nil, ErrUnsupportedVideoURL
+ }
+}
diff --git a/importers/vimeo_importer.go b/importers/vimeo_importer.go
new file mode 100644
index 0000000..5d5ed02
--- /dev/null
+++ b/importers/vimeo_importer.go
@@ -0,0 +1,10 @@
+package importers
+
+import "fmt"
+
+type VimeoImporter struct{}
+
+func (i *VimeoImporter) GetVideoInfo(url string) (videoInfo VideoInfo, err error) {
+ err = fmt.Errorf("Not Implemented")
+ return
+}
diff --git a/importers/youtube_importer.go b/importers/youtube_importer.go
new file mode 100644
index 0000000..5702b05
--- /dev/null
+++ b/importers/youtube_importer.go
@@ -0,0 +1,37 @@
+package importers
+
+import (
+ "fmt"
+ "strings"
+
+ "github.com/rylio/ytdl"
+)
+
+type YoutubeImporter struct{}
+
+func (i *YoutubeImporter) GetVideoInfo(url string) (videoInfo VideoInfo, err error) {
+ if strings.HasPrefix(url, "youtube:") {
+ url = strings.TrimPrefix(url, "youtube:")
+ }
+
+ info, err := ytdl.GetVideoInfo(url)
+ if err != nil {
+ err = fmt.Errorf("error retriving youtube video info: %w", err)
+ return
+ }
+
+ videoURL, err := ytdl.DefaultClient.GetDownloadURL(info, info.Formats[0])
+ if err != nil {
+ err = fmt.Errorf("error retriving youtube video url: %w", err)
+ return
+ }
+ videoInfo.VideoURL = videoURL.String()
+
+ videoInfo.ThumbnailURL = info.GetThumbnailURL(ytdl.ThumbnailQualityHigh).String()
+
+ videoInfo.ID = info.ID
+ videoInfo.Title = info.Title
+ videoInfo.Description = info.Description
+
+ return
+}