better output

This commit is contained in:
TJ Horner 2019-02-28 11:57:42 -08:00
parent 280cbaccd8
commit 0ccfdd7097
1 changed files with 18 additions and 4 deletions

View File

@ -44,15 +44,31 @@ func work(wn int, posts []Post, directory string, completed *int, total *int, wg
for _, post := range posts { for _, post := range posts {
*completed++ *completed++
fmt.Printf("[%d/%d] [w%d] Downloading post %d (%s)...\n", *completed, *total, wn, post.ID, humanize.Bytes(uint64(post.FileSize))) fmt.Printf(
"[%d/%d] [w%d] Downloading post %d (%s) -> %s...\n",
*completed,
*total,
wn,
post.ID,
humanize.Bytes(uint64(post.FileSize)),
getSavePath(&post, &directory),
)
downloadPost(&post, directory) downloadPost(&post, directory)
} }
} }
func downloadPost(post *Post, directory string) { func getSavePath(post *Post, directory *string) string {
pathSliced := strings.Split(post.FileURL, ".") pathSliced := strings.Split(post.FileURL, ".")
extension := pathSliced[len(pathSliced)-1] extension := pathSliced[len(pathSliced)-1]
savePath := path.Join(*directory, strconv.Itoa(post.ID)+"."+extension)
return savePath
}
func downloadPost(post *Post, directory string) {
savePath := getSavePath(post, &directory)
resp, err := HTTPGet(post.FileURL) resp, err := HTTPGet(post.FileURL)
if err != nil { if err != nil {
fmt.Println("Unable to download, skipping...") fmt.Println("Unable to download, skipping...")
@ -66,8 +82,6 @@ func downloadPost(post *Post, directory string) {
return return
} }
savePath := path.Join(directory, strconv.Itoa(post.ID)+"."+extension)
err = ioutil.WriteFile(savePath, body, 0755) err = ioutil.WriteFile(savePath, body, 0755)
if err != nil { if err != nil {
fmt.Printf("Error: could not write to file: %v\n", err) fmt.Printf("Error: could not write to file: %v\n", err)