4 Commits
v1.0 ... master

Author SHA1 Message Date
400aab0964 Shortened download status 2018-04-25 19:15:23 +02:00
fc046da8a6 Error handling; extended logging 2018-04-25 19:13:17 +02:00
e37ec06287 Fixed empty line crash; Download status 2018-04-25 18:54:39 +02:00
7b6d3ffe54 Fixed go routines syncing 2018-04-21 21:08:29 +02:00

View File

@@ -6,6 +6,8 @@ import (
"log" "log"
"os" "os"
"regexp" "regexp"
"strings"
"sync"
//"path/filepath" //"path/filepath"
. "github.com/kkdai/youtube" . "github.com/kkdai/youtube"
@@ -21,7 +23,7 @@ func NewBatch(batchFile, destinationDir string) *Batch {
func (b *Batch) Start() { func (b *Batch) Start() {
//Start downloading all videos //Start downloading all videos
log.Println("Start downloading...")
//open the file containing yt links //open the file containing yt links
linkfile, err := os.Open(b.batchfile) linkfile, err := os.Open(b.batchfile)
if err != nil { if err != nil {
@@ -34,18 +36,32 @@ func (b *Batch) Start() {
//That should be enough for yt links. //That should be enough for yt links.
scanner := bufio.NewScanner(linkfile) scanner := bufio.NewScanner(linkfile)
var wg sync.WaitGroup
for scanner.Scan() { for scanner.Scan() {
//Call download //Call download
go downloadVideo(b.destDir, scanner.Text()) //add go routine to wait for
if strings.TrimSpace(scanner.Text()) != "" {
wg.Add(1)
log.Println(scanner.Text())
go downloadVideo(b.destDir, strings.TrimSpace(scanner.Text()), &wg)
} else {
log.Println("Ignoring blank line")
}
} }
//wait for all go routines to finish
wg.Wait()
log.Println("All downloads finished")
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
func downloadVideo(destDir, url string) { func downloadVideo(destDir, url string, wg *sync.WaitGroup) {
// NewYoutube(debug) if debug parameter will set true we can log of messages // NewYoutube(debug) if debug parameter will set true we can log of messages
//defer Done() so that WaitGroup knows when the routine finishes
defer wg.Done()
y := NewYoutube(false) y := NewYoutube(false)
y.DecodeURL(url) y.DecodeURL(url)
re := regexp.MustCompile("(mp4|webm|3gpp)") re := regexp.MustCompile("(mp4|webm|3gpp)")
@@ -53,6 +69,20 @@ func downloadVideo(destDir, url string) {
filename := fmt.Sprintf("%s/%s.%s", destDir, y.StreamList[0]["title"], fileext) filename := fmt.Sprintf("%s/%s.%s", destDir, y.StreamList[0]["title"], fileext)
log.Println("Start downloading:", filename) log.Println("Start downloading:", filename)
y.StartDownload(filename) go func() {
var i int64 = 0
for i < 100 {
i = <-y.DownloadPercent
if i%10 == 0 {
log.Println(i, "%", y.StreamList[0]["title"])
}
}
}()
err := y.StartDownload(filename)
if err != nil {
log.Println("Failed to download", url)
log.Println("Message:", err.Error())
} else {
log.Println("Finished downloading:", filename) log.Println("Finished downloading:", filename)
}
} }