Compare commits

..

4 Commits

Author SHA1 Message Date
9cd1fc0596 behavior changes: UserAgent configurable, test cookies, check errors 2024-01-24 19:22:31 +01:00
8df3ebfa6d add throttling to image download 2024-01-24 19:22:31 +01:00
de82127223 first step in fixing #49:
fetch cookies from 1st response and use them in subsequent requests.
2024-01-24 19:22:31 +01:00
a79a28f4a1 add contribution guidelines and non-code-of-conduct 2024-01-23 18:01:14 +01:00
10 changed files with 118 additions and 21 deletions

View File

@@ -35,19 +35,27 @@ import (
) )
const ( const (
VERSION string = "0.3.0" VERSION string = "0.3.1"
Baseuri string = "https://www.kleinanzeigen.de" Baseuri string = "https://www.kleinanzeigen.de"
Listuri string = "/s-bestandsliste.html" Listuri string = "/s-bestandsliste.html"
Defaultdir string = "." Defaultdir string = "."
DefaultTemplate string = "Title: {{.Title}}\nPrice: {{.Price}}\nId: {{.Id}}\n" + DefaultTemplate string = "Title: {{.Title}}\nPrice: {{.Price}}\nId: {{.Id}}\n" +
"Category: {{.Category}}\nCondition: {{.Condition}}\n" + "Category: {{.Category}}\nCondition: {{.Condition}}\n" +
"Created: {{.Created}}\nExpire: {{.Expire}}\n\n{{.Text}}\n" "Created: {{.Created}}\nExpire: {{.Expire}}\n\n{{.Text}}\n"
DefaultTemplateWin string = "Title: {{.Title}}\r\nPrice: {{.Price}}\r\nId: {{.Id}}\r\n" + DefaultTemplateWin string = "Title: {{.Title}}\r\nPrice: {{.Price}}\r\nId: {{.Id}}\r\n" +
"Category: {{.Category}}\r\nCondition: {{.Condition}}\r\n" + "Category: {{.Category}}\r\nCondition: {{.Condition}}\r\n" +
"Created: {{.Created}}\r\nExpires: {{.Expire}}\r\n\r\n{{.Text}}\r\n" "Created: {{.Created}}\r\nExpires: {{.Expire}}\r\n\r\n{{.Text}}\r\n"
Useragent string = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
DefaultUserAgent string = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
DefaultAdNameTemplate string = "{{.Slug}}" DefaultAdNameTemplate string = "{{.Slug}}"
// for image download throttling
MinThrottle int = 2
MaxThrottle int = 20
) )
const Usage string = `This is kleingebaeck, the kleinanzeigen.de backup tool. const Usage string = `This is kleingebaeck, the kleinanzeigen.de backup tool.
@@ -84,6 +92,7 @@ type Config struct {
Limit int `koanf:"limit"` Limit int `koanf:"limit"`
IgnoreErrors bool `koanf:"ignoreerrors"` IgnoreErrors bool `koanf:"ignoreerrors"`
ForceDownload bool `koanf:"force"` ForceDownload bool `koanf:"force"`
UserAgent string `koanf:"useragent"` // conf only
Adlinks []string Adlinks []string
StatsCountAds int StatsCountAds int
StatsCountImages int StatsCountImages int
@@ -114,6 +123,7 @@ func InitConfig(w io.Writer) (*Config, error) {
"loglevel": "notice", "loglevel": "notice",
"userid": 0, "userid": 0,
"adnametemplate": DefaultAdNameTemplate, "adnametemplate": DefaultAdNameTemplate,
"useragent": DefaultUserAgent,
}, "."), nil); err != nil { }, "."), nil); err != nil {
return nil, err return nil, err
} }

View File

@@ -22,21 +22,32 @@ import (
"io" "io"
"log/slog" "log/slog"
"net/http" "net/http"
"net/http/cookiejar"
"net/url"
) )
// convenient wrapper to fetch some web content // convenient wrapper to fetch some web content
type Fetcher struct { type Fetcher struct {
Config *Config Config *Config
Client *http.Client Client *http.Client
Useragent string // FIXME: make configurable Cookies []*http.Cookie
} }
func NewFetcher(c *Config) *Fetcher { func NewFetcher(c *Config) (*Fetcher, error) {
return &Fetcher{ jar, err := cookiejar.New(nil)
Client: &http.Client{Transport: &loggingTransport{}}, // implemented in http.go if err != nil {
Useragent: Useragent, // default in config.go return nil, err
Config: c,
} }
return &Fetcher{
Client: &http.Client{
Transport: &loggingTransport{}, // implemented in http.go
Jar: jar,
},
Config: c,
Cookies: []*http.Cookie{},
},
nil
} }
func (f *Fetcher) Get(uri string) (io.ReadCloser, error) { func (f *Fetcher) Get(uri string) (io.ReadCloser, error) {
@@ -45,7 +56,16 @@ func (f *Fetcher) Get(uri string) (io.ReadCloser, error) {
return nil, err return nil, err
} }
req.Header.Set("User-Agent", f.Useragent) req.Header.Set("User-Agent", f.Config.UserAgent)
if len(f.Cookies) > 0 {
uriobj, _ := url.Parse(Baseuri)
slog.Debug("have cookies, sending them",
"sample-cookie-name", f.Cookies[0].Name,
"sample-cookie-expire", f.Cookies[0].Expires,
)
f.Client.Jar.SetCookies(uriobj, f.Cookies)
}
res, err := f.Client.Do(req) res, err := f.Client.Do(req)
if err != nil { if err != nil {
@@ -56,6 +76,9 @@ func (f *Fetcher) Get(uri string) (io.ReadCloser, error) {
return nil, errors.New("could not get page via HTTP") return nil, errors.New("could not get page via HTTP")
} }
slog.Debug("got cookies?", "cookies", res.Cookies())
f.Cookies = res.Cookies()
return res.Body, nil return res.Body, nil
} }

View File

@@ -133,7 +133,7 @@
.\" ======================================================================== .\" ========================================================================
.\" .\"
.IX Title "KLEINGEBAECK 1" .IX Title "KLEINGEBAECK 1"
.TH KLEINGEBAECK 1 "2024-01-22" "1" "User Commands" .TH KLEINGEBAECK 1 "2024-01-24" "1" "User Commands"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents. .\" way too many mistakes in technical documents.
.if n .ad l .if n .ad l
@@ -174,10 +174,11 @@ well. We use \s-1TOML\s0 as our configuration language. See
.PP .PP
Format is pretty simple: Format is pretty simple:
.PP .PP
.Vb 10 .Vb 11
\& user = 1010101 \& user = 1010101
\& loglevel = verbose \& loglevel = verbose
\& outdir = "test" \& outdir = "test"
\& useragent = "Mozilla/5.0"
\& template = """ \& template = """
\& Title: {{.Title}} \& Title: {{.Title}}
\& Price: {{.Price}} \& Price: {{.Price}}

View File

@@ -39,6 +39,7 @@ CONFIGURATION
user = 1010101 user = 1010101
loglevel = verbose loglevel = verbose
outdir = "test" outdir = "test"
useragent = "Mozilla/5.0"
template = """ template = """
Title: {{.Title}} Title: {{.Title}}
Price: {{.Price}} Price: {{.Price}}

View File

@@ -39,6 +39,7 @@ Format is pretty simple:
user = 1010101 user = 1010101
loglevel = verbose loglevel = verbose
outdir = "test" outdir = "test"
useragent = "Mozilla/5.0"
template = """ template = """
Title: {{.Title}} Title: {{.Title}}
Price: {{.Price}} Price: {{.Price}}

10
main.go
View File

@@ -22,8 +22,10 @@ import (
"fmt" "fmt"
"io" "io"
"log/slog" "log/slog"
"math/rand"
"os" "os"
"runtime/debug" "runtime/debug"
"time"
"github.com/lmittmann/tint" "github.com/lmittmann/tint"
"github.com/tlinden/yadu" "github.com/tlinden/yadu"
@@ -111,7 +113,13 @@ func Main(w io.Writer) int {
} }
// used for all HTTP requests // used for all HTTP requests
fetch := NewFetcher(conf) fetch, err := NewFetcher(conf)
if err != nil {
return Die(err)
}
// randomization needed here and there
rand.Seed(time.Now().UnixNano())
if len(conf.Adlinks) >= 1 { if len(conf.Adlinks) >= 1 {
// directly backup ad listing[s] // directly backup ad listing[s]

View File

@@ -21,6 +21,7 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"net/http"
"os" "os"
"strings" "strings"
"testing" "testing"
@@ -446,19 +447,22 @@ func GetImage(path string) []byte {
// setup httpmock // setup httpmock
func SetIntercept(ads []Adsource) { func SetIntercept(ads []Adsource) {
ch := http.Header{}
ch.Add("Set-Cookie", "session=permanent")
for _, ad := range ads { for _, ad := range ads {
if ad.status == 0 { if ad.status == 0 {
ad.status = 200 ad.status = 200
} }
httpmock.RegisterResponder("GET", ad.uri, httpmock.RegisterResponder("GET", ad.uri,
httpmock.NewStringResponder(ad.status, ad.content)) httpmock.NewStringResponder(ad.status, ad.content).HeaderAdd(ch))
} }
// we just use 2 images, put this here // we just use 2 images, put this here
for _, image := range []string{"t/1.jpg", "t/2.jpg"} { for _, image := range []string{"t/1.jpg", "t/2.jpg"} {
httpmock.RegisterResponder("GET", image, httpmock.RegisterResponder("GET", image,
httpmock.NewBytesResponder(200, GetImage(image))) httpmock.NewBytesResponder(200, GetImage(image)).HeaderAdd(ch))
} }
} }

View File

@@ -24,6 +24,7 @@ import (
"log/slog" "log/slog"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"astuart.co/goq" "astuart.co/goq"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
@@ -150,6 +151,11 @@ func ScrapeImages(fetch *Fetcher, ad *Ad, addir string) error {
imguri := imguri imguri := imguri
file := filepath.Join(adpath, fmt.Sprintf("%d.jpg", img)) file := filepath.Join(adpath, fmt.Sprintf("%d.jpg", img))
g.Go(func() error { g.Go(func() error {
// wait a little
t := GetThrottleTime()
time.Sleep(t)
body, err := fetch.Getimage(imguri) body, err := fetch.Getimage(imguri)
if err != nil { if err != nil {
return err return err
@@ -163,7 +169,7 @@ func ScrapeImages(fetch *Fetcher, ad *Ad, addir string) error {
buf2 := buf.Bytes() // needed for image writing buf2 := buf.Bytes() // needed for image writing
image := NewImage(buf, "", imguri) image := NewImage(buf, file, imguri)
err = image.CalcHash() err = image.CalcHash()
if err != nil { if err != nil {
return err return err
@@ -181,7 +187,7 @@ func ScrapeImages(fetch *Fetcher, ad *Ad, addir string) error {
return err return err
} }
slog.Debug("wrote image", "image", image, "size", len(buf2)) slog.Debug("wrote image", "image", image, "size", len(buf2), "throttle", t)
return nil return nil
}) })
img++ img++

37
store_test.go Normal file
View File

@@ -0,0 +1,37 @@
/*
Copyright © 2023-2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"testing"
)
// this is a weird thing. WriteImage() is being called in scrape.go
// which is being tested by TestMain() in main_test.go. However, it
// doesn't show up in the coverage report for unknown reasons, so
// here's a single test for it
func TestWriteImage(t *testing.T) {
buf := []byte{1, 2, 3, 4, 5, 6, 7, 8}
file := "t/out/t.jpg"
err := WriteImage(file, buf)
if err != nil {
t.Errorf("Could not write mock image to %s: %s", file, err.Error())
}
}

View File

@@ -20,9 +20,11 @@ package main
import ( import (
"bytes" "bytes"
"errors" "errors"
"math/rand"
"os" "os"
"os/exec" "os/exec"
"runtime" "runtime"
"time"
"github.com/mattn/go-isatty" "github.com/mattn/go-isatty"
) )
@@ -66,3 +68,7 @@ func IsNoTty() bool {
// it is a tty // it is a tty
return false return false
} }
func GetThrottleTime() time.Duration {
return time.Duration(rand.Intn(MaxThrottle-MinThrottle+1)+MinThrottle) * time.Millisecond
}