Skip to content
This repository has been archived by the owner on Jul 26, 2020. It is now read-only.

Commit

Permalink
Import using video URL (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: Dirk Kelly <github@dirkkelly.com>
  • Loading branch information
jmickey and dirkkelly committed May 17, 2019
1 parent bc137f6 commit 97254c5
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion cmd/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"log"
"os"
"regexp"

"github.com/breadtubetv/bake/providers"
"github.com/spf13/cobra"
Expand All @@ -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)
Expand All @@ -24,6 +42,7 @@ var videoCmd = &cobra.Command{

var (
id string
url string
creator string
provider string
)
Expand All @@ -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")
}

0 comments on commit 97254c5

Please sign in to comment.