From 23997e7370cccb48aef9ea3bd9e60d513bfb29de Mon Sep 17 00:00:00 2001 From: structix Date: Sat, 21 Apr 2018 17:54:57 +0200 Subject: [PATCH] Initial commit --- .gitignore | 3 +++ download.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 10 +++++++++ 3 files changed, 71 insertions(+) create mode 100644 download.go create mode 100644 main.go diff --git a/.gitignore b/.gitignore index 7560965..7c71257 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 .glide/ +dl.txt +output +ytbatchdownloader diff --git a/download.go b/download.go new file mode 100644 index 0000000..24cb340 --- /dev/null +++ b/download.go @@ -0,0 +1,58 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "regexp" + //"path/filepath" + + . "github.com/kkdai/youtube" +) + +type Batch struct { + batchfile, destDir string +} + +func NewBatch(batchFile, destinationDir string) *Batch { + return &Batch{batchfile: batchFile, destDir: destinationDir} +} + +func (b *Batch) Start() { + //Start downloading all videos + + //open the file containing yt links + linkfile, err := os.Open(b.batchfile) + if err != nil { + log.Fatal(err) + } + defer linkfile.Close() + + //Create scanner to go line by line through the file + //This scanner allows 65536 characters per line. + //That should be enough for yt links. + scanner := bufio.NewScanner(linkfile) + + for scanner.Scan() { + //Call download + go downloadVideo(b.destDir, scanner.Text()) + } + + if err := scanner.Err(); err != nil { + log.Fatal(err) + } +} + +func downloadVideo(destDir, url string) { + // NewYoutube(debug) if debug parameter will set true we can log of messages + y := NewYoutube(false) + y.DecodeURL(url) + re := regexp.MustCompile("(mp4|webm|3gpp)") + fileext := re.FindString(y.StreamList[0]["type"]) + filename := fmt.Sprintf("%s/%s.%s", destDir, y.StreamList[0]["title"], fileext) + + log.Println("Start downloading:", filename) + y.StartDownload(filename) + log.Println("Finished downloading:", filename) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..db8f4ed --- /dev/null +++ b/main.go @@ -0,0 +1,10 @@ +package main + +import "fmt" + +func main() { + fmt.Println("ytbatchdownloader") + + batch := NewBatch("./dl.txt", "./output") + batch.Start() +}