Added progressbar
This commit is contained in:
22
download.go
22
download.go
@@ -10,6 +10,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
//"path/filepath"
|
//"path/filepath"
|
||||||
|
|
||||||
|
"github.com/gosuri/uiprogress"
|
||||||
. "github.com/kkdai/youtube"
|
. "github.com/kkdai/youtube"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -36,6 +37,11 @@ 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)
|
||||||
|
|
||||||
|
//Start rendering for progressbar
|
||||||
|
//This is the initialization the actual bars
|
||||||
|
//will be instantiated in the go routines
|
||||||
|
uiprogress.Start()
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
@@ -67,14 +73,24 @@ func downloadVideo(destDir, url string, wg *sync.WaitGroup) {
|
|||||||
re := regexp.MustCompile("(mp4|webm|3gpp)")
|
re := regexp.MustCompile("(mp4|webm|3gpp)")
|
||||||
fileext := re.FindString(y.StreamList[0]["type"])
|
fileext := re.FindString(y.StreamList[0]["type"])
|
||||||
filename := fmt.Sprintf("%s/%s.%s", destDir, y.StreamList[0]["title"], fileext)
|
filename := fmt.Sprintf("%s/%s.%s", destDir, y.StreamList[0]["title"], fileext)
|
||||||
|
filenameRune := []rune(y.StreamList[0]["title"])
|
||||||
|
shortFilename := string(filenameRune[0:20]) + "..."
|
||||||
|
|
||||||
log.Println("Start downloading:", filename)
|
//log.Println("Start downloading:", filename)
|
||||||
go func() {
|
go func() {
|
||||||
|
bar := uiprogress.AddBar(100).AppendCompleted()
|
||||||
|
|
||||||
|
//Custom decorator
|
||||||
|
bar.PrependFunc(func(b *uiprogress.Bar) string {
|
||||||
|
return shortFilename
|
||||||
|
})
|
||||||
|
|
||||||
var i int64 = 0
|
var i int64 = 0
|
||||||
for i < 100 {
|
for i < 100 {
|
||||||
i = <-y.DownloadPercent
|
i = <-y.DownloadPercent
|
||||||
|
bar.Set(int(i))
|
||||||
if i%10 == 0 {
|
if i%10 == 0 {
|
||||||
log.Println(i, "%", y.StreamList[0]["title"])
|
//log.Println(i, "%", y.StreamList[0]["title"])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -83,6 +99,6 @@ func downloadVideo(destDir, url string, wg *sync.WaitGroup) {
|
|||||||
log.Println("Failed to download", url)
|
log.Println("Failed to download", url)
|
||||||
log.Println("Message:", err.Error())
|
log.Println("Message:", err.Error())
|
||||||
} else {
|
} else {
|
||||||
log.Println("Finished downloading:", filename)
|
//log.Println("Finished downloading:", filename)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user