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

Import using video URL #19

Merged
merged 2 commits into from
May 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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})`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another valid format is https://youtu.be/rURhJr0cDnk and it's been used in a few submissions so far.

I'd be happy for us to have limitations on acceptable formats for simplicity sake, eg:

Perhaps the regex is something along the lines of v=XXX or final slash and everything after.

Say I tried https://youtu.be/rURhJr0cDnk?rofl=lmao I would be happy for the parser to fail.

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")
}