2020-10-31 11:02:59 +00:00
|
|
|
package workers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/deletescape/goop/internal/utils"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"sync"
|
2020-11-24 08:00:07 +00:00
|
|
|
"time"
|
2020-10-31 11:02:59 +00:00
|
|
|
)
|
|
|
|
|
2020-11-24 08:00:07 +00:00
|
|
|
func DownloadWorker(c *fasthttp.Client, queue chan string, baseUrl, baseDir string, wg *sync.WaitGroup, allowHtml bool) {
|
2020-10-31 11:02:59 +00:00
|
|
|
defer wg.Done()
|
2020-11-24 08:00:07 +00:00
|
|
|
var ctr int
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case file := <-queue:
|
|
|
|
|
|
|
|
checkRatelimted()
|
|
|
|
if file == "" {
|
2020-10-31 11:02:59 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-11-24 08:00:07 +00:00
|
|
|
targetFile := utils.Url(baseDir, file)
|
|
|
|
if utils.Exists(targetFile) {
|
|
|
|
fmt.Printf("%s was downloaded already, skipping\n", targetFile)
|
2020-11-12 13:32:12 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-11-24 08:00:07 +00:00
|
|
|
uri := utils.Url(baseUrl, file)
|
|
|
|
code, body, err := c.Get(nil, uri)
|
|
|
|
fmt.Printf("[-] Fetching %s [%d]\n", uri, code)
|
|
|
|
if err != nil {
|
2020-10-31 11:02:59 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "error: %s\n", err)
|
|
|
|
continue
|
|
|
|
}
|
2020-11-24 08:00:07 +00:00
|
|
|
if code == 200 {
|
|
|
|
if !allowHtml && utils.IsHtml(body) {
|
|
|
|
fmt.Printf("warning: %s appears to be an html file, skipping\n", uri)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if utils.IsEmptyBytes(body) {
|
|
|
|
fmt.Printf("warning: %s appears to be an empty file, skipping\n", uri)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := utils.CreateParentFolders(targetFile); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %s\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(targetFile, body, os.ModePerm); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %s\n", err)
|
|
|
|
}
|
|
|
|
} else if code == 429 {
|
|
|
|
setRatelimited()
|
|
|
|
queue <- file
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// TODO: get rid of dirty hack somehow
|
|
|
|
if ctr >= graceTimes {
|
|
|
|
return
|
2020-10-31 11:02:59 +00:00
|
|
|
}
|
2020-11-24 08:00:07 +00:00
|
|
|
ctr++
|
|
|
|
time.Sleep(gracePeriod)
|
2020-10-31 11:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|