mirror of
https://codeberg.org/scip/kleingebaeck.git
synced 2025-12-17 04:21:00 +01:00
attempt to fix #117. code works, tests are still failing
This commit is contained in:
42
scrape.go
42
scrape.go
@@ -18,11 +18,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -125,16 +125,7 @@ func ScrapeAd(fetch *Fetcher, uri string) error {
|
||||
return fmt.Errorf("could not extract ad data from page, got empty struct")
|
||||
}
|
||||
|
||||
for _, detail := range advertisement.Details {
|
||||
switch {
|
||||
case slices.Contains(CONDITIONS, detail):
|
||||
advertisement.Condition = detail
|
||||
case slices.Contains(COLORS, detail):
|
||||
advertisement.Color = detail
|
||||
default:
|
||||
advertisement.Type = detail
|
||||
}
|
||||
}
|
||||
advertisement.Attributes = DecodeAttributes(advertisement.Details)
|
||||
|
||||
advertisement.CalculateExpire()
|
||||
|
||||
@@ -167,6 +158,35 @@ func ScrapeAd(fetch *Fetcher, uri string) error {
|
||||
return ScrapeImages(fetch, advertisement, addir)
|
||||
}
|
||||
|
||||
func DecodeAttributes(attributes string) map[string]string {
|
||||
rd := strings.NewReader(attributes)
|
||||
scanner := bufio.NewScanner(rd)
|
||||
|
||||
isattr := true
|
||||
attr := ""
|
||||
attrmap := map[string]string{}
|
||||
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if isattr {
|
||||
attr = line
|
||||
} else {
|
||||
attrmap[attr] = line
|
||||
}
|
||||
|
||||
isattr = !isattr
|
||||
}
|
||||
|
||||
fmt.Println(attributes)
|
||||
|
||||
return attrmap
|
||||
}
|
||||
|
||||
func ScrapeImages(fetch *Fetcher, advertisement *Ad, addir string) error {
|
||||
// fetch images
|
||||
img := 1
|
||||
|
||||
Reference in New Issue
Block a user