autobrr/internal/domain/macros.go
kenstir 4009554d10
feat(filters): skip duplicates (#1711)
* feat(filters): skip duplicates

* fix: add interface instead of any

* fix(filters): tonullint

* feat(filters): skip dupes check month day

* chore: cleanup

* feat(db): set autoincrement id

* feat(filters): add repack and proper to dupe profile

* feat(filters): add default dupe profiles

* feat(duplicates): check audio and website

* feat(duplicates): update tests

* feat(duplicates): add toggles on addform

* feat(duplicates): fix sqlite upgrade path and initialize duplicate profiles

* feat(duplicates): simplify sqlite upgrade

avoiding temp table and unwieldy select.  Besides, FK constraints
are turned off anyway in #229.

* feat(duplicates): change CheckIsDuplicateRelease treatment of PROPER and REPACK

"Proper" and "Repack" are not parallel to the other conditions like "Title",
so they do not belong as dedup conditions.  "PROPER" means there was an issue in
the previous release, and so a PROPER is never a duplicate, even if it replaces
another PROPER.  Similarly, "REPACK" means there was an issue in the previous
release by that group, and so it is a duplicate only if we previously took a
release from a DIFFERENT group.

I have not removed Proper and Repack from the UI or the schema yet.

* feat(duplicates): update postgres schema to match sqlite

* feat(duplicates): fix web build errors

* feat(duplicates): fix postgres errors

* feat(filters): do leftjoin for duplicate profile

* fix(filters): partial update dupe profile

* go fmt `internal/domain/filter.go`

* feat(duplicates): restore straightforward logic for proper/repack

* feat(duplicates): remove mostly duplicate TV duplicate profiles

Having one profile seems the cleanest.  If somebody wants multiple
resolutions then they can add Resolution to the duplicate profile.
Tested this profile with both weekly episodic releases and daily
show releases.

* feat(release): add db indexes and sub_title

* feat(release): add IsDuplicate tests

* feat(release): update action handler

* feat(release): add more tests for skip duplicates

* feat(duplicates): check audio

* feat(duplicates): add more tests

* feat(duplicates): match edition cut and more

* fix(duplicates): tests

* fix(duplicates): missing imports

* fix(duplicates): tests

* feat(duplicates): handle sub_title edition and language in ui

* fix(duplicates): tests

* feat(duplicates): check name against normalized hash

* fix(duplicates): tests

* chore: update .gitignore to ignore .pnpm-store

* fix: tests

* fix(filters): tests

* fix: bad conflict merge

* fix: update release type in test

* fix: use vendored hot-toast

* fix: release_test.go

* fix: rss_test.go

* feat(duplicates): improve title hashing for unique check

* feat(duplicates): further improve title hashing for unique check with lang

* feat(duplicates): fix tests

* feat(duplicates): add macros IsDuplicate and DuplicateProfile ID and name

* feat(duplicates): add normalized hash match option

* fix: headlessui-state prop warning

* fix(duplicates): add missing year in daily ep normalize

* fix(duplicates): check rejections len

---------

Co-authored-by: ze0s <ze0s@riseup.net>
2024-12-25 22:33:46 +01:00

212 lines
6.9 KiB
Go

// Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package domain
import (
"bytes"
"strings"
"text/template"
"time"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/Masterminds/sprig/v3"
"github.com/dustin/go-humanize"
)
type Macro struct {
Artists string
Audio []string
AudioChannels string
AudioFormat string
Bitrate string
Bonus []string
Categories []string
Category string
Codec []string
Container string
CurrentDay int
CurrentHour int
CurrentMinute int
CurrentMonth int
CurrentSecond int
CurrentYear int
Description string
DownloadUrl string
Episode int
FilterID int
FilterName string
Freeleech bool
FreeleechPercent int
Group string
GroupID string
HDR string
HasCue bool
HasLog bool
Implementation string
Indexer string
IndexerIdentifier string
IndexerIdentifierExternal string
IndexerName string
InfoUrl string
IsDuplicate bool
Language []string
Leechers int
LogScore int
MagnetURI string
Origin string
Other []string
PreTime string
Protocol string
Proper bool
Region string
Repack bool
Resolution string
Season int
Seeders int
Size uint64
SizeString string
SkipDuplicateProfileID int64
SkipDuplicateProfileName string
Source string
Tags string
Title string
TorrentDataRawBytes []byte
TorrentHash string
TorrentID string
TorrentName string
TorrentPathName string
TorrentUrl string
TorrentTmpFile string
Type string
Uploader string
RecordLabel string
Website string
Year int
Month int
Day int
}
func NewMacro(release Release) Macro {
currentTime := time.Now()
ma := Macro{
Artists: release.Artists,
Audio: release.Audio,
AudioChannels: release.AudioChannels,
AudioFormat: release.AudioFormat,
Bitrate: release.Bitrate,
Bonus: release.Bonus,
Categories: release.Categories,
Category: release.Category,
Codec: release.Codec,
Container: release.Container,
CurrentDay: currentTime.Day(),
CurrentHour: currentTime.Hour(),
CurrentMinute: currentTime.Minute(),
CurrentMonth: int(currentTime.Month()),
CurrentSecond: currentTime.Second(),
CurrentYear: currentTime.Year(),
Description: release.Description,
DownloadUrl: release.DownloadURL,
Episode: release.Episode,
FilterID: release.FilterID,
FilterName: release.FilterName,
Freeleech: release.Freeleech,
FreeleechPercent: release.FreeleechPercent,
Group: release.Group,
GroupID: release.GroupID,
HDR: strings.Join(release.HDR, ", "),
HasCue: release.HasCue,
HasLog: release.HasLog,
Implementation: release.Implementation.String(),
Indexer: release.Indexer.Identifier,
IndexerIdentifier: release.Indexer.Identifier,
IndexerIdentifierExternal: release.Indexer.IdentifierExternal,
IndexerName: release.Indexer.Name,
InfoUrl: release.InfoURL,
IsDuplicate: release.IsDuplicate,
Language: release.Language,
Leechers: release.Leechers,
LogScore: release.LogScore,
MagnetURI: release.MagnetURI,
Origin: release.Origin,
Other: release.Other,
PreTime: release.PreTime,
Protocol: release.Protocol.String(),
Proper: release.Proper,
Region: release.Region,
Repack: release.Repack,
Resolution: release.Resolution,
Season: release.Season,
Seeders: release.Seeders,
Size: release.Size,
SizeString: humanize.Bytes(release.Size),
Source: release.Source,
SkipDuplicateProfileID: release.SkipDuplicateProfileID,
SkipDuplicateProfileName: release.SkipDuplicateProfileName,
Tags: strings.Join(release.Tags, ", "),
Title: release.Title,
TorrentDataRawBytes: release.TorrentDataRawBytes,
TorrentHash: release.TorrentHash,
TorrentID: release.TorrentID,
TorrentName: release.TorrentName,
TorrentPathName: release.TorrentTmpFile,
TorrentUrl: release.DownloadURL,
TorrentTmpFile: release.TorrentTmpFile,
Type: release.Type.String(),
Uploader: release.Uploader,
RecordLabel: release.RecordLabel,
Website: release.Website,
Year: release.Year,
Month: release.Month,
Day: release.Day,
}
return ma
}
// Parse takes a string and replaces valid vars
func (m Macro) Parse(text string) (string, error) {
if text == "" {
return "", nil
}
// TODO implement template cache
// setup template
tmpl, err := template.New("macro").Funcs(sprig.TxtFuncMap()).Parse(text)
if err != nil {
return "", errors.Wrap(err, "could parse macro template")
}
var tpl bytes.Buffer
err = tmpl.Execute(&tpl, m)
if err != nil {
return "", errors.Wrap(err, "could not parse macro")
}
return tpl.String(), nil
}
// MustParse takes a string and replaces valid vars
func (m Macro) MustParse(text string) string {
if text == "" {
return ""
}
// setup template
tmpl, err := template.New("macro").Funcs(sprig.TxtFuncMap()).Parse(text)
if err != nil {
return ""
}
var tpl bytes.Buffer
err = tmpl.Execute(&tpl, m)
if err != nil {
return ""
}
return tpl.String()
}