attempt to fix #117. code works, tests are still failing

This commit is contained in:
2025-02-10 16:13:21 +01:00
parent 09948a6b39
commit 602085d15e
5 changed files with 38 additions and 29 deletions

View File

@@ -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