re-orgainzied code a little, using go templates instead format string

This commit is contained in:
2023-12-17 17:32:05 +01:00
parent e904ed6687
commit f932d7be83
9 changed files with 119 additions and 43 deletions

View File

@@ -23,7 +23,6 @@ import (
"io"
"log/slog"
"net/http"
"os"
"strings"
"sync"
@@ -160,28 +159,12 @@ func Scrape(uri string, dir string, template string) error {
}
slog.Debug("extracted ad listing", "ad", ad)
// prepare output dir
dir = dir + "/" + ad.Slug
err = Mkdir(dir)
// write listing
err = WriteAd(dir, ad, template)
if err != nil {
return err
}
// write ad file
listingfile := strings.Join([]string{dir, "Adlisting.txt"}, "/")
f, err := os.Create(listingfile)
if err != nil {
return err
}
ad.Text = strings.ReplaceAll(ad.Text, "<br/>", "\n")
_, err = fmt.Fprintf(f, template,
ad.Title, ad.Price, ad.Id, ad.Category, ad.Condition, ad.Created, ad.Text)
if err != nil {
return err
}
slog.Info("wrote ad listing", "listingfile", listingfile)
return ScrapeImages(dir, ad)
}
@@ -230,13 +213,7 @@ func Getimage(uri, fileName string) error {
return errors.New("received non 200 response code")
}
file, err := os.Create(fileName)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, response.Body)
err = WriteImage(fileName, response.Body)
if err != nil {
return err
}