Browse Source

Fix CLI for channel dumps

master
terorie 2 years ago
parent
commit
e2ea0a179e
3 changed files with 47 additions and 19 deletions
  1. 8
    0
      api/api.go
  2. 36
    18
      cmd/channeldump.go
  3. 3
    1
      main.go

+ 8
- 0
api/api.go View File

@@ -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,

+ 36
- 18
cmd/channeldump.go View File

@@ -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 <channel ID> <file>",
Use: "dumpurls <channel ID> [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) }
}
}
}


+ 3
- 1
main.go View File

@@ -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:

Loading…
Cancel
Save