From 55181b4c1787557bf2d10f668b4fd89cee6ad618 Mon Sep 17 00:00:00 2001 From: Josh Michielsen Date: Thu, 16 May 2019 19:31:18 +0100 Subject: [PATCH] added ability to import using video URL --- cmd/video.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/cmd/video.go b/cmd/video.go index d9ff1fb..e50d237 100644 --- a/cmd/video.go +++ b/cmd/video.go @@ -3,6 +3,7 @@ package cmd import ( "log" "os" + "regexp" "github.com/breadtubetv/bake/providers" "github.com/spf13/cobra" @@ -15,6 +16,23 @@ var videoCmd = &cobra.Command{ Short: "Import a video by ID", Long: `Import a YouTube video by ID and assign it to a creator.`, Run: func(cmd *cobra.Command, args []string) { + if id == "" && url == "" { + log.Fatal("command must include either video ID or URL") + } + if id != "" && url != "" { + log.Fatal("both video ID and URL provided, expected only one") + } + + if url != "" { + re := regexp.MustCompile(`(?:v|embed|watch\?v)(?:=|/)([^"&?/=%]{11})`) + if match := re.MatchString(url); match { + subs := re.FindStringSubmatch(url) + id = subs[1] + } else { + log.Fatal("the given URL is not a valid YouTube URL") + } + } + err := providers.ImportVideo(id, creator, os.ExpandEnv(viper.GetString("projectRoot"))) if err != nil { log.Fatalf("could not import video: %v", err) @@ -24,6 +42,7 @@ var videoCmd = &cobra.Command{ var ( id string + url string creator string provider string ) @@ -32,10 +51,10 @@ func init() { importRootCmd.AddCommand(videoCmd) videoCmd.Flags().StringVar(&id, "id", "", "ID of the video, e.g. xspEtjnSfQA is the ID for https://www.youtube.com/watch?v=xspEtjnSfQA") + videoCmd.Flags().StringVarP(&url, "url", "u", "", "URL of the video, e.g. https://www.youtube.com/watch?v=xspEtjnSfQA. Use instead of --id.") videoCmd.Flags().StringVarP(&creator, "creator", "c", "", "Creator slug for the imported video") videoCmd.Flags().StringVarP(&provider, "provider", "p", "", "Video provider to import from - e.g. youtube") - videoCmd.MarkFlagRequired("id") videoCmd.MarkFlagRequired("creator") videoCmd.MarkFlagRequired("provider") }