mirror of
https://codeberg.org/scip/kleingebaeck.git
synced 2025-12-16 12:01:00 +01:00
get rid of duplicate bytes.Buffer, use bytes.Reader instead, #39
This commit is contained in:
2
ad.go
2
ad.go
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Copyright © 2023 Thomas von Dein
|
Copyright © 2023-2024 Thomas von Dein
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
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
|
it under the terms of the GNU General Public License as published by
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VERSION string = "0.3.2"
|
VERSION string = "0.3.3"
|
||||||
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 = "."
|
||||||
|
|||||||
8
image.go
8
image.go
@@ -33,7 +33,7 @@ const MaxDistance = 3
|
|||||||
type Image struct {
|
type Image struct {
|
||||||
Filename string
|
Filename string
|
||||||
Hash *goimagehash.ImageHash
|
Hash *goimagehash.ImageHash
|
||||||
Data *bytes.Buffer
|
Data *bytes.Reader
|
||||||
URI string
|
URI string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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.Buffer, filename string, 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,
|
||||||
@@ -131,7 +131,9 @@ func ReadImages(addir string, dont bool) (Cache, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
img := NewImage(data, filename, "")
|
reader := bytes.NewReader(data.Bytes())
|
||||||
|
|
||||||
|
img := NewImage(reader, filename, "")
|
||||||
if err = img.CalcHash(); err != nil {
|
if err = img.CalcHash(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
10
scrape.go
10
scrape.go
@@ -168,14 +168,15 @@ func ScrapeImages(fetch *Fetcher, advertisement *Ad, addir string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
_, err = buf.ReadFrom(body)
|
_, err = buf.ReadFrom(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to read from image buffer: %w", err)
|
return fmt.Errorf("failed to read from image buffer: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf2 := buf.Bytes() // needed for image writing
|
reader := bytes.NewReader(buf.Bytes())
|
||||||
|
|
||||||
image := NewImage(buf, file, imguri)
|
image := NewImage(reader, file, imguri)
|
||||||
err = image.CalcHash()
|
err = image.CalcHash()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -189,12 +190,13 @@ func ScrapeImages(fetch *Fetcher, advertisement *Ad, addir string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = WriteImage(file, buf2)
|
reader.Seek(0, 0)
|
||||||
|
err = WriteImage(file, reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Debug("wrote image", "image", image, "size", len(buf2), "throttle", throttle)
|
slog.Debug("wrote image", "image", image, "size", buf.Len(), "throttle", throttle)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|||||||
4
store.go
4
store.go
@@ -89,14 +89,14 @@ func WriteAd(conf *Config, advertisement *Ad) (string, error) {
|
|||||||
return addir, nil
|
return addir, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteImage(filename string, buf []byte) error {
|
func WriteImage(filename string, reader *bytes.Reader) error {
|
||||||
file, err := os.Create(filename)
|
file, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to open image file: %w", err)
|
return fmt.Errorf("failed to open image file: %w", err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
_, err = file.Write(buf)
|
_, err = reader.WriteTo(file)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to write to image file: %w", err)
|
return fmt.Errorf("failed to write to image file: %w", err)
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -28,10 +29,10 @@ import (
|
|||||||
func TestWriteImage(t *testing.T) {
|
func TestWriteImage(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
buf := []byte{1, 2, 3, 4, 5, 6, 7, 8}
|
reader := bytes.NewReader([]byte{1, 2, 3, 4, 5, 6, 7, 8})
|
||||||
file := "t/out/t.jpg"
|
file := "t/out/t.jpg"
|
||||||
|
|
||||||
err := WriteImage(file, buf)
|
err := WriteImage(file, reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Could not write mock image to %s: %s", file, err.Error())
|
t.Errorf("Could not write mock image to %s: %s", file, err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
2
util.go
2
util.go
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Copyright © 2023 Thomas von Dein
|
Copyright © 2023-2024 Thomas von Dein
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
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
|
it under the terms of the GNU General Public License as published by
|
||||||
|
|||||||
Reference in New Issue
Block a user