Compare commits

..

2 Commits

12 changed files with 43 additions and 63 deletions

View File

@@ -63,7 +63,6 @@ lint:
lint-full: lint-full:
golangci-lint run --enable-all --exclude-use-default --disable exhaustivestruct,exhaustruct,depguard,interfacer,deadcode,golint,structcheck,scopelint,varcheck,ifshort,maligned,nosnakecase,godot,funlen,gofumpt,cyclop,noctx,gochecknoglobals,paralleltest golangci-lint run --enable-all --exclude-use-default --disable exhaustivestruct,exhaustruct,depguard,interfacer,deadcode,golint,structcheck,scopelint,varcheck,ifshort,maligned,nosnakecase,godot,funlen,gofumpt,cyclop,noctx,gochecknoglobals,paralleltest
gocritic check -enableAll *.go
testfuzzy: clean testfuzzy: clean
go test -fuzz ./... $(ARGS) go test -fuzz ./... $(ARGS)

View File

@@ -1,17 +0,0 @@
# Security Policy
## Supported Versions
Only the latest release is supported. If you find an issue (any
issue!), please check with the latest release first.
## Reporting a Vulnerability
I don't agree with the "responsible disclosure" process most projects
(and companies) work these days.
So, if you find a vulnerability of any kind, please just open an
[issue](https://github.com/TLINDEN/kleingebaeck/issues). Please add
all details required to reproduce the vulnerability. You won't be chased.
That's just all about it.

2
ad.go
View File

@@ -73,7 +73,7 @@ func (ad *Ad) Incomplete() bool {
} }
func (ad *Ad) CalculateExpire() { func (ad *Ad) CalculateExpire() {
if ad.Created != "" { if len(ad.Created) > 0 {
ts, err := time.Parse("02.01.2006", ad.Created) ts, err := time.Parse("02.01.2006", ad.Created)
if err == nil { if err == nil {
ad.Expire = ts.AddDate(0, ExpireMonths, ExpireDays).Format("02.01.2006") ad.Expire = ts.AddDate(0, ExpireMonths, ExpireDays).Format("02.01.2006")

View File

@@ -34,7 +34,7 @@ import (
) )
const ( const (
VERSION string = "0.3.5" VERSION string = "0.3.4"
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 = "."

View File

@@ -52,7 +52,7 @@ func NewFetcher(conf *Config) (*Fetcher, error) {
} }
func (f *Fetcher) Get(uri string) (io.ReadCloser, error) { func (f *Fetcher) Get(uri string) (io.ReadCloser, error) {
req, err := http.NewRequest(http.MethodGet, uri, http.NoBody) req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create a new HTTP request obj: %w", err) return nil, fmt.Errorf("failed to create a new HTTP request obj: %w", err)
} }

View File

@@ -49,7 +49,7 @@ func (img *Image) LogValue() slog.Value {
// holds all images of an ad // holds all images of an ad
type Cache []*goimagehash.ImageHash type Cache []*goimagehash.ImageHash
func NewImage(buf *bytes.Reader, filename, uri string) *Image { func NewImage(buf *bytes.Reader, filename string, uri string) *Image {
img := &Image{ img := &Image{
Filename: filename, Filename: filename,
URI: uri, URI: uri,
@@ -134,7 +134,7 @@ func ReadImages(addir string, dont bool) (Cache, error) {
reader := bytes.NewReader(data.Bytes()) reader := bytes.NewReader(data.Bytes())
img := NewImage(reader, filename, "") img := NewImage(reader, filename, "")
if err := img.CalcHash(); err != nil { if err = img.CalcHash(); err != nil {
return nil, err return nil, err
} }

View File

@@ -116,6 +116,11 @@ func Main(output io.Writer) int {
if err != nil { if err != nil {
return Die(err) return Die(err)
} }
err = Mkdir(outdir)
if err != nil {
return Die(err)
}
conf.Outdir = outdir conf.Outdir = outdir
// used for all HTTP requests // used for all HTTP requests

View File

@@ -334,14 +334,14 @@ type Adsource struct {
} }
// Render a HTML template for an adlisting or an ad // Render a HTML template for an adlisting or an ad
func GetTemplate(adconfigs []AdConfig, adconfig *AdConfig, htmltemplate string) string { func GetTemplate(adconfigs []AdConfig, adconfig AdConfig, htmltemplate string) string {
tmpl, err := tpl.New("template").Parse(htmltemplate) tmpl, err := tpl.New("template").Parse(htmltemplate)
if err != nil { if err != nil {
panic(err) panic(err)
} }
var out bytes.Buffer var out bytes.Buffer
if adconfig.ID == "" { if len(adconfig.ID) == 0 {
err = tmpl.Execute(&out, adconfigs) err = tmpl.Execute(&out, adconfigs)
} else { } else {
err = tmpl.Execute(&out, adconfig) err = tmpl.Execute(&out, adconfig)
@@ -376,15 +376,15 @@ func InitValidSources() []Adsource {
ads := []Adsource{ ads := []Adsource{
{ {
uri: fmt.Sprintf("%s%s?userId=1", Baseuri, Listuri), uri: fmt.Sprintf("%s%s?userId=1", Baseuri, Listuri),
content: GetTemplate(list1, &empty, LISTTPL), content: GetTemplate(list1, empty, LISTTPL),
}, },
{ {
uri: fmt.Sprintf("%s%s?userId=1&pageNum=2", Baseuri, Listuri), uri: fmt.Sprintf("%s%s?userId=1&pageNum=2", Baseuri, Listuri),
content: GetTemplate(list2, &empty, LISTTPL), content: GetTemplate(list2, empty, LISTTPL),
}, },
{ {
uri: fmt.Sprintf("%s%s?userId=1&pageNum=3", Baseuri, Listuri), uri: fmt.Sprintf("%s%s?userId=1&pageNum=3", Baseuri, Listuri),
content: GetTemplate(list3, &empty, LISTTPL), content: GetTemplate(list3, empty, LISTTPL),
}, },
} }
@@ -392,7 +392,7 @@ func InitValidSources() []Adsource {
for _, ad := range adsrc { for _, ad := range adsrc {
ads = append(ads, Adsource{ ads = append(ads, Adsource{
uri: fmt.Sprintf("%s/s-anzeige/%s/%s", Baseuri, ad.Slug, ad.ID), uri: fmt.Sprintf("%s/s-anzeige/%s/%s", Baseuri, ad.Slug, ad.ID),
content: GetTemplate(nil, &ad, ADTPL), content: GetTemplate(nil, ad, ADTPL),
}) })
} }
@@ -405,28 +405,28 @@ func InitInvalidSources() []Adsource {
{ {
// valid ad page but without content // valid ad page but without content
uri: fmt.Sprintf("%s/s-anzeige/empty/1", Baseuri), uri: fmt.Sprintf("%s/s-anzeige/empty/1", Baseuri),
content: GetTemplate(nil, &empty, EMPTYPAGE), content: GetTemplate(nil, empty, EMPTYPAGE),
}, },
{ {
// some random foreign webpage // some random foreign webpage
uri: INVALIDURI, uri: INVALIDURI,
content: GetTemplate(nil, &empty, "<html>foo</html>"), content: GetTemplate(nil, empty, "<html>foo</html>"),
}, },
{ {
// some invalid page path // some invalid page path
uri: fmt.Sprintf("%s/anzeige/name/1", Baseuri), uri: fmt.Sprintf("%s/anzeige/name/1", Baseuri),
content: GetTemplate(nil, &empty, "<html></html>"), content: GetTemplate(nil, empty, "<html></html>"),
}, },
{ {
// some none-ad page // some none-ad page
uri: fmt.Sprintf("%s/anzeige/name/1/foo/bar", Baseuri), uri: fmt.Sprintf("%s/anzeige/name/1/foo/bar", Baseuri),
content: GetTemplate(nil, &empty, "<html>HTTP 404: /eine-anzeige/ does not exist!</html>"), content: GetTemplate(nil, empty, "<html>HTTP 404: /eine-anzeige/ does not exist!</html>"),
status: 404, status: 404,
}, },
{ {
// valid ad page but 503 // valid ad page but 503
uri: fmt.Sprintf("%s/s-anzeige/503/1", Baseuri), uri: fmt.Sprintf("%s/s-anzeige/503/1", Baseuri),
content: GetTemplate(nil, &empty, "<html>HTTP 503: service unavailable</html>"), content: GetTemplate(nil, empty, "<html>HTTP 503: service unavailable</html>"),
status: 503, status: 503,
}, },
} }
@@ -465,7 +465,7 @@ func SetIntercept(ads []Adsource) {
} }
} }
func VerifyAd(advertisement *AdConfig) error { func VerifyAd(advertisement AdConfig) error {
body := advertisement.Title + advertisement.Price + advertisement.ID + "Kleinanzeigen => " + body := advertisement.Title + advertisement.Price + advertisement.ID + "Kleinanzeigen => " +
advertisement.Category + advertisement.Condition + advertisement.Created advertisement.Category + advertisement.Condition + advertisement.Created
@@ -525,7 +525,7 @@ func TestMain(t *testing.T) {
// verify if downloaded ads match // verify if downloaded ads match
for _, ad := range adsrc { for _, ad := range adsrc {
if err := VerifyAd(&ad); err != nil { if err := VerifyAd(ad); err != nil {
t.Errorf(err.Error()) t.Errorf(err.Error())
} }
} }

View File

@@ -22,12 +22,7 @@ freebsd/amd64
linux/amd64 linux/amd64
netbsd/amd64 netbsd/amd64
openbsd/amd64 openbsd/amd64
windows/amd64 windows/amd64"
freebsd/arm64
linux/arm64
netbsd/arm64
openbsd/arm64
windows/arm64"
tool="$1" tool="$1"
version="$2" version="$2"

View File

@@ -126,32 +126,21 @@ func ScrapeAd(fetch *Fetcher, uri string) error {
advertisement.CalculateExpire() advertisement.CalculateExpire()
// prepare ad dir name proceed := CheckAdVisited(fetch.Config, advertisement.Slug)
addir, err := AdDirName(fetch.Config, advertisement)
if err != nil {
return err
}
proceed := CheckAdVisited(fetch.Config, addir)
if !proceed { if !proceed {
return nil return nil
} }
// write listing // write listing
err = WriteAd(fetch.Config, advertisement, addir) addir, err := WriteAd(fetch.Config, advertisement)
if err != nil { if err != nil {
return err return err
} }
// tell the user
slog.Debug("extracted ad listing", "ad", advertisement) slog.Debug("extracted ad listing", "ad", advertisement)
// stats
fetch.Config.IncrAds() fetch.Config.IncrAds()
// register for later checks
DirsVisited[addir] = 1
return ScrapeImages(fetch, advertisement, addir) return ScrapeImages(fetch, advertisement, addir)
} }

View File

@@ -72,13 +72,19 @@ func AdDirName(conf *Config, advertisement *Ad) (string, error) {
return buf.String(), nil return buf.String(), nil
} }
func WriteAd(conf *Config, advertisement *Ad, addir string) error { func WriteAd(conf *Config, advertisement *Ad) (string, error) {
// prepare ad dir name
addir, err := AdDirName(conf, advertisement)
if err != nil {
return "", err
}
// prepare output dir // prepare output dir
dir := filepath.Join(conf.Outdir, addir) dir := filepath.Join(conf.Outdir, addir)
err := Mkdir(dir) err = Mkdir(dir)
if err != nil { if err != nil {
return err return "", err
} }
// write ad file // write ad file
@@ -86,7 +92,7 @@ func WriteAd(conf *Config, advertisement *Ad, addir string) error {
listingfd, err := os.Create(listingfile) listingfd, err := os.Create(listingfile)
if err != nil { if err != nil {
return fmt.Errorf("failed to create Adlisting.txt: %w", err) return "", fmt.Errorf("failed to create Adlisting.txt: %w", err)
} }
defer listingfd.Close() defer listingfd.Close()
@@ -98,17 +104,17 @@ func WriteAd(conf *Config, advertisement *Ad, addir string) error {
tmpl, err := tpl.New("adlisting").Parse(conf.Template) tmpl, err := tpl.New("adlisting").Parse(conf.Template)
if err != nil { if err != nil {
return fmt.Errorf("failed to parse adlisting template: %w", err) return "", fmt.Errorf("failed to parse adlisting template: %w", err)
} }
err = tmpl.Execute(listingfd, advertisement) err = tmpl.Execute(listingfd, advertisement)
if err != nil { if err != nil {
return fmt.Errorf("failed to execute adlisting template: %w", err) return "", fmt.Errorf("failed to execute adlisting template: %w", err)
} }
slog.Info("wrote ad listing", "listingfile", listingfile) slog.Info("wrote ad listing", "listingfile", listingfile)
return nil return addir, nil
} }
func WriteImage(filename string, reader *bytes.Reader) error { func WriteImage(filename string, reader *bytes.Reader) error {
@@ -170,6 +176,9 @@ func CheckAdVisited(conf *Config, adname string) bool {
return false return false
} }
// register
DirsVisited[adname] = 1
// overwrite // overwrite
return true return true
} }

View File

@@ -32,7 +32,7 @@ import (
func Mkdir(dir string) error { func Mkdir(dir string) error {
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) { if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
err := os.MkdirAll(dir, os.ModePerm) err := os.Mkdir(dir, os.ModePerm)
if err != nil { if err != nil {
return fmt.Errorf("failed to create directory %s: %w", dir, err) return fmt.Errorf("failed to create directory %s: %w", dir, err)
} }