You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
818 B
39 lines
818 B
package DownloadUtil
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/cavaliercoder/grab"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
func DownloadFile(url string, localPath string){
|
|
// create client
|
|
client := grab.NewClient()
|
|
req, _ := grab.NewRequest(localPath, url)
|
|
|
|
// start download
|
|
fmt.Printf("Downloading %v...\n", req.URL())
|
|
resp := client.Do(req)
|
|
fmt.Printf(" %v\n", resp.HTTPResponse.Status)
|
|
// start UI loop
|
|
t := time.NewTicker(500 * time.Millisecond)
|
|
defer t.Stop()
|
|
Loop:
|
|
for {
|
|
select {
|
|
case <-t.C:
|
|
fmt.Printf(" transferred %v / %v bytes (%.2f%%)\n",
|
|
resp.BytesComplete(),resp.Size,100*resp.Progress())
|
|
case <-resp.Done:
|
|
break Loop
|
|
}
|
|
}
|
|
// check for errors
|
|
if err := resp.Err(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Download failed: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Printf("Download saved to %v \n", resp.Filename)
|
|
}
|