mirror of
https://codeberg.org/scip/kleingebaeck.git
synced 2025-12-17 04:21:00 +01:00
Add expire support (#44)
This commit is contained in:
13
ad.go
13
ad.go
@@ -20,6 +20,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Index struct {
|
type Index struct {
|
||||||
@@ -37,6 +38,7 @@ type Ad struct {
|
|||||||
Created string `goquery:"#viewad-extra-info,text"`
|
Created string `goquery:"#viewad-extra-info,text"`
|
||||||
Text string `goquery:"p#viewad-description-text,html"`
|
Text string `goquery:"p#viewad-description-text,html"`
|
||||||
Images []string `goquery:".galleryimage-element img,[src]"`
|
Images []string `goquery:".galleryimage-element img,[src]"`
|
||||||
|
Expire string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used by slog to pretty print an ad
|
// Used by slog to pretty print an ad
|
||||||
@@ -49,6 +51,8 @@ func (ad *Ad) LogValue() slog.Value {
|
|||||||
slog.Int("bodysize", len(ad.Text)),
|
slog.Int("bodysize", len(ad.Text)),
|
||||||
slog.String("categorytree", strings.Join(ad.CategoryTree, "+")),
|
slog.String("categorytree", strings.Join(ad.CategoryTree, "+")),
|
||||||
slog.String("condition", ad.Condition),
|
slog.String("condition", ad.Condition),
|
||||||
|
slog.String("created", ad.Created),
|
||||||
|
slog.String("expire", ad.Expire),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,3 +71,12 @@ func (ad *Ad) Incomplete() bool {
|
|||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ad *Ad) CalculateExpire() {
|
||||||
|
if len(ad.Created) > 0 {
|
||||||
|
ts, err := time.Parse("02.01.2006", ad.Created)
|
||||||
|
if err == nil {
|
||||||
|
ad.Expire = ts.AddDate(0, 2, 1).Format("02.01.2006")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -35,14 +35,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VERSION string = "0.1.3"
|
VERSION string = "0.2.0"
|
||||||
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}}\nCreated: {{.Created}}\n\n{{.Text}}\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" +
|
DefaultTemplateWin string = "Title: {{.Title}}\r\nPrice: {{.Price}}\r\nId: {{.Id}}\r\n" +
|
||||||
"Category: {{.Category}}\r\nCondition: {{.Condition}}\r\nCreated: {{.Created}}\r\n\r\n{{.Text}}\r\n"
|
"Category: {{.Category}}\r\nCondition: {{.Condition}}\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) " +
|
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"
|
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
||||||
DefaultAdNameTemplate string = "{{.Slug}}"
|
DefaultAdNameTemplate string = "{{.Slug}}"
|
||||||
|
|||||||
@@ -117,6 +117,8 @@ func ScrapeAd(fetch *Fetcher, uri string) error {
|
|||||||
return errors.New("could not extract ad data from page, got empty struct")
|
return errors.New("could not extract ad data from page, got empty struct")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ad.CalculateExpire()
|
||||||
|
|
||||||
slog.Debug("extracted ad listing", "ad", ad)
|
slog.Debug("extracted ad listing", "ad", ad)
|
||||||
|
|
||||||
// write listing
|
// write listing
|
||||||
|
|||||||
Reference in New Issue
Block a user