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
This commit is contained in:
Ludvig Lundgren 2022-04-30 13:43:51 +02:00 committed by GitHub
parent bb62e724a1
commit e6c151a029
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 3210 additions and 3201 deletions

View file

@ -2,7 +2,13 @@ package domain
import (
"context"
"strconv"
"strings"
"time"
"github.com/autobrr/autobrr/pkg/wildcard"
"github.com/dustin/go-humanize"
)
/*
@ -39,7 +45,7 @@ type Filter struct {
MatchReleaseGroups string `json:"match_release_groups"`
ExceptReleaseGroups string `json:"except_release_groups"`
Scene bool `json:"scene"`
Origins string `json:"origins"`
Origins []string `json:"origins"`
Freeleech bool `json:"freeleech"`
FreeleechPercent string `json:"freeleech_percent"`
Shows string `json:"shows"`
@ -51,6 +57,8 @@ type Filter struct {
Containers []string `json:"containers"`
MatchHDR []string `json:"match_hdr"`
ExceptHDR []string `json:"except_hdr"`
MatchOther []string `json:"match_other"`
ExceptOther []string `json:"except_other"`
Years string `json:"years"`
Artists string `json:"artists"`
Albums string `json:"albums"`
@ -74,3 +82,433 @@ type Filter struct {
Actions []*Action `json:"actions"`
Indexers []Indexer `json:"indexers"`
}
func (f Filter) CheckFilter(r *Release) ([]string, bool) {
// reset rejections first to clean previous checks
r.resetRejections()
if f.Freeleech && r.Freeleech != f.Freeleech {
r.addRejection("wanted: freeleech")
}
if f.FreeleechPercent != "" && !checkFreeleechPercent(r.FreeleechPercent, f.FreeleechPercent) {
r.addRejectionF("freeleech percent not matching. got: %v want: %v", r.FreeleechPercent, f.FreeleechPercent)
}
if len(f.Origins) > 0 && !containsSlice(r.Origin, f.Origins) {
r.addRejectionF("origin not matching. got: %v want: %v", r.Origin, f.Origins)
}
// title is the parsed title
if f.Shows != "" && !contains(r.Title, f.Shows) {
r.addRejectionF("shows not matching. got: %v want: %v", r.Title, f.Shows)
}
if f.Seasons != "" && !containsIntStrings(r.Season, f.Seasons) {
r.addRejectionF("season not matching. got: %d want: %v", r.Season, f.Seasons)
}
if f.Episodes != "" && !containsIntStrings(r.Episode, f.Episodes) {
r.addRejectionF("episodes not matching. got: %d want: %v", r.Episode, f.Episodes)
}
// matchRelease
// TODO allow to match against regex
if f.MatchReleases != "" && !containsFuzzy(r.TorrentName, f.MatchReleases) {
r.addRejectionF("match release not matching. got: %v want: %v", r.TorrentName, f.MatchReleases)
}
if f.ExceptReleases != "" && containsFuzzy(r.TorrentName, f.ExceptReleases) {
r.addRejectionF("except releases: unwanted release. got: %v want: %v", r.TorrentName, f.ExceptReleases)
}
if f.MatchReleaseGroups != "" && !contains(r.Group, f.MatchReleaseGroups) {
r.addRejectionF("release groups not matching. got: %v want: %v", r.Group, f.MatchReleaseGroups)
}
if f.ExceptReleaseGroups != "" && contains(r.Group, f.ExceptReleaseGroups) {
r.addRejectionF("unwanted release group. got: %v unwanted: %v", r.Group, f.ExceptReleaseGroups)
}
if f.MatchUploaders != "" && !contains(r.Uploader, f.MatchUploaders) {
r.addRejectionF("uploaders not matching. got: %v want: %v", r.Uploader, f.MatchUploaders)
}
if f.ExceptUploaders != "" && contains(r.Uploader, f.ExceptUploaders) {
r.addRejectionF("unwanted uploaders. got: %v unwanted: %v", r.Uploader, f.ExceptUploaders)
}
if len(f.Resolutions) > 0 && !containsSlice(r.Resolution, f.Resolutions) {
r.addRejectionF("resolution not matching. got: %v want: %v", r.Resolution, f.Resolutions)
}
if len(f.Codecs) > 0 && !sliceContainsSlice(r.Codec, f.Codecs) {
r.addRejectionF("codec not matching. got: %v want: %v", r.Codec, f.Codecs)
}
if len(f.Sources) > 0 && !containsSlice(r.Source, f.Sources) {
r.addRejectionF("source not matching. got: %v want: %v", r.Source, f.Sources)
}
if len(f.Containers) > 0 && !containsSlice(r.Container, f.Containers) {
r.addRejectionF("container not matching. got: %v want: %v", r.Container, f.Containers)
}
// HDR is parsed into the Codec slice from rls
if len(f.MatchHDR) > 0 && !sliceContainsSlice(r.HDR, f.MatchHDR) {
r.addRejectionF("hdr not matching. got: %v want: %v", r.HDR, f.MatchHDR)
}
// HDR is parsed into the Codec slice from rls
if len(f.ExceptHDR) > 0 && sliceContainsSlice(r.HDR, f.ExceptHDR) {
r.addRejectionF("hdr unwanted. got: %v want: %v", r.HDR, f.ExceptHDR)
}
if f.Years != "" && !containsIntStrings(r.Year, f.Years) {
r.addRejectionF("year not matching. got: %d want: %v", r.Year, f.Years)
}
if f.MatchCategories != "" && !contains(r.Category, f.MatchCategories) {
r.addRejectionF("category not matching. got: %v want: %v", r.Category, f.MatchCategories)
}
if f.ExceptCategories != "" && contains(r.Category, f.ExceptCategories) {
r.addRejectionF("category unwanted. got: %v want: %v", r.Category, f.ExceptCategories)
}
if len(f.MatchReleaseTypes) > 0 && !containsSlice(r.Category, f.MatchReleaseTypes) {
r.addRejectionF("release type not matching. got: %v want: %v", r.Category, f.MatchReleaseTypes)
}
if (f.MinSize != "" || f.MaxSize != "") && !f.checkSizeFilter(r, f.MinSize, f.MaxSize) {
r.addRejectionF("size not matching. got: %v want min: %v max: %v", r.Size, f.MinSize, f.MaxSize)
}
if f.Tags != "" && !containsAny(r.Tags, f.Tags) {
r.addRejectionF("tags not matching. got: %v want: %v", r.Tags, f.Tags)
}
if f.ExceptTags != "" && containsAny(r.Tags, f.ExceptTags) {
r.addRejectionF("tags unwanted. got: %v want: %v", r.Tags, f.ExceptTags)
}
if len(f.Artists) > 0 && !containsFuzzy(r.TorrentName, f.Artists) {
r.addRejectionF("artists not matching. got: %v want: %v", r.TorrentName, f.Artists)
}
if len(f.Albums) > 0 && !containsFuzzy(r.TorrentName, f.Albums) {
r.addRejectionF("albums not matching. got: %v want: %v", r.TorrentName, f.Albums)
}
// Perfect flac requires Cue, Log, Log Score 100, FLAC and 24bit Lossless
if f.PerfectFlac && !f.isPerfectFLAC(r) {
r.addRejectionF("wanted: perfect flac. got: %v", r.Audio)
}
if len(f.Formats) > 0 && !sliceContainsSlice(r.Audio, f.Formats) {
r.addRejectionF("formats not matching. got: %v want: %v", r.Audio, f.Formats)
}
if len(f.Quality) > 0 && !sliceContainsSlice(r.Audio, f.Quality) {
r.addRejectionF("quality not matching. got: %v want: %v", r.Audio, f.Quality)
}
if len(f.Media) > 0 && !containsSlice(r.Source, f.Media) {
r.addRejectionF("media not matching. got: %v want: %v", r.Source, f.Media)
}
if f.Cue && !containsAny(r.Audio, "Cue") {
r.addRejection("wanted: cue")
}
if f.Log && !containsAny(r.Audio, "Log") {
r.addRejection("wanted: log")
}
if f.Log && f.LogScore != 0 && r.LogScore != f.LogScore {
r.addRejectionF("log score. got: %v want: %v", r.LogScore, f.LogScore)
}
if len(r.Rejections) > 0 {
return r.Rejections, false
}
return nil, true
}
// isPerfectFLAC Perfect is "CD FLAC Cue Log 100% Lossless or 24bit Lossless"
func (f Filter) isPerfectFLAC(r *Release) bool {
if !contains(r.Source, "CD") {
return false
}
if !containsAny(r.Audio, "Cue") {
return false
}
if !containsAny(r.Audio, "Log") {
return false
}
if !containsAny(r.Audio, "Log100") {
return false
}
if !containsAny(r.Audio, "FLAC") {
return false
}
if !containsAnySlice(r.Audio, []string{"Lossless", "24bit Lossless"}) {
return false
}
return true
}
// checkSizeFilter additional size check
// for indexers that doesn't announce size, like some gazelle based
// set flag r.AdditionalSizeCheckRequired if there's a size in the filter, otherwise go a head
// implement API for ptp,btn,ggn to check for size if needed
// for others pull down torrent and do check
func (f Filter) checkSizeFilter(r *Release, minSize string, maxSize string) bool {
if r.Size == 0 {
r.AdditionalSizeCheckRequired = true
return true
} else {
r.AdditionalSizeCheckRequired = false
}
// if r.Size parse filter to bytes and compare
// handle both min and max
if minSize != "" {
// string to bytes
minSizeBytes, err := humanize.ParseBytes(minSize)
if err != nil {
// log could not parse into bytes
}
if r.Size <= minSizeBytes {
r.addRejection("size: smaller than min size")
return false
}
}
if maxSize != "" {
// string to bytes
maxSizeBytes, err := humanize.ParseBytes(maxSize)
if err != nil {
// log could not parse into bytes
}
if r.Size >= maxSizeBytes {
r.addRejection("size: larger than max size")
return false
}
}
return true
}
// checkFilterIntStrings "1,2,3-20"
func containsIntStrings(value int, filterList string) bool {
filters := strings.Split(filterList, ",")
for _, s := range filters {
s = strings.Replace(s, "%", "", -1)
s = strings.Trim(s, " ")
if strings.Contains(s, "-") {
minMax := strings.Split(s, "-")
// to int
min, err := strconv.ParseInt(minMax[0], 10, 32)
if err != nil {
return false
}
max, err := strconv.ParseInt(minMax[1], 10, 32)
if err != nil {
return false
}
if min > max {
// handle error
return false
} else {
// if announcePercent is greater than min and less than max return true
if value >= int(min) && value <= int(max) {
return true
}
}
}
filterInt, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return false
}
if int(filterInt) == value {
return true
}
}
return false
}
func contains(tag string, filter string) bool {
return containsMatch([]string{tag}, strings.Split(filter, ","))
}
func containsFuzzy(tag string, filter string) bool {
return containsMatchFuzzy([]string{tag}, strings.Split(filter, ","))
}
func containsSlice(tag string, filters []string) bool {
return containsMatch([]string{tag}, filters)
}
func containsAny(tags []string, filter string) bool {
return containsMatch(tags, strings.Split(filter, ","))
}
func sliceContainsSlice(tags []string, filters []string) bool {
return containsMatchBasic(tags, filters)
}
func containsMatchFuzzy(tags []string, filters []string) bool {
for _, tag := range tags {
tag = strings.ToLower(tag)
for _, filter := range filters {
filter = strings.ToLower(filter)
filter = strings.Trim(filter, " ")
// check if line contains * or ?, if so try wildcard match, otherwise try substring match
a := strings.ContainsAny(filter, "?|*")
if a {
match := wildcard.Match(filter, tag)
if match {
return true
}
} else if strings.Contains(tag, filter) {
return true
}
}
}
return false
}
func containsMatch(tags []string, filters []string) bool {
for _, tag := range tags {
tag = strings.ToLower(tag)
for _, filter := range filters {
filter = strings.ToLower(filter)
filter = strings.Trim(filter, " ")
// check if line contains * or ?, if so try wildcard match, otherwise try substring match
a := strings.ContainsAny(filter, "?|*")
if a {
match := wildcard.Match(filter, tag)
if match {
return true
}
} else if tag == filter {
return true
}
}
}
return false
}
func containsMatchBasic(tags []string, filters []string) bool {
for _, tag := range tags {
tag = strings.ToLower(tag)
for _, filter := range filters {
filter = strings.ToLower(filter)
filter = strings.Trim(filter, " ")
if tag == filter {
return true
}
}
}
return false
}
func containsAnySlice(tags []string, filters []string) bool {
for _, tag := range tags {
tag = strings.ToLower(tag)
for _, filter := range filters {
filter = strings.ToLower(filter)
filter = strings.Trim(filter, " ")
// check if line contains * or ?, if so try wildcard match, otherwise try substring match
wild := strings.ContainsAny(filter, "?|*")
if wild {
match := wildcard.Match(filter, tag)
if match {
return true
}
} else if tag == filter {
return true
}
}
}
return false
}
func checkFreeleechPercent(announcePercent int, filterPercent string) bool {
filters := strings.Split(filterPercent, ",")
// remove % and trim spaces
//announcePercent = strings.Replace(announcePercent, "%", "", -1)
//announcePercent = strings.Trim(announcePercent, " ")
//announcePercentInt, err := strconv.ParseInt(announcePercent, 10, 32)
//if err != nil {
// return false
//}
for _, s := range filters {
s = strings.Replace(s, "%", "", -1)
s = strings.Trim(s, " ")
if strings.Contains(s, "-") {
minMax := strings.Split(s, "-")
// to int
min, err := strconv.ParseInt(minMax[0], 10, 32)
if err != nil {
return false
}
max, err := strconv.ParseInt(minMax[1], 10, 32)
if err != nil {
return false
}
if min > max {
// handle error
return false
} else {
// if announcePercent is greater than min and less than max return true
if announcePercent >= int(min) && announcePercent <= int(max) {
return true
}
}
}
filterPercentInt, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return false
}
if int(filterPercentInt) == announcePercent {
return true
}
}
return false
}

File diff suppressed because it is too large Load diff

View file

@ -1,9 +1,13 @@
package domain
import (
"bytes"
"context"
"net/url"
"text/template"
"github.com/dustin/go-humanize"
"github.com/rs/zerolog/log"
)
type IndexerRepo interface {
@ -115,6 +119,56 @@ type IndexerParseMatch struct {
Encode []string `json:"encode"`
}
func (p *IndexerParse) ParseTorrentUrl(vars map[string]string, extraVars map[string]string, release *Release) error {
tmpVars := map[string]string{}
// copy vars to new tmp map
for k, v := range vars {
tmpVars[k] = v
}
// merge extra vars with vars
if extraVars != nil {
for k, v := range extraVars {
tmpVars[k] = v
}
}
// handle url encode of values
if p.Match.Encode != nil {
for _, e := range p.Match.Encode {
if v, ok := tmpVars[e]; ok {
// url encode value
t := url.QueryEscape(v)
tmpVars[e] = t
}
}
}
// setup text template to inject variables into
tmpl, err := template.New("torrenturl").Parse(p.Match.TorrentURL)
if err != nil {
log.Error().Err(err).Msg("could not create torrent url template")
return err
}
var urlBytes bytes.Buffer
err = tmpl.Execute(&urlBytes, &tmpVars)
if err != nil {
log.Error().Err(err).Msg("could not write torrent url template output")
return err
}
release.TorrentURL = urlBytes.String()
// handle cookies
if v, ok := extraVars["cookie"]; ok {
release.RawCookie = v
}
return nil
}
type TorrentBasic struct {
Id string `json:"Id"`
TorrentId string `json:"TorrentId,omitempty"`

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,377 @@
package domain
import (
"fmt"
"regexp"
)
var types map[string][]*TagInfo
func init() {
types = make(map[string][]*TagInfo)
audio := []*TagInfo{
{tag: "24BIT", title: "", regexp: "(?-i:24BIT)", re: nil},
{tag: "24BIT Lossless", title: "", regexp: "(?:24BIT lossless)", re: nil},
{tag: "16BIT", title: "", regexp: "(?-i:16BIT)", re: nil},
{tag: "320", title: "320 Kbps", regexp: "320[\\\\-\\\\._ kbps]?", re: nil},
{tag: "256", title: "256 Kbps", regexp: "256[\\\\-\\\\._ kbps]?", re: nil},
{tag: "192", title: "192 Kbps", regexp: "192[\\\\-\\\\._ kbps]?", re: nil},
{tag: "128", title: "128 Kbps", regexp: "128[\\\\-\\\\._ kbps]?", re: nil},
{tag: "AAC-LC", title: "Advanced Audio Coding (LC)", regexp: "aac[\\-\\._ ]?lc", re: nil},
{tag: "AAC", title: "Advanced Audio Coding (LC)", regexp: "", re: nil},
{tag: "AC3D", title: "", regexp: "ac[\\-\\._ ]?3d", re: nil},
{tag: "Atmos", title: "Dolby Atmos", regexp: "", re: nil},
{tag: "CBR", title: "Constant Bit Rate", regexp: "", re: nil},
{tag: "Cue", title: "Cue File", regexp: "", re: nil},
{tag: "DDPA", title: "Dolby Digital+ Atmos (E-AC-3+Atmos)", regexp: "dd[p\\+]a", re: nil},
{tag: "DDP", title: "Dolby Digital+ (E-AC-3)", regexp: "dd[p\\+]|e[\\-\\._ ]?ac3", re: nil},
{tag: "DD", title: "Dolby Digital (AC-3)", regexp: "dd|ac3|dolby[\\-\\._ ]?digital", re: nil},
{tag: "DTS-HD.HRA", title: "DTS (HD HRA)", regexp: "dts[\\-\\._ ]?hd[\\-\\._ ]?hra", re: nil},
{tag: "DTS-HD.HR", title: "DTS (HD HR)", regexp: "dts[\\-\\._ ]?hd[\\-\\._ ]?hr", re: nil},
{tag: "DTS-HD.MA", title: "DTS (HD MA)", regexp: "dts[\\-\\._ ]?hd[\\-\\._ ]?ma", re: nil},
{tag: "DTS-HD", title: "DTS (HD)", regexp: "dts[\\-\\._ ]?hd[\\-\\._ ]?", re: nil},
{tag: "DTS-MA", title: "DTS (MA)", regexp: "dts[\\-\\._ ]?ma[\\-\\._ ]?", re: nil},
{tag: "DTS-X", title: "DTS (X)", regexp: "dts[\\-\\._ ]?x", re: nil},
{tag: "DTS", title: "", regexp: "", re: nil},
{tag: "DUAL.AUDIO", title: "Dual Audio", regexp: "dual(?:[\\-\\._ ]?audio)?", re: nil},
{tag: "EAC3D", title: "", regexp: "", re: nil},
{tag: "ES", title: "Dolby Digital (ES)", regexp: "(?-i:ES)", re: nil},
{tag: "EX", title: "Dolby Digital (EX)", regexp: "(?-i:EX)", re: nil},
{tag: "FLAC", title: "Free Lossless Audio Codec", regexp: "", re: nil},
{tag: "LiNE", title: "Line", regexp: "(?-i:L[iI]NE)", re: nil},
{tag: "Lossless", title: "", regexp: "(?i:Lossless)", re: nil},
{tag: "Log100", title: "", regexp: "(log 100%)", re: nil},
{tag: "Log", title: "", regexp: "(?:log)", re: nil},
{tag: "LPCM", title: "Linear Pulse-Code Modulation", regexp: "", re: nil},
{tag: "MP3", title: "", regexp: "", re: nil},
{tag: "OGG", title: "", regexp: "", re: nil},
{tag: "OPUS", title: "", regexp: "", re: nil},
{tag: "TrueHD", title: "Dolby TrueHD", regexp: "(?:dolby[\\-\\._ ]?)?true[\\-\\._ ]?hd", re: nil},
{tag: "VBR", title: "Variable Bit Rate", regexp: "", re: nil},
}
types["audio"] = audio
bonus := []*TagInfo{
{tag: "Freeleech", title: "Freeleech", regexp: "freeleech", re: nil},
}
types["bonus"] = bonus
channels := []*TagInfo{
{tag: "7.1", title: "", regexp: "7\\.1(?:[\\-\\._ ]?audios)?", re: nil},
{tag: "6.1", title: "", regexp: "6\\.1(?:[\\-\\._ ]?audios)?", re: nil},
{tag: "6.0", title: "", regexp: "6\\.0(?:[\\-\\._ ]?audios)?", re: nil},
{tag: "5.1", title: "", regexp: "5\\.1(?:[\\-\\._ ]?audios)?", re: nil},
{tag: "5.0", title: "", regexp: "5\\.0(?:[\\-\\._ ]?audios)?", re: nil},
{tag: "4.1", title: "", regexp: "4\\.1(?:[\\-\\._ ]?audios)?", re: nil},
{tag: "4.0", title: "", regexp: "4\\.0(?:[\\-\\._ ]?audios)?", re: nil},
{tag: "3.1", title: "", regexp: "3\\.1(?:[\\-\\._ ]?audios)?", re: nil},
{tag: "3.0", title: "", regexp: "3\\.0(?:[\\-\\._ ]?audios)?", re: nil},
{tag: "2.1", title: "", regexp: "2\\.1(?:[\\-\\._ ]?audios)?", re: nil},
{tag: "2.0", title: "", regexp: "2\\.0(?:[\\-\\._ ]?audios)?", re: nil},
{tag: "1.0", title: "", regexp: "1\\.0(?:[\\-\\._ ]?audios)?", re: nil},
}
types["channels"] = channels
codecs := []*TagInfo{
{tag: "DiVX.SBC", title: "DivX SBC", regexp: "(?:divx[\\-\\._ ]?)?sbc", re: nil},
{tag: "x264.HQ", title: "x264 (HQ)", regexp: "x[\\\\-\\\\._ ]?264[\\\\-\\\\._ ]?hq", re: nil},
{tag: "MPEG-2", title: "", regexp: "mpe?g(?:[\\-\\._ ]?2)?", re: nil},
{tag: "H.265", title: "", regexp: "h[\\-\\._ ]?265", re: nil},
{tag: "H.264", title: "", regexp: "h[\\-\\._ ]?264", re: nil},
{tag: "H.263", title: "", regexp: "h[\\-\\._ ]?263", re: nil},
{tag: "H.262", title: "", regexp: "h[\\-\\._ ]?2[26]2", re: nil},
{tag: "H.261", title: "", regexp: "h[\\-\\._ ]?261", re: nil},
{tag: "dxva", title: "Direct-X Video Acceleration", regexp: "", re: nil},
{tag: "HEVC", title: "High Efficiency Video Coding", regexp: "", re: nil},
{tag: "VC-1", title: "", regexp: "vc[\\-\\._ ]?1", re: nil},
{tag: "x265", title: "", regexp: "x[\\-\\._ ]?265", re: nil},
{tag: "x264", title: "", regexp: "x[\\-\\._ ]?264", re: nil},
{tag: "XViD", title: "Xvid", regexp: "", re: nil},
{tag: "AVC", title: "Advanced Video Coding", regexp: "avc(?:[\\-\\._ ]?1)?", re: nil},
{tag: "VP9", title: "", regexp: "vp[\\-\\._ ]?9", re: nil},
{tag: "VP8", title: "", regexp: "vp[\\-\\._ ]?8", re: nil},
{tag: "VP7", title: "", regexp: "vp[\\-\\._ ]?7", re: nil},
}
types["codecs"] = codecs
container := []*TagInfo{
{tag: "avi", title: "Audio Video Interleave (avi)", regexp: "", re: nil},
{tag: "img", title: "IMG", regexp: "", re: nil},
{tag: "iso", title: "ISO", regexp: "", re: nil},
{tag: "mkv", title: "Matroska (mkv)", regexp: "", re: nil},
{tag: "mov", title: "MOV", regexp: "", re: nil},
{tag: "mp4", title: "MP4", regexp: "", re: nil},
{tag: "mpg", title: "MPEG", regexp: "mpe?g", re: nil},
{tag: "m2ts", title: "BluRay Disc (m2ts)", regexp: "", re: nil},
{tag: "vob", title: "VOB", regexp: "", re: nil},
}
types["container"] = container
hdr := []*TagInfo{
{tag: "HDR10+", title: "High Dynamic Range (10-bit+)", regexp: "hdr[\\-\\.]?10\\+|10\\+[\\-\\.]?bit|hdr10plus|hi10p", re: nil},
{tag: "HDR10", title: "High Dynamic Range (10-bit)", regexp: "hdr[\\-\\.]?10|10[\\-\\.]?bit|hi10", re: nil},
{tag: "HDR+", title: "High Dynamic Range+", regexp: "hdr\\+", re: nil},
{tag: "HDR", title: "High Dynamic Range", regexp: "", re: nil},
{tag: "SDR", title: "Standard Dynamic Range", regexp: "", re: nil},
{tag: "DV", title: "Dolby Vision", regexp: "(?i:dolby[\\-\\._ ]vision|dovi|\\Qdv\\E\\b)", re: nil},
}
types["hdr"] = hdr
other := []*TagInfo{
{tag: "HYBRID", title: "Hybrid", regexp: "", re: nil},
{tag: "REMUX", title: "Remux", regexp: "", re: nil},
{tag: "REPACK", title: "Repack", regexp: "repack(?:ed)?", re: nil},
{tag: "REREPACK", title: "Rerepack", regexp: "rerepack(?:ed)?", re: nil},
}
types["other"] = other
source := []*TagInfo{
{tag: "Cassette", title: "Cassette", regexp: "", re: nil},
{tag: "CD", title: "Compact Disc", regexp: "cd[\\-\\._ ]?(?:album)?", re: nil},
{tag: "BDRiP", title: "BluRay (rip)", regexp: "b[dr]?[\\-\\._ ]?rip", re: nil},
{tag: "BDSCR", title: "BluRay (screener)", regexp: "b[dr][\\-\\._ ]?scr(?:eener)?", re: nil},
{tag: "BluRay3D", title: "", regexp: "blu[\\-\\._ ]?ray[\\-\\._ ]?3d|bd3d", re: nil},
{tag: "BluRayRiP", title: "BluRay (rip)", regexp: "", re: nil},
{tag: "BluRay", title: "", regexp: "blu[\\-\\._ ]?ray|bd", re: nil},
{tag: "BRDRip", title: "BluRay Disc (rip)", regexp: "", re: nil},
{tag: "DAT", title: "Datacable", regexp: "(?-i:DAT)", re: nil},
{tag: "DVBRiP", title: "Digital Video Broadcasting (rip)", regexp: "dvb[\\-\\._ ]?rip", re: nil},
{tag: "DVDA", title: "Audio DVD", regexp: "", re: nil},
{tag: "DVDRiP", title: "Digital Video Disc (rip)", regexp: "dvd[\\-\\._ ]?rip", re: nil},
{tag: "DVDSCRRiP", title: "Digital Video Disc (screener rip)", regexp: "(?:dvd[\\-\\._ ]?)?scr(?:eener)?[\\-\\._ ]?rip", re: nil},
{tag: "DVDSCR", title: "Digital Video Disc (screener)", regexp: "(?:dvd[\\-\\._ ]?)?scr(?:eener)?", re: nil},
{tag: "DVDS", title: "Digital Video Disc (single)", regexp: "dvds(?:ingle)?", re: nil},
{tag: "DVD", title: "Digital Video Disc", regexp: "dvd", re: nil},
{tag: "SACD", title: "Super Audio Compact Disc", regexp: "", re: nil},
{tag: "RADIO", title: "Radio", regexp: "(?-i:R[aA]D[iI][oO])", re: nil},
{tag: "SATRiP", title: "Satellite (rip)", regexp: "sat[\\-\\._ ]?rip", re: nil},
{tag: "SAT", title: "Satellite Radio", regexp: "(?-i:SAT)", re: nil},
{tag: "SBD", title: "Soundboard", regexp: "(?-i:SBD|DAB|Soundboard)", re: nil},
{tag: "UHD.BDRiP", title: "Ultra High-Definition BluRay (rip)", regexp: "uhd[\\-\\._ ]?(?:bd)?rip", re: nil},
{tag: "UHD.BluRay", title: "Ultra High-Definition BluRay", regexp: "uhd[\\-\\._ ]?(?:blu[\\-\\._ ]?ray|bd)", re: nil},
{tag: "UHDTV", title: "Ultra High-Definition TV", regexp: "", re: nil},
{tag: "UMDMOVIE", title: "Universal Media Disc Movie", regexp: "", re: nil},
{tag: "Vinyl", title: "Vinyl", regexp: "vinyl|vl", re: nil},
{tag: "WEB-DL", title: "Web (DL)", regexp: "web[\\-\\._ ]?dl", re: nil},
{tag: "WEB-HD", title: "Web (HD)", regexp: "web[\\-\\._ ]?hd", re: nil},
{tag: "WEBFLAC", title: "Web (FLAC)", regexp: "", re: nil},
{tag: "WebHDRiP", title: "Web (HD rip)", regexp: "", re: nil},
{tag: "WEBRiP", title: "Web (rip)", regexp: "web[\\-\\._ ]?rip", re: nil},
{tag: "WEBSCR", title: "Web (screener)", regexp: "web[\\-\\._ ]?scr(?:eener)?", re: nil},
{tag: "WebUHD", title: "Web (UHD)", regexp: "", re: nil},
{tag: "WEB", title: "Web", regexp: "", re: nil},
}
types["source"] = source
resolution := []*TagInfo{
{tag: "PN", title: "Selector", regexp: "p(?:al)?[\\-\\._ ]?n(?:tsc)?[\\-\\._ ]selector", re: nil},
{tag: "DCI4K", title: "DCI 4k", regexp: "dci[\\-\\._ ]?4k|4096x2160", re: nil},
{tag: "DCI2K", title: "DCI 2k", regexp: "dci[\\-\\._ ]?2k|2048x1080", re: nil},
{tag: "4320p", title: "UltraHD 8K (4320p)", regexp: "4320p|7680x4320", re: nil},
{tag: "2880p", title: "5k (2880p)", regexp: "2880p|5k|5120x2880", re: nil},
{tag: "2160p", title: "UltraHD 4K (2160p)", regexp: "2160p|3840x2160|uhd|4k", re: nil},
{tag: "1800p", title: "QHD+ (1800p)", regexp: "1800p|3200x1800", re: nil},
{tag: "1440p", title: "QHD (1440p)", regexp: "1440p|2560x1440", re: nil},
{tag: "1080p", title: "FullHD (1080p)", regexp: "1080[ip]|1920x1080", re: nil},
{tag: "900p", title: "900[ip]|1600x900", regexp: "900[ip]|1600x900", re: nil},
{tag: "720p", title: "HD (720p)", regexp: "720[ip]|1280x720", re: nil},
{tag: "576p", title: "PAL (576p)", regexp: "576[ip]|720x576|pal", re: nil},
{tag: "540p", title: "qHD (540p)", regexp: "540[ip]|960x540", re: nil},
{tag: "480p", title: "NTSC (480p)", regexp: "480[ip]|720x480|848x480|854x480|ntsc", re: nil},
{tag: "360p", title: "nHD (360p)", regexp: "360[ip]|640x360", re: nil},
{tag: "$1p", title: "Other ($1p)", regexp: "([123]\\d{3})p", re: nil},
}
types["resolution"] = resolution
// language `(?i)\b((DK|DKSUBS|DANiSH|DUTCH|NL|NLSUBBED|ENG|FI|FLEMiSH|FiNNiSH|DE|FRENCH|GERMAN|HE|HEBREW|HebSub|HiNDi|iCELANDiC|KOR|MULTi|MULTiSUBS|NORWEGiAN|NO|NORDiC|PL|PO|POLiSH|PLDUB|RO|ROMANiAN|RUS|SPANiSH|SE|SWEDiSH|SWESUB||))\b`)
// websites `(?i)\b((AMBC|AS|AMZN|AMC|ANPL|ATVP|iP|CORE|BCORE|CMOR|CN|CBC|CBS|CMAX|CNBC|CC|CRIT|CR|CSPN|CW|DAZN|DCU|DISC|DSCP|DSNY|DSNP|DPLY|ESPN|FOX|FUNI|PLAY|HBO|HMAX|HIST|HS|HOTSTAR|HULU|iT|MNBC|MTV|NATG|NBC|NF|NICK|NRK|PMNT|PMNP|PCOK|PBS|PBSK|PSN|QIBI|SBS|SHO|STAN|STZ|SVT|SYFY|TLC|TRVL|TUBI|TV3|TV4|TVL|VH1|VICE|VMEO|UFC|USAN|VIAP|VIAPLAY|VL|WWEN|XBOX|YHOO|YT|RED))\b`)
for s, infos := range types {
for _, info := range infos {
var err error
//if info.re, err = regexp.Compile(`(?i)^(?:` + info.RE() + `)$`); err != nil {
if info.re, err = regexp.Compile(`(?i)(?:` + info.RE() + `)`); err != nil {
fmt.Errorf("tag %q has invalid regexp %q\n", s, info.re)
}
}
}
}
type TagInfo struct {
tag string
title string
regexp string
re *regexp.Regexp
}
// Tag returns the tag info tag.
func (info *TagInfo) Tag() string {
return info.tag
}
// Title returns the tag info title.
func (info *TagInfo) Title() string {
return info.title
}
// Regexp returns the tag info regexp.
func (info *TagInfo) Regexp() string {
return info.regexp
}
//// Other returns the tag info other.
//func (info *TagInfo) Other() string {
// return info.other
//}
//
//// Type returns the tag info type.
//func (info *TagInfo) Type() int {
// return info.typ
//}
//// Excl returns the tag info excl.
//func (info *TagInfo) Excl() bool {
// return info.excl
//}
// RE returns the tag info regexp string.
func (info *TagInfo) RE() string {
if info.regexp != "" {
return info.regexp
}
return `\Q` + info.tag + `\E`
}
// Match matches the tag info to s.
func (info *TagInfo) Match(s string) bool {
return info.re.MatchString(s)
}
// FindFunc is the find signature..
type FindFunc func(string) *TagInfo
// Find returns a func to find tag info.
func Find(infos ...*TagInfo) FindFunc {
n := len(infos)
return func(s string) *TagInfo {
for i := 0; i < n; i++ {
if infos[i].Match(s) {
return infos[i]
}
}
return nil
}
}
type ReleaseTags struct {
Audio []string
Channels string
Source string
Resolution string
Container string
Codec string
HDR []string
Other []string
Bonus []string
}
func ParseReleaseTags(tags []string) ReleaseTags {
releaseTags := ReleaseTags{}
for _, tag := range tags {
for tagType, tagInfos := range types {
for _, info := range tagInfos {
// check tag
match := info.Match(tag)
if match {
fmt.Printf("match: %v, info: %v\n", tag, info.Tag())
switch tagType {
case "audio":
releaseTags.Audio = append(releaseTags.Audio, info.Tag())
continue
case "bonus":
releaseTags.Bonus = append(releaseTags.Bonus, info.Tag())
continue
case "channels":
releaseTags.Channels = info.Tag()
break
case "codecs":
releaseTags.Codec = info.Tag()
break
case "container":
releaseTags.Container = info.Tag()
break
case "hdr":
releaseTags.HDR = append(releaseTags.HDR, info.Tag())
continue
case "other":
releaseTags.Other = append(releaseTags.Other, info.Tag())
continue
case "source":
releaseTags.Source = info.Tag()
break
case "resolution":
releaseTags.Resolution = info.Tag()
break
}
break
}
}
}
}
return releaseTags
}
func ParseReleaseTagString(tags string) ReleaseTags {
releaseTags := ReleaseTags{}
for tagType, tagInfos := range types {
//fmt.Printf("tagType: %v\n", tagType)
for _, info := range tagInfos {
// check tag
match := info.Match(tags)
if !match {
continue
}
//fmt.Printf("match: info: %v\n", info.Tag())
switch tagType {
case "audio":
releaseTags.Audio = append(releaseTags.Audio, info.Tag())
continue
case "bonus":
releaseTags.Bonus = append(releaseTags.Bonus, info.Tag())
continue
case "channels":
releaseTags.Channels = info.Tag()
break
case "codecs":
releaseTags.Codec = info.Tag()
break
case "container":
releaseTags.Container = info.Tag()
break
case "hdr":
releaseTags.HDR = append(releaseTags.HDR, info.Tag())
continue
case "other":
releaseTags.Other = append(releaseTags.Other, info.Tag())
continue
case "source":
releaseTags.Source = info.Tag()
break
case "resolution":
releaseTags.Resolution = info.Tag()
break
}
break
}
}
return releaseTags
}

View file

@ -0,0 +1,56 @@
package domain
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseReleaseTags(t *testing.T) {
type args struct {
tags []string
}
tests := []struct {
name string
args args
want ReleaseTags
}{
{name: "test_1", args: args{tags: []string{"CD", "FLAC", "Lossless"}}, want: ReleaseTags{Source: "CD", Audio: []string{"FLAC", "Lossless"}}},
{name: "test_2", args: args{tags: []string{"MP4", "2160p", "BluRay", "DV"}}, want: ReleaseTags{Source: "BluRay", Resolution: "2160p", Container: "mp4", HDR: []string{"DV"}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, ParseReleaseTags(tt.args.tags), "ParseReleaseTags(%v)", tt.args.tags)
})
}
}
func TestParseReleaseTagString(t *testing.T) {
type args struct {
tags string
}
tests := []struct {
name string
args args
want ReleaseTags
}{
{name: "music_1", args: args{tags: "FLAC / Lossless / Log / 80% / Cue / CD"}, want: ReleaseTags{Audio: []string{"Cue", "FLAC", "Lossless", "Log"}, Source: "CD"}},
{name: "music_2", args: args{tags: "FLAC Lossless Log 80% Cue CD"}, want: ReleaseTags{Audio: []string{"Cue", "FLAC", "Lossless", "Log"}, Source: "CD"}},
{name: "music_3", args: args{tags: "FLAC Lossless Log 100% Cue CD"}, want: ReleaseTags{Audio: []string{"Cue", "FLAC", "Lossless", "Log100", "Log"}, Source: "CD"}},
{name: "music_4", args: args{tags: "FLAC 24bit Lossless Log 100% Cue CD"}, want: ReleaseTags{Audio: []string{"24BIT Lossless", "Cue", "FLAC", "Lossless", "Log100", "Log"}, Source: "CD"}},
{name: "music_5", args: args{tags: "MP3 320 WEB"}, want: ReleaseTags{Audio: []string{"320", "MP3"}, Source: "WEB"}},
{name: "movies_1", args: args{tags: "x264 Blu-ray MKV 1080p"}, want: ReleaseTags{Codec: "x264", Source: "BluRay", Resolution: "1080p", Container: "mkv"}},
{name: "movies_2", args: args{tags: "HEVC HDR Blu-ray mp4 2160p"}, want: ReleaseTags{Codec: "HEVC", Source: "BluRay", Resolution: "2160p", Container: "mp4", HDR: []string{"HDR"}}},
{name: "movies_3", args: args{tags: "HEVC HDR DV Blu-ray mp4 2160p"}, want: ReleaseTags{Codec: "HEVC", Source: "BluRay", Resolution: "2160p", Container: "mp4", HDR: []string{"HDR", "DV"}}},
{name: "movies_4", args: args{tags: "H.264, Blu-ray/HD DVD"}, want: ReleaseTags{Codec: "H.264", Source: "BluRay"}},
{name: "movies_5", args: args{tags: "H.264, Remux"}, want: ReleaseTags{Codec: "H.264", Other: []string{"REMUX"}}},
{name: "movies_6", args: args{tags: "H.264, DVD"}, want: ReleaseTags{Codec: "H.264", Source: "DVD"}},
{name: "movies_7", args: args{tags: "H.264, DVD, Freeleech"}, want: ReleaseTags{Codec: "H.264", Source: "DVD", Bonus: []string{"Freeleech"}}},
{name: "movies_8", args: args{tags: "H.264, DVD, Freeleech!"}, want: ReleaseTags{Codec: "H.264", Source: "DVD", Bonus: []string{"Freeleech"}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, ParseReleaseTagString(tt.args.tags), "ParseReleaseTagString(%v)", tt.args.tags)
})
}
}