autobrr/internal/action/macros.go
Ludvig Lundgren e6c151a029
feat: improve release parsing and filtering (#257)
* feat(releases): improve parsing

* refactor: extend filtering add more tests

* feat: improve macro

* feat: add and remove fields

* feat: add freeleech percent to bonus

* feat: filter by origin
2022-04-30 13:43:51 +02:00

74 lines
1.6 KiB
Go

package action
import (
"bytes"
"strings"
"text/template"
"time"
"github.com/autobrr/autobrr/internal/domain"
)
type Macro struct {
TorrentName string
TorrentPathName string
TorrentHash string
TorrentUrl string
Indexer string
Title string
Resolution string
Source string
HDR string
Season int
Episode int
Year int
Month int
Day int
Hour int
Minute int
Second int
}
func NewMacro(release domain.Release) Macro {
currentTime := time.Now()
ma := Macro{
TorrentName: release.TorrentName,
TorrentUrl: release.TorrentURL,
TorrentPathName: release.TorrentTmpFile,
TorrentHash: release.TorrentHash,
Indexer: release.Indexer,
Title: release.Title,
Resolution: release.Resolution,
Source: release.Source,
HDR: strings.Join(release.HDR, ", "),
Season: release.Season,
Episode: release.Episode,
Year: currentTime.Year(),
Month: int(currentTime.Month()),
Day: currentTime.Day(),
Hour: currentTime.Hour(),
Minute: currentTime.Minute(),
Second: currentTime.Second(),
}
return ma
}
// Parse takes a string and replaces valid vars
func (m Macro) Parse(text string) (string, error) {
// setup template
tmpl, err := template.New("macro").Parse(text)
if err != nil {
return "", err
}
var tpl bytes.Buffer
err = tmpl.Execute(&tpl, m)
if err != nil {
return "", err
}
return tpl.String(), nil
}