Fix scraping multiple ads (#126)

Upstream has changed their content generation system. When using the same http client, the first page looks as we expect it, but subsequent pages look totally different. I can't find out why that is, but it's not an issue of this tool.

So, the "fix" is to abandon client reuse and re-initialize a new client for every http request. However, the image fetcher keeps resusing the client.

Co-authored-by: Thomas von Dein <tom@vondein.org>
Reviewed-on: https://codeberg.org/scip/kleingebaeck/pulls/126
This commit is contained in:
T. von Dein
2026-10-12 21:29:49 +02:00
parent 0d5ee0b485
commit d9f2f0a712
15 changed files with 251 additions and 69 deletions

View File

@@ -19,7 +19,9 @@ package main
import (
"bytes"
"errors"
"fmt"
"io"
"log/slog"
"path/filepath"
"strconv"
@@ -32,30 +34,44 @@ import (
// extract links from all ad listing pages (that is: use pagination)
// and scrape every page
func ScrapeUser(fetch *Fetcher) error {
func ScrapeUser(conf *Config) error {
adlinks := []string{}
baseuri := fmt.Sprintf("%s%s?userId=%d", Baseuri, Listuri, fetch.Config.User)
baseuri := fmt.Sprintf("%s%s?userId=%d", Baseuri, Listuri, conf.User)
page := 1
uri := baseuri
slog.Info("fetching ad pages", "user", fetch.Config.User)
slog.Info("fetching ad pages", "user", conf.User)
for {
var index Index
slog.Debug("fetching page", "uri", uri)
body, err := fetch.Get(uri)
fetch, err := NewFetcher(conf)
if err != nil {
return err
}
body, err := fetch.Get(uri)
defer func() {
if body == nil {
return
}
// make sure body is completely flushed
if _, err := io.Copy(io.Discard, body); err != nil {
panic(err)
}
if err := body.Close(); err != nil {
panic(err)
}
}()
if err != nil {
return err
}
err = goq.NewDecoder(body).Decode(&index)
if err != nil {
return fmt.Errorf("failed to goquery decode HTML index body: %w", err)
@@ -77,12 +93,12 @@ func ScrapeUser(fetch *Fetcher) error {
}
for index, adlink := range adlinks {
err := ScrapeAd(fetch, Baseuri+adlink)
err := ScrapeAd(conf, Baseuri+adlink)
if err != nil {
return err
}
if fetch.Config.Limit > 0 && index == fetch.Config.Limit-1 {
if conf.Limit > 0 && index == conf.Limit-1 {
break
}
}
@@ -91,7 +107,7 @@ func ScrapeUser(fetch *Fetcher) error {
}
// scrape an ad. uri is the full uri of the ad, dir is the basedir
func ScrapeAd(fetch *Fetcher, uri string) error {
func ScrapeAd(conf *Config, uri string) error {
now := time.Now()
advertisement := &Ad{
@@ -112,16 +128,31 @@ func ScrapeAd(fetch *Fetcher, uri string) error {
// get the ad
slog.Debug("fetching ad page", "uri", uri)
body, err := fetch.Get(uri)
fetch, err := NewFetcher(conf)
if err != nil {
return err
}
body, err := fetch.Get(uri)
defer func() {
if body == nil {
return
}
// make sure body is completely flushed
if _, err := io.Copy(io.Discard, body); err != nil {
panic(err)
}
if err := body.Close(); err != nil {
panic(err)
}
}()
if err != nil {
return err
}
// extract ad contents with goquery/goq
err = goq.NewDecoder(body).Decode(&advertisement)
if err != nil {
@@ -135,7 +166,7 @@ func ScrapeAd(fetch *Fetcher, uri string) error {
if advertisement.Incomplete() {
slog.Debug("got ad", "ad", advertisement)
return fmt.Errorf("could not extract ad data from page, got empty struct")
return errors.New("could not extract ad data from page, got empty struct")
}
advertisement.DecodeAttributes()
@@ -184,14 +215,11 @@ func ScrapeImages(fetch *Fetcher, advertisement *Ad, addir string) error {
egroup := new(errgroup.Group)
for _, imguri := range advertisement.Images {
imguri := imguri
// we append the suffix later in NewImage() based on image format
basefilename := filepath.Join(adpath, fmt.Sprintf("%d", img))
basefilename := filepath.Join(adpath, strconv.Itoa(img))
egroup.Go(func() error {
// wait a little
throttle := GetThrottleTime()
time.Sleep(throttle)
@@ -241,6 +269,7 @@ func ScrapeImages(fetch *Fetcher, advertisement *Ad, addir string) error {
return nil
})
img++
}