Compare commits

..

1 Commits

Author SHA1 Message Date
e346356431 add contribution guidelines and non-code-of-conduct 2024-01-23 17:59:33 +01:00
10 changed files with 21 additions and 118 deletions

View File

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

View File

@@ -22,32 +22,21 @@ import (
"io"
"log/slog"
"net/http"
"net/http/cookiejar"
"net/url"
)
// convenient wrapper to fetch some web content
type Fetcher struct {
Config *Config
Client *http.Client
Cookies []*http.Cookie
}
func NewFetcher(c *Config) (*Fetcher, error) {
jar, err := cookiejar.New(nil)
if err != nil {
return nil, err
Useragent string // FIXME: make configurable
}
func NewFetcher(c *Config) *Fetcher {
return &Fetcher{
Client: &http.Client{
Transport: &loggingTransport{}, // implemented in http.go
Jar: jar,
},
Client: &http.Client{Transport: &loggingTransport{}}, // implemented in http.go
Useragent: Useragent, // default in config.go
Config: c,
Cookies: []*http.Cookie{},
},
nil
}
}
func (f *Fetcher) Get(uri string) (io.ReadCloser, error) {
@@ -56,16 +45,7 @@ func (f *Fetcher) Get(uri string) (io.ReadCloser, error) {
return nil, err
}
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)
}
req.Header.Set("User-Agent", f.Useragent)
res, err := f.Client.Do(req)
if err != nil {
@@ -76,9 +56,6 @@ func (f *Fetcher) Get(uri string) (io.ReadCloser, error) {
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
}

View File

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

View File

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

View File

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

10
main.go
View File

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

View File

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

View File

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

View File

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