Skip to content

Commit

Permalink
fix #77: use processed ad dir for duplicate checking, not slug
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas von Dein authored and TLINDEN committed Feb 10, 2024
1 parent 5a2c277 commit 2288806
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
15 changes: 13 additions & 2 deletions scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,32 @@ func ScrapeAd(fetch *Fetcher, uri string) error {

advertisement.CalculateExpire()

proceed := CheckAdVisited(fetch.Config, advertisement.Slug)
// prepare ad dir name
addir, err := AdDirName(fetch.Config, advertisement)
if err != nil {
return err
}

proceed := CheckAdVisited(fetch.Config, addir)
if !proceed {
return nil
}

// write listing
addir, err := WriteAd(fetch.Config, advertisement)
err = WriteAd(fetch.Config, advertisement, addir)
if err != nil {
return err
}

// tell the user
slog.Debug("extracted ad listing", "ad", advertisement)

// stats
fetch.Config.IncrAds()

// register for later checks
DirsVisited[addir] = 1

return ScrapeImages(fetch, advertisement, addir)
}

Expand Down
23 changes: 7 additions & 16 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,21 @@ func AdDirName(conf *Config, advertisement *Ad) (string, error) {
return buf.String(), nil
}

func WriteAd(conf *Config, advertisement *Ad) (string, error) {
// prepare ad dir name
addir, err := AdDirName(conf, advertisement)
if err != nil {
return "", err
}

func WriteAd(conf *Config, advertisement *Ad, addir string) error {
// prepare output dir
dir := filepath.Join(conf.Outdir, addir)

err = Mkdir(dir)
err := Mkdir(dir)
if err != nil {
return "", err
return err
}

// write ad file
listingfile := filepath.Join(dir, "Adlisting.txt")

listingfd, err := os.Create(listingfile)
if err != nil {
return "", fmt.Errorf("failed to create Adlisting.txt: %w", err)
return fmt.Errorf("failed to create Adlisting.txt: %w", err)
}
defer listingfd.Close()

Expand All @@ -104,17 +98,17 @@ func WriteAd(conf *Config, advertisement *Ad) (string, error) {

tmpl, err := tpl.New("adlisting").Parse(conf.Template)
if err != nil {
return "", fmt.Errorf("failed to parse adlisting template: %w", err)
return fmt.Errorf("failed to parse adlisting template: %w", err)
}

err = tmpl.Execute(listingfd, advertisement)
if err != nil {
return "", fmt.Errorf("failed to execute adlisting template: %w", err)
return fmt.Errorf("failed to execute adlisting template: %w", err)
}

slog.Info("wrote ad listing", "listingfile", listingfile)

return addir, nil
return nil
}

func WriteImage(filename string, reader *bytes.Reader) error {
Expand Down Expand Up @@ -176,9 +170,6 @@ func CheckAdVisited(conf *Config, adname string) bool {
return false
}

// register
DirsVisited[adname] = 1

// overwrite
return true
}

0 comments on commit 2288806

Please sign in to comment.