Skip to content

Commit

Permalink
Merge branch 'feature-progressbar'
Browse files Browse the repository at this point in the history
* feature-progressbar:
  show download progress
  • Loading branch information
mitakeck committed Mar 3, 2017
2 parents 9bef080 + 824c49a commit 6ac4903
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
30 changes: 21 additions & 9 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"sync"
"time"

pb "gopkg.in/cheggaaa/pb.v1"

"github.com/PuerkitoBio/goquery"
)

Expand All @@ -26,7 +28,7 @@ var (
// worker size : 並列ダウンロード数
worker = 4
// delay time : サーバに負荷を与えないように一定の時間待つ
waitSec = 5
waitSec = 10
)

func (d *Downloader) createURI(category string) ([]string, error) {
Expand All @@ -45,7 +47,7 @@ func (d *Downloader) createURI(category string) ([]string, error) {
return nil, fmt.Errorf("Invalid category : %s", category)
}

func (d *Downloader) download(wg *sync.WaitGroup, q chan string, directory string) {
func (d *Downloader) download(pool *pb.Pool, wg *sync.WaitGroup, q chan string, directory string) {
defer wg.Done()
client := &http.Client{}

Expand All @@ -57,7 +59,7 @@ func (d *Downloader) download(wg *sync.WaitGroup, q chan string, directory strin

fileName, _ := pop(strings.Split(url, "/"))
fileName = filepath.Join(directory, fileName)
fmt.Printf("Download : %s\n", fileName)

output, err := os.Create(fileName)
if err != nil {
fmt.Printf("Error while creating %s : %v\n", fileName, err)
Expand All @@ -84,7 +86,15 @@ func (d *Downloader) download(wg *sync.WaitGroup, q chan string, directory strin
continue
}

_, err = io.Copy(output, res.Body)
// create new bar
bar := pb.New(int(res.ContentLength)).SetUnits(pb.U_BYTES).Prefix(fmt.Sprintf("%-90s", url))
bar.ShowBar = false

pool.Add(bar)
// bar.Start()
writer := io.MultiWriter(output, bar)

_, err = io.Copy(writer, res.Body)
if err != nil {
fmt.Printf("Error while downloading %s : %v\n", url, err)
continue
Expand All @@ -107,11 +117,15 @@ func (d *Downloader) Download(category string, format string, directory string,

var wg sync.WaitGroup
q := make(chan string, len(list))
pool, err := pb.StartPool()
if err != nil {
return err
}

// make worker
for i := 0; i < worker; i++ {
wg.Add(1)
go d.download(&wg, q, directory)
go d.download(pool, &wg, q, directory)
}

for _, url := range list {
Expand All @@ -120,11 +134,9 @@ func (d *Downloader) Download(category string, format string, directory string,
close(q)

wg.Wait()
return nil
}
pool.Stop()

func isContain(s string, substr string) bool {
return strings.Contains(strings.ToLower(s), strings.ToLower(substr))
return nil
}

func (d *Downloader) getFileList(category string, format string, searchWord string) ([]string, error) {
Expand Down
6 changes: 6 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package main

import "strings"

func pop(slice []string) (string, []string) {
ans := slice[len(slice)-1]
slice = slice[:len(slice)-1]
return ans, slice
}

func isContain(s string, substr string) bool {
return strings.Contains(strings.ToLower(s), strings.ToLower(substr))
}

0 comments on commit 6ac4903

Please sign in to comment.