This commit is contained in:
TJ Horner 2019-03-13 12:20:04 -07:00
parent 1c5856a243
commit 96aee306b5
2 changed files with 9 additions and 7 deletions

View File

@ -1,4 +1,4 @@
package e621 package concurrent
import ( import (
"fmt" "fmt"
@ -10,13 +10,14 @@ import (
"time" "time"
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
"github.com/tjhorner/e6dl/e621"
) )
// BeginDownload takes a slice of posts, a directory to save them in, and a // BeginDownload takes a slice of posts, a directory to save them in, and a
// number of concurrent workers to make. It blocks until all the post have // number of concurrent workers to make. It blocks until all the post have
// been processed. It returns the number of successes, failures, and the total // been processed. It returns the number of successes, failures, and the total
// amount of posts. // amount of posts.
func BeginDownload(posts *[]Post, saveDirectory *string, maxConcurrents *int) (*int, *int, *int) { func BeginDownload(posts *[]e621.Post, saveDirectory *string, maxConcurrents *int) (*int, *int, *int) {
var wg sync.WaitGroup var wg sync.WaitGroup
var completed int var completed int
var successes int var successes int
@ -50,7 +51,7 @@ func BeginDownload(posts *[]Post, saveDirectory *string, maxConcurrents *int) (*
return &successes, &failures, &total return &successes, &failures, &total
} }
func work(wn int, posts []Post, directory string, completed *int, successes *int, failures *int, total *int, wg *sync.WaitGroup) { func work(wn int, posts []e621.Post, directory string, completed *int, successes *int, failures *int, total *int, wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()
for _, post := range posts { for _, post := range posts {
@ -76,7 +77,7 @@ func work(wn int, posts []Post, directory string, completed *int, successes *int
} }
} }
func getSavePath(post *Post, directory *string) string { func getSavePath(post *e621.Post, directory *string) string {
pathSliced := strings.Split(post.FileURL, ".") pathSliced := strings.Split(post.FileURL, ".")
extension := pathSliced[len(pathSliced)-1] extension := pathSliced[len(pathSliced)-1]
@ -85,10 +86,10 @@ func getSavePath(post *Post, directory *string) string {
return savePath return savePath
} }
func downloadPost(post *Post, directory string) error { func downloadPost(post *e621.Post, directory string) error {
savePath := getSavePath(post, &directory) savePath := getSavePath(post, &directory)
resp, err := HTTPGet(post.FileURL) resp, err := e621.HTTPGet(post.FileURL)
if err != nil { if err != nil {
return err return err
} }

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"path" "path"
"github.com/tjhorner/e6dl/concurrent"
"github.com/tjhorner/e6dl/e621" "github.com/tjhorner/e6dl/e621"
) )
@ -38,7 +39,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
successes, failures, _ := e621.BeginDownload(&posts, saveDirectory, maxConcurrents) successes, failures, _ := concurrent.BeginDownload(&posts, saveDirectory, maxConcurrents)
fmt.Printf("\nAll done! %d posts downloaded and saved. (%d failed to download)\n", *successes, *failures) fmt.Printf("\nAll done! %d posts downloaded and saved. (%d failed to download)\n", *successes, *failures)
} }