mirror of
https://codeberg.org/scip/kleingebaeck.git
synced 2026-11-23 06:59:00 +01:00
fix #125: could not extract data from subsequent pages
This commit is contained in:
40
scrape.go
40
scrape.go
@@ -20,6 +20,7 @@ package main
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
@@ -32,29 +33,39 @@ 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() {
|
||||
// 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 {
|
||||
@@ -77,12 +88,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 +102,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,15 +123,25 @@ 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() {
|
||||
// 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)
|
||||
@@ -184,14 +205,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))
|
||||
|
||||
egroup.Go(func() error {
|
||||
// wait a little
|
||||
|
||||
throttle := GetThrottleTime()
|
||||
time.Sleep(throttle)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user