From e2ea0a179ea584fa994a05dc913298c904060412 Mon Sep 17 00:00:00 2001 From: terorie Date: Mon, 30 Jul 2018 00:10:50 +0200 Subject: [PATCH] Fix CLI for channel dumps --- api/api.go | 8 ++++++++ cmd/channeldump.go | 54 ++++++++++++++++++++++++++++++++++++------------------ main.go | 4 +++- 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/api/api.go b/api/api.go index 06ccb2b..32f2f90 100644 --- a/api/api.go +++ b/api/api.go @@ -16,6 +16,14 @@ type API struct { // TODO Fallback option var DefaultAPI *API = nil +// TODO: Remove when everything is implemented +var TempAPI = API{ + GetVideo: apiclassic.GetVideo, + GetVideoSubtitleList: apiclassic.GetVideoSubtitleList, + GetChannel: apiclassic.GetChannel, + GetChannelVideoURLs: apijson.GetChannelVideoURLs, +} + var ClassicAPI = API{ GetVideo: apiclassic.GetVideo, GetVideoSubtitleList: apiclassic.GetVideoSubtitleList, diff --git a/cmd/channeldump.go b/cmd/channeldump.go index b1b843c..4a1ebf0 100644 --- a/cmd/channeldump.go +++ b/cmd/channeldump.go @@ -3,36 +3,42 @@ package cmd import ( "github.com/spf13/cobra" "net/url" - "fmt" "os" "strings" "time" "bufio" "log" "github.com/terorie/yt-mango/api" + "fmt" ) var channelDumpCmd = cobra.Command{ - Use: "dumpurls ", + Use: "dumpurls [file]", Short: "Get all public video URLs from channel", Long: "Write all videos URLs of a channel to a file", - Args: cobra.ExactArgs(2), + Args: cobra.RangeArgs(1, 2), Run: func(cmd *cobra.Command, args []string) { + printResults := false + fileName := "" channelID := args[0] - fileName := args[1] + if len(args) != 2 { + printResults = true + } else { + fileName = args[1] + } if !matchChannelID.MatchString(channelID) { // Check if youtube.com domain _url, err := url.Parse(channelID) if err != nil || (_url.Host != "www.youtube.com" && _url.Host != "youtube.com") { - fmt.Fprintln(os.Stderr, "Not a channel ID:", channelID) + log.Fatal("Not a channel ID:", channelID) os.Exit(1) } // Check if old /user/ URL if strings.HasPrefix(_url.Path, "/user/") { // TODO Implement extraction of channel ID - fmt.Fprintln(os.Stderr, "New /channel/ link is required!\n" + + log.Fatal("New /channel/ link is required!\n" + "The old /user/ links do not work.") os.Exit(1) } @@ -41,7 +47,7 @@ var channelDumpCmd = cobra.Command{ channelID = strings.TrimPrefix(_url.Path, "/channel/") if len(channelID) == len(_url.Path) { // No such prefix to be removed - fmt.Fprintln(os.Stderr, "Not a channel ID:", channelID) + log.Fatal("Not a channel ID:", channelID) os.Exit(1) } @@ -62,21 +68,27 @@ var channelDumpCmd = cobra.Command{ flags = os.O_WRONLY | os.O_CREATE | os.O_EXCL } - file, err := os.OpenFile(fileName, flags, 0640) - defer file.Close() - writer := bufio.NewWriter(file) - defer writer.Flush() + var file *os.File + var writer *bufio.Writer + + if !printResults { + var err error + file, err = os.OpenFile(fileName, flags, 0640) + if err != nil { + log.Fatal(err) + os.Exit(1) + } + defer file.Close() - if err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) + writer = bufio.NewWriter(file) + defer writer.Flush() } totalURLs := 0 for i := offset; true; i++ { channelURLs, err := api.DefaultAPI.GetChannelVideoURLs(channelID, uint(i)) if err != nil { - log.Printf("Aborting on error %v.", err) + log.Printf("Aborting on error: %v.", err) break } if len(channelURLs) == 0 { @@ -86,9 +98,15 @@ var channelDumpCmd = cobra.Command{ totalURLs += len(channelURLs) log.Printf("Received page %d: %d videos.", i, len(channelURLs)) - for _, _url:= range channelURLs { - _, err := writer.WriteString(_url + "\n") - if err != nil { panic(err) } + if printResults { + for _, _url := range channelURLs { + fmt.Println(_url) + } + } else { + for _, _url := range channelURLs { + _, err := writer.WriteString(_url + "\n") + if err != nil { panic(err) } + } } } diff --git a/main.go b/main.go index 18b1170..0b08cf1 100644 --- a/main.go +++ b/main.go @@ -35,8 +35,10 @@ func main() { fmt.Println(Version) os.Exit(0) } + }, + PersistentPreRun: func(cmd *cobra.Command, args []string) { switch forceAPI { - case "": break + case "": api.DefaultAPI = &api.TempAPI case "classic": api.DefaultAPI = &api.ClassicAPI case "json": api.DefaultAPI = &api.JsonAPI default: -- 2.11.4.GIT