2023-12-17 17:32:05 +01:00
|
|
|
/*
|
2025-02-10 22:20:25 +01:00
|
|
|
Copyright © 2023-2025 Thomas von Dein
|
2023-12-17 17:32:05 +01:00
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2024-01-12 13:29:59 +01:00
|
|
|
"bytes"
|
2024-01-21 12:38:14 +01:00
|
|
|
"fmt"
|
2023-12-17 17:32:05 +01:00
|
|
|
"log/slog"
|
|
|
|
|
"os"
|
2023-12-18 09:21:26 +01:00
|
|
|
"path/filepath"
|
2023-12-18 20:18:37 +01:00
|
|
|
"runtime"
|
2023-12-17 17:32:05 +01:00
|
|
|
"strings"
|
|
|
|
|
tpl "text/template"
|
2024-02-10 14:36:53 +01:00
|
|
|
"time"
|
2023-12-17 17:32:05 +01:00
|
|
|
)
|
|
|
|
|
|
2024-02-10 14:36:53 +01:00
|
|
|
type OutdirData struct {
|
|
|
|
|
Year, Day, Month string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func OutDirName(conf *Config) (string, error) {
|
|
|
|
|
tmpl, err := tpl.New("outdir").Parse(conf.Outdir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("failed to parse outdir template: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
data := OutdirData{
|
|
|
|
|
Year: now.Format("2006"),
|
2025-02-10 22:20:25 +01:00
|
|
|
Month: now.Format("01"),
|
|
|
|
|
Day: now.Format("02"),
|
2024-02-10 14:36:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = tmpl.Execute(&buf, data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("failed to execute outdir template: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buf.String(), nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 19:03:34 +01:00
|
|
|
func AdDirName(conf *Config, advertisement *Ad) (string, error) {
|
|
|
|
|
tmpl, err := tpl.New("adname").Parse(conf.Adnametemplate)
|
2024-01-12 13:29:59 +01:00
|
|
|
if err != nil {
|
2024-01-25 19:03:34 +01:00
|
|
|
return "", fmt.Errorf("failed to parse adname template: %w", err)
|
2024-01-12 13:29:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf := bytes.Buffer{}
|
2024-01-25 19:03:34 +01:00
|
|
|
|
|
|
|
|
err = tmpl.Execute(&buf, advertisement)
|
2024-01-12 13:29:59 +01:00
|
|
|
if err != nil {
|
2024-01-25 19:03:34 +01:00
|
|
|
return "", fmt.Errorf("failed to execute adname template: %w", err)
|
2024-01-12 13:29:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buf.String(), nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-10 15:11:37 +01:00
|
|
|
func WriteAd(conf *Config, advertisement *Ad, addir string) error {
|
2023-12-17 17:32:05 +01:00
|
|
|
// prepare output dir
|
2024-01-25 19:03:34 +01:00
|
|
|
dir := filepath.Join(conf.Outdir, addir)
|
|
|
|
|
|
2024-02-10 15:11:37 +01:00
|
|
|
err := Mkdir(dir)
|
2023-12-17 17:32:05 +01:00
|
|
|
if err != nil {
|
2024-02-10 15:11:37 +01:00
|
|
|
return err
|
2023-12-17 17:32:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// write ad file
|
2023-12-18 09:21:26 +01:00
|
|
|
listingfile := filepath.Join(dir, "Adlisting.txt")
|
2024-01-25 19:03:34 +01:00
|
|
|
|
|
|
|
|
listingfd, err := os.Create(listingfile)
|
2023-12-17 17:32:05 +01:00
|
|
|
if err != nil {
|
2024-02-10 15:11:37 +01:00
|
|
|
return fmt.Errorf("failed to create Adlisting.txt: %w", err)
|
2023-12-17 17:32:05 +01:00
|
|
|
}
|
2025-05-04 12:05:59 +02:00
|
|
|
defer func() {
|
|
|
|
|
if err := listingfd.Close(); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
2023-12-17 17:32:05 +01:00
|
|
|
|
2024-01-25 19:03:34 +01:00
|
|
|
if runtime.GOOS == WIN {
|
|
|
|
|
advertisement.Text = strings.ReplaceAll(advertisement.Text, "<br/>", "\r\n")
|
2023-12-18 20:18:37 +01:00
|
|
|
} else {
|
2024-01-25 19:03:34 +01:00
|
|
|
advertisement.Text = strings.ReplaceAll(advertisement.Text, "<br/>", "\n")
|
2023-12-18 20:18:37 +01:00
|
|
|
}
|
2023-12-17 17:32:05 +01:00
|
|
|
|
2024-01-25 19:03:34 +01:00
|
|
|
tmpl, err := tpl.New("adlisting").Parse(conf.Template)
|
2023-12-17 17:32:05 +01:00
|
|
|
if err != nil {
|
2024-02-10 15:11:37 +01:00
|
|
|
return fmt.Errorf("failed to parse adlisting template: %w", err)
|
2023-12-17 17:32:05 +01:00
|
|
|
}
|
2023-12-29 13:47:18 +01:00
|
|
|
|
2024-01-25 19:03:34 +01:00
|
|
|
err = tmpl.Execute(listingfd, advertisement)
|
2023-12-17 17:32:05 +01:00
|
|
|
if err != nil {
|
2024-02-10 15:11:37 +01:00
|
|
|
return fmt.Errorf("failed to execute adlisting template: %w", err)
|
2023-12-17 17:32:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slog.Info("wrote ad listing", "listingfile", listingfile)
|
|
|
|
|
|
2024-02-10 15:11:37 +01:00
|
|
|
return nil
|
2023-12-17 17:32:05 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-27 17:23:09 +01:00
|
|
|
func WriteImage(filename string, reader *bytes.Reader) error {
|
2023-12-17 17:32:05 +01:00
|
|
|
file, err := os.Create(filename)
|
|
|
|
|
if err != nil {
|
2024-01-25 19:03:34 +01:00
|
|
|
return fmt.Errorf("failed to open image file: %w", err)
|
2023-12-17 17:32:05 +01:00
|
|
|
}
|
2025-05-04 12:05:59 +02:00
|
|
|
defer func() {
|
|
|
|
|
if err := file.Close(); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
2023-12-17 17:32:05 +01:00
|
|
|
|
2024-01-27 17:23:09 +01:00
|
|
|
_, err = reader.WriteTo(file)
|
2024-01-21 12:38:14 +01:00
|
|
|
|
2023-12-17 17:32:05 +01:00
|
|
|
if err != nil {
|
2024-01-25 19:03:34 +01:00
|
|
|
return fmt.Errorf("failed to write to image file: %w", err)
|
2023-12-17 17:32:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2024-01-21 12:38:14 +01:00
|
|
|
|
|
|
|
|
func ReadImage(filename string) (*bytes.Buffer, error) {
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
|
|
if !fileExists(filename) {
|
|
|
|
|
return nil, fmt.Errorf("image %s does not exist", filename)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := os.ReadFile(filename)
|
|
|
|
|
if err != nil {
|
2024-01-25 19:03:34 +01:00
|
|
|
return nil, fmt.Errorf("failed to read image file: %w", err)
|
2024-01-21 12:38:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = buf.Write(data)
|
|
|
|
|
if err != nil {
|
2024-01-25 19:03:34 +01:00
|
|
|
return nil, fmt.Errorf("failed to write image into buffer: %w", err)
|
2024-01-21 12:38:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &buf, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func fileExists(filename string) bool {
|
|
|
|
|
info, err := os.Stat(filename)
|
2024-12-13 11:23:24 +01:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
// return false on any error
|
2024-01-21 12:38:14 +01:00
|
|
|
return false
|
|
|
|
|
}
|
2024-01-25 19:03:34 +01:00
|
|
|
|
2024-01-21 12:38:14 +01:00
|
|
|
return !info.IsDir()
|
|
|
|
|
}
|
2024-02-10 14:06:06 +01:00
|
|
|
|
|
|
|
|
// check if an addir has already been processed by current run and
|
|
|
|
|
// decide what to do
|
|
|
|
|
func CheckAdVisited(conf *Config, adname string) bool {
|
|
|
|
|
if Exists(DirsVisited, adname) {
|
|
|
|
|
if conf.ForceDownload {
|
|
|
|
|
slog.Warn("an ad with the same name has already been downloaded, overwriting", "addir", adname)
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// don't overwrite
|
|
|
|
|
slog.Warn("an ad with the same name has already been downloaded, skipping (use -f to overwrite)", "addir", adname)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// overwrite
|
|
|
|
|
return true
|
|
|
|
|
}
|