Skip to content

Commit

Permalink
* ⚡ improve download progress bar percentage value
Browse files Browse the repository at this point in the history
* 🎨 remove ShowProgress public API, refactor flags in main file
  • Loading branch information
Noel committed Mar 12, 2018
1 parent 6a9b88d commit 8994d77
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 43 deletions.
39 changes: 22 additions & 17 deletions domain/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Video struct {

videoLength int
downloadedLength int
DownloadP chan int
downloadPro chan float64
savePath string
}

Expand Down Expand Up @@ -74,43 +74,45 @@ func NewVideo(t, a, q, vt, url string) Video {
url,
0,
0,
make(chan int),
make(chan float64),
"./",
}
}


func (v Video) ShowProgress() {
func (v Video) showProgress() {
fmt.Println("Downloading: " + v.GetTitle() + " " + v.GetQuality())
uiprogress.Start()
bar := uiprogress.AddBar(100)
bar.AppendCompleted()
bar.PrependElapsed()
pVal := 0
for p := range v.DownloadP {
if pVal != p {
diff := p - pVal
for i := 0; i < diff; i++ {
bar.Incr()
for p := range v.downloadPro {
if pVal != int(p) {
bar.Incr()
pVal = int(p)
if pVal >= 98 {
for i := pVal; i < 100; i++ {
bar.Incr()
}
}
pVal = p
}
}

fmt.Println("Finished: " + v.GetTitle() + " quality: " + v.GetQuality())
}


func (v *Video) Write(b []byte) (n int, err error) {
v.downloadedLength = v.downloadedLength + len(b)
p := (100 / (v.videoLength / v.downloadedLength))
v.DownloadP <- p
if p == 100 && v.videoLength <= v.downloadedLength {
close(v.DownloadP)
p := 100 / (float64(v.videoLength) / float64(v.downloadedLength))
v.downloadPro <- p
if p == 100 {
close(v.downloadPro)
}
return len(b), nil
}

func (v *Video) Download() Video {
// this has to have a public chan to notify the download is done
go func() {
resp, err := http.Get(v.getUrl())
if err != nil {
Expand All @@ -132,6 +134,10 @@ func (v *Video) Download() Video {
fmt.Println(err)
}
}()

// show progress bar here
v.showProgress()

return Video{
v.title,
v.author,
Expand All @@ -140,8 +146,7 @@ func (v *Video) Download() Video {
v.url,
v.videoLength,
v.downloadedLength,
v.DownloadP,
v.downloadPro,
"./",
}
}

38 changes: 12 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,26 @@ import (
gonyvido "github.com/noelyahan/gonyvido/api"
)

var url string
var savePath string
var quality string

func init() {
const (
defaultUrl = "https://www.youtube.com/watch?v=PIyrV8UmNqg"
defaultSavePath = "./"
defaultQuality = "high" // medium, low
)
// url flags
flag.StringVar(&url, "url", defaultUrl, "Video download url")
flag.StringVar(&url, "u", defaultUrl, "Video download url")

// save path flags
flag.StringVar(&savePath, "path", defaultSavePath, "Video save path")
flag.StringVar(&savePath, "p", defaultSavePath, "Video save path")

// quaity flags
flag.StringVar(&quality, "quality", defaultQuality, "Video quality")
flag.StringVar(&quality, "q", defaultQuality, "Video quality")
}
const (
defaultUrl = "https://youtu.be/FBHEF1nxbi0"
defaultSavePath = "./"
defaultQuality = "high" // medium, low
)

func main() {
url := flag.String("url", defaultUrl, "Video download url")
savePath := flag.String("path", defaultSavePath, "Video download path")
quality := flag.String("quality", defaultQuality, "Video download quality")

flag.Parse()

switch quality {
switch *quality {
case "high":
gonyvido.GetHQVideo(url).SetSavePath(savePath).Download().ShowProgress()
gonyvido.GetHQVideo(*url).SetSavePath(*savePath).Download()
case "medium":
gonyvido.GetMQVideo(url).SetSavePath(savePath).Download().ShowProgress()
gonyvido.GetMQVideo(*url).SetSavePath(*savePath).Download()
case "low":
gonyvido.GetLQVideo(url).SetSavePath(savePath).Download().ShowProgress()
gonyvido.GetLQVideo(*url).SetSavePath(*savePath).Download()
}

}

0 comments on commit 8994d77

Please sign in to comment.