Fixed empty line crash; Download status

This commit is contained in:
2018-04-25 18:54:39 +02:00
parent 7b6d3ffe54
commit e37ec06287

View File

@@ -6,6 +6,7 @@ import (
"log" "log"
"os" "os"
"regexp" "regexp"
"strings"
"sync" "sync"
//"path/filepath" //"path/filepath"
@@ -41,18 +42,23 @@ func (b *Batch) Start() {
//Call download //Call download
log.Println(scanner.Text()) log.Println(scanner.Text())
//add go routine to wait for //add go routine to wait for
if strings.TrimSpace(scanner.Text()) != "" {
wg.Add(1) wg.Add(1)
go downloadVideo(b.destDir, scanner.Text(), wg) go downloadVideo(b.destDir, scanner.Text(), &wg)
} else {
log.Println("Ignoring blank line")
}
} }
//wait for all go routines to finish //wait for all go routines to finish
wg.Wait() 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, wg sync.WaitGroup) { 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 Done() so that WaitGroup knows when the routine finishes
defer wg.Done() defer wg.Done()
@@ -63,6 +69,15 @@ func downloadVideo(destDir, url string, wg sync.WaitGroup) {
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)
go func() {
var i int64 = 0
for i < 100 {
i = <-y.DownloadPercent
if i%10 == 0 {
log.Println("Status: ", i, "%", y.StreamList[0]["title"])
}
}
}()
y.StartDownload(filename) y.StartDownload(filename)
log.Println("Finished downloading:", filename) log.Println("Finished downloading:", filename)
} }