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"
|
||||
"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)
|
||||
|
||||
Reference in New Issue
Block a user