mirror of
https://codeberg.org/scip/kleingebaeck.git
synced 2026-11-22 22:48:59 +01:00
fix #125: could not extract data from subsequent pages
This commit is contained in:
44
fetch.go
44
fetch.go
@@ -24,7 +24,11 @@ import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// convenient wrapper to fetch some web content
|
||||
@@ -34,12 +38,24 @@ type Fetcher struct {
|
||||
Cookies []*http.Cookie
|
||||
}
|
||||
|
||||
// Pos is a counter for debug output file names
|
||||
type Pos struct {
|
||||
sync.Mutex
|
||||
pos int
|
||||
}
|
||||
|
||||
var pos *Pos
|
||||
|
||||
func NewFetcher(conf *Config) (*Fetcher, error) {
|
||||
jar, err := cookiejar.New(nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create a cookie jar obj: %w", err)
|
||||
}
|
||||
|
||||
if pos == nil {
|
||||
pos = &Pos{}
|
||||
}
|
||||
|
||||
return &Fetcher{
|
||||
Client: &http.Client{
|
||||
Transport: &loggingTransport{}, // implemented in http.go
|
||||
@@ -82,9 +98,37 @@ func (f *Fetcher) Get(uri string) (io.ReadCloser, error) {
|
||||
slog.Debug("got cookies?", "cookies", res.Cookies())
|
||||
f.Cookies = res.Cookies()
|
||||
|
||||
f.Dump(res)
|
||||
|
||||
return res.Body, nil
|
||||
}
|
||||
|
||||
func (f *Fetcher) Dump(res *http.Response) error {
|
||||
if f.Config.Debug && strings.Contains(res.Header.Get("Content-Type"), "text/html") {
|
||||
// => dump content
|
||||
pos.Lock()
|
||||
defer pos.Unlock()
|
||||
|
||||
pos.pos++
|
||||
dump, err := httputil.DumpResponse(res, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file := fmt.Sprintf("out%d.html", pos.pos)
|
||||
fd, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE, 0600)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fd.Write(dump)
|
||||
|
||||
slog.Debug("dump content", "file", file)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// fetch an image
|
||||
func (f *Fetcher) Getimage(uri string) (io.ReadCloser, error) {
|
||||
slog.Debug("fetching ad image", "uri", uri)
|
||||
|
||||
10
main.go
10
main.go
@@ -146,12 +146,6 @@ func Main(output io.Writer) int {
|
||||
}
|
||||
conf.Outdir = outdir
|
||||
|
||||
// used for all HTTP requests
|
||||
fetch, err := NewFetcher(conf)
|
||||
if err != nil {
|
||||
return Die(err)
|
||||
}
|
||||
|
||||
// setup ad dir registry, needed to check for duplicates
|
||||
DirsVisited = make(map[string]int)
|
||||
|
||||
@@ -159,14 +153,14 @@ func Main(output io.Writer) int {
|
||||
case len(conf.Adlinks) >= 1:
|
||||
// directly backup ad listing[s]
|
||||
for _, uri := range conf.Adlinks {
|
||||
err := ScrapeAd(fetch, uri)
|
||||
err = ScrapeAd(conf, uri)
|
||||
if err != nil {
|
||||
return Die(err)
|
||||
}
|
||||
}
|
||||
case conf.User > 0:
|
||||
// backup all ads of the given user (via config or cmdline)
|
||||
err := ScrapeUser(fetch)
|
||||
err := ScrapeUser(conf)
|
||||
if err != nil {
|
||||
return Die(err)
|
||||
}
|
||||
|
||||
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