mirror of
https://codeberg.org/scip/kleingebaeck.git
synced 2025-12-16 12:01:00 +01:00
Add HTTP retries and the possibility to ignore image download errors (#33)
added HTTP retry and --ignoreerrors which ignores image download errors, fix #30
This commit is contained in:
12
scrape.go
12
scrape.go
@@ -44,9 +44,6 @@ func Get(uri string, client *http.Client) (io.ReadCloser, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
slog.Debug("response", "code", res.StatusCode, "status",
|
||||
res.Status, "size", res.ContentLength)
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
return nil, errors.New("could not get page via HTTP")
|
||||
}
|
||||
@@ -165,11 +162,10 @@ func ScrapeImages(c *Config, ad *Ad, addir string, client *http.Client) error {
|
||||
imguri := imguri
|
||||
file := filepath.Join(c.Outdir, addir, fmt.Sprintf("%d.jpg", img))
|
||||
g.Go(func() error {
|
||||
err := Getimage(imguri, file, client)
|
||||
err := Getimage(c, imguri, file, client)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
slog.Info("wrote ad image", "image", file)
|
||||
|
||||
return nil
|
||||
})
|
||||
@@ -186,10 +182,13 @@ func ScrapeImages(c *Config, ad *Ad, addir string, client *http.Client) error {
|
||||
}
|
||||
|
||||
// fetch an image
|
||||
func Getimage(uri, fileName string, client *http.Client) error {
|
||||
func Getimage(c *Config, uri, fileName string, client *http.Client) error {
|
||||
slog.Debug("fetching ad image", "uri", uri)
|
||||
req, err := http.NewRequest("GET", uri, nil)
|
||||
if err != nil {
|
||||
if c.IgnoreErrors {
|
||||
slog.Info("Failed to download image, error ignored", "error", err.Error())
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -210,5 +209,6 @@ func Getimage(uri, fileName string, client *http.Client) error {
|
||||
return err
|
||||
}
|
||||
|
||||
slog.Info("wrote ad image", "image", fileName)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user