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:
44
fetch.go
44
fetch.go
@@ -24,7 +24,11 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// convenient wrapper to fetch some web content
|
// convenient wrapper to fetch some web content
|
||||||
@@ -34,12 +38,24 @@ type Fetcher struct {
|
|||||||
Cookies []*http.Cookie
|
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) {
|
func NewFetcher(conf *Config) (*Fetcher, error) {
|
||||||
jar, err := cookiejar.New(nil)
|
jar, err := cookiejar.New(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to create a cookie jar obj: %w", err)
|
return nil, fmt.Errorf("failed to create a cookie jar obj: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if pos == nil {
|
||||||
|
pos = &Pos{}
|
||||||
|
}
|
||||||
|
|
||||||
return &Fetcher{
|
return &Fetcher{
|
||||||
Client: &http.Client{
|
Client: &http.Client{
|
||||||
Transport: &loggingTransport{}, // implemented in http.go
|
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())
|
slog.Debug("got cookies?", "cookies", res.Cookies())
|
||||||
f.Cookies = res.Cookies()
|
f.Cookies = res.Cookies()
|
||||||
|
|
||||||
|
f.Dump(res)
|
||||||
|
|
||||||
return res.Body, nil
|
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
|
// fetch an image
|
||||||
func (f *Fetcher) Getimage(uri string) (io.ReadCloser, error) {
|
func (f *Fetcher) Getimage(uri string) (io.ReadCloser, error) {
|
||||||
slog.Debug("fetching ad image", "uri", uri)
|
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
|
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
|
// setup ad dir registry, needed to check for duplicates
|
||||||
DirsVisited = make(map[string]int)
|
DirsVisited = make(map[string]int)
|
||||||
|
|
||||||
@@ -159,14 +153,14 @@ func Main(output io.Writer) int {
|
|||||||
case len(conf.Adlinks) >= 1:
|
case len(conf.Adlinks) >= 1:
|
||||||
// directly backup ad listing[s]
|
// directly backup ad listing[s]
|
||||||
for _, uri := range conf.Adlinks {
|
for _, uri := range conf.Adlinks {
|
||||||
err := ScrapeAd(fetch, uri)
|
err = ScrapeAd(conf, uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Die(err)
|
return Die(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case conf.User > 0:
|
case conf.User > 0:
|
||||||
// backup all ads of the given user (via config or cmdline)
|
// backup all ads of the given user (via config or cmdline)
|
||||||
err := ScrapeUser(fetch)
|
err := ScrapeUser(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Die(err)
|
return Die(err)
|
||||||
}
|
}
|
||||||
|
|||||||
40
scrape.go
40
scrape.go
@@ -20,6 +20,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -32,29 +33,39 @@ import (
|
|||||||
|
|
||||||
// extract links from all ad listing pages (that is: use pagination)
|
// extract links from all ad listing pages (that is: use pagination)
|
||||||
// and scrape every page
|
// and scrape every page
|
||||||
func ScrapeUser(fetch *Fetcher) error {
|
func ScrapeUser(conf *Config) error {
|
||||||
adlinks := []string{}
|
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
|
page := 1
|
||||||
uri := baseuri
|
uri := baseuri
|
||||||
|
|
||||||
slog.Info("fetching ad pages", "user", fetch.Config.User)
|
slog.Info("fetching ad pages", "user", conf.User)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
var index Index
|
var index Index
|
||||||
|
|
||||||
slog.Debug("fetching page", "uri", uri)
|
slog.Debug("fetching page", "uri", uri)
|
||||||
|
|
||||||
body, err := fetch.Get(uri)
|
fetch, err := NewFetcher(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
body, err := fetch.Get(uri)
|
||||||
defer func() {
|
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 {
|
if err := body.Close(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
err = goq.NewDecoder(body).Decode(&index)
|
err = goq.NewDecoder(body).Decode(&index)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -77,12 +88,12 @@ func ScrapeUser(fetch *Fetcher) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for index, adlink := range adlinks {
|
for index, adlink := range adlinks {
|
||||||
err := ScrapeAd(fetch, Baseuri+adlink)
|
err := ScrapeAd(conf, Baseuri+adlink)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if fetch.Config.Limit > 0 && index == fetch.Config.Limit-1 {
|
if conf.Limit > 0 && index == conf.Limit-1 {
|
||||||
break
|
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
|
// 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()
|
now := time.Now()
|
||||||
|
|
||||||
advertisement := &Ad{
|
advertisement := &Ad{
|
||||||
@@ -112,15 +123,25 @@ func ScrapeAd(fetch *Fetcher, uri string) error {
|
|||||||
// get the ad
|
// get the ad
|
||||||
slog.Debug("fetching ad page", "uri", uri)
|
slog.Debug("fetching ad page", "uri", uri)
|
||||||
|
|
||||||
body, err := fetch.Get(uri)
|
fetch, err := NewFetcher(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
body, err := fetch.Get(uri)
|
||||||
defer func() {
|
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 {
|
if err := body.Close(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// extract ad contents with goquery/goq
|
// extract ad contents with goquery/goq
|
||||||
err = goq.NewDecoder(body).Decode(&advertisement)
|
err = goq.NewDecoder(body).Decode(&advertisement)
|
||||||
@@ -184,14 +205,11 @@ func ScrapeImages(fetch *Fetcher, advertisement *Ad, addir string) error {
|
|||||||
egroup := new(errgroup.Group)
|
egroup := new(errgroup.Group)
|
||||||
|
|
||||||
for _, imguri := range advertisement.Images {
|
for _, imguri := range advertisement.Images {
|
||||||
imguri := imguri
|
|
||||||
|
|
||||||
// we append the suffix later in NewImage() based on image format
|
// we append the suffix later in NewImage() based on image format
|
||||||
basefilename := filepath.Join(adpath, fmt.Sprintf("%d", img))
|
basefilename := filepath.Join(adpath, fmt.Sprintf("%d", img))
|
||||||
|
|
||||||
egroup.Go(func() error {
|
egroup.Go(func() error {
|
||||||
// wait a little
|
// wait a little
|
||||||
|
|
||||||
throttle := GetThrottleTime()
|
throttle := GetThrottleTime()
|
||||||
time.Sleep(throttle)
|
time.Sleep(throttle)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user