mirror of
https://codeberg.org/scip/kleingebaeck.git
synced 2025-12-17 04:21:00 +01:00
behavior changes: UserAgent configurable, test cookies, check errors
This commit is contained in:
10
config.go
10
config.go
@@ -35,18 +35,22 @@ 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
|
// for image download throttling
|
||||||
@@ -88,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
|
||||||
@@ -118,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
|
||||||
}
|
}
|
||||||
|
|||||||
14
fetch.go
14
fetch.go
@@ -20,7 +20,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
@@ -31,15 +30,13 @@ import (
|
|||||||
type Fetcher struct {
|
type Fetcher struct {
|
||||||
Config *Config
|
Config *Config
|
||||||
Client *http.Client
|
Client *http.Client
|
||||||
Useragent string // FIXME: make configurable
|
|
||||||
Cookies []*http.Cookie
|
Cookies []*http.Cookie
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFetcher(c *Config) *Fetcher {
|
func NewFetcher(c *Config) (*Fetcher, error) {
|
||||||
jar, err := cookiejar.New(nil)
|
jar, err := cookiejar.New(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// cannot return error here, FIXME
|
return nil, err
|
||||||
log.Fatalf("Got error while creating cookie jar %s", err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Fetcher{
|
return &Fetcher{
|
||||||
@@ -47,10 +44,10 @@ func NewFetcher(c *Config) *Fetcher {
|
|||||||
Transport: &loggingTransport{}, // implemented in http.go
|
Transport: &loggingTransport{}, // implemented in http.go
|
||||||
Jar: jar,
|
Jar: jar,
|
||||||
},
|
},
|
||||||
Useragent: Useragent, // default in config.go
|
|
||||||
Config: c,
|
Config: c,
|
||||||
Cookies: []*http.Cookie{},
|
Cookies: []*http.Cookie{},
|
||||||
}
|
},
|
||||||
|
nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher) Get(uri string) (io.ReadCloser, error) {
|
func (f *Fetcher) Get(uri string) (io.ReadCloser, error) {
|
||||||
@@ -59,7 +56,7 @@ 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 {
|
if len(f.Cookies) > 0 {
|
||||||
uriobj, _ := url.Parse(Baseuri)
|
uriobj, _ := url.Parse(Baseuri)
|
||||||
@@ -79,6 +76,7 @@ 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()
|
f.Cookies = res.Cookies()
|
||||||
|
|
||||||
return res.Body, nil
|
return res.Body, nil
|
||||||
|
|||||||
@@ -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}}
|
||||||
|
|||||||
@@ -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}}
|
||||||
|
|||||||
@@ -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}}
|
||||||
|
|||||||
5
main.go
5
main.go
@@ -113,7 +113,10 @@ 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
|
// randomization needed here and there
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
|||||||
@@ -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))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
37
store_test.go
Normal file
37
store_test.go
Normal 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())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user