diff --git a/go.mod b/go.mod index 41b747d..f10200e 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module github.com/ProfChaos/torrent-name-parser go 1.20 -require github.com/stretchr/testify v1.8.2 +require ( + github.com/stretchr/testify v1.8.2 + golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 +) require ( github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/go.sum b/go.sum index 6a56e69..66999ab 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o= +golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/misc.go b/misc.go index d464685..faf49b8 100644 --- a/misc.go +++ b/misc.go @@ -2,6 +2,8 @@ package torrentparser import ( "regexp" + + "golang.org/x/exp/slices" ) var ( @@ -23,7 +25,7 @@ func init() { hardcodedGeneral = regexp.MustCompile(`(?i)\bHC|HARDCODED\b`) regionGeneral = regexp.MustCompile(`(?i)dvd(R[0-9])`) containerGeneral = regexp.MustCompile(`(?i)\.(MKV|AVI|MP4)$`) - hdrGeneral = regexp.MustCompile("(?i)hdr") + hdrGeneral = regexp.MustCompile("(?i)hdr(?:10)?|dv") repackGeneral = regexp.MustCompile("(?i)repack|rerip") extendedGeneral = regexp.MustCompile("(?i)extended") properGeneral = regexp.MustCompile("(?i)proper") @@ -50,8 +52,24 @@ func (p *parser) GetContainer() string { return p.FindString("container", containerGeneral, FindStringOptions{}) } -func (p *parser) GetHdr() bool { - return p.FindBoolean("hdr", hdrGeneral) +func (p *parser) Hdr() ([]string, bool) { + isHDR := false + return p.FindStrings("hdr", hdrGeneral, FindStringsOptions{ + Handler: func(s []string) []string { + if len(s) > 0 { + isHDR = true + } + pos := slices.Index(s, "HDR") + if pos != -1 { + if len(s) == 1 { + return nil + } + + return slices.Delete(s, pos, pos+1) + } + return s + }, + }), isHDR } func (p *parser) Repack() bool { diff --git a/parser.go b/parser.go index 69e6740..734d876 100644 --- a/parser.go +++ b/parser.go @@ -60,6 +60,7 @@ type Torrent struct { Episode int `json:"episode"` Languages []string `json:"languages"` Hdr bool `json:"hdr"` + HdrType []string `json:"hdrType"` ColorDepth string `json:"colorDepth"` Date string `json:"date"` } @@ -101,6 +102,15 @@ func ParseName(name string) (Torrent, error) { return p.Parse() } +func DebugParser(name string) { + p := &parser{Name: name, MatchedIndicies: map[string]index{}, LowestIndex: len(name)} + p.Parse() + + for _, index := range p.MatchedIndicies { + fmt.Printf("%s\033[34m%s\033[0m%s | \033[34m%s\033[0m\n", name[:index.Start], name[index.Start:index.End], name[index.End:], index.Name) + } +} + func (p *parser) Parse() (Torrent, error) { torrent := Torrent{ Season: -1, @@ -127,7 +137,7 @@ func (p *parser) Parse() (Torrent, error) { } torrent.Episode = p.GetEpisode() torrent.Unrated = p.GetUnrated() - torrent.Hdr = p.GetHdr() + torrent.HdrType, torrent.Hdr = p.Hdr() torrent.ColorDepth = p.GetColorDepth() torrent.Languages = p.GetLanguages() @@ -180,6 +190,7 @@ func (p *parser) FindBoolean(attr string, rx *regexp.Regexp) bool { if p.MatchedRange(loc[0], loc[1]) { return false } + p.AddMatchedIndex(attr, loc) p.setLowestIndex(loc[0]) return true } @@ -357,8 +368,8 @@ func (p *parser) shouldAllReturnNil(name string, locs [][]int) ([]string, bool) } matches := make([]string, 0) - for _, loc := range locs { - match, returnNil := p.shouldReturnNil(name, loc) + for i, loc := range locs { + match, returnNil := p.shouldReturnNil(fmt.Sprintf("%s%d", name, i), loc) if returnNil { return nil, true } diff --git a/parser_test.go b/parser_test.go index 747ecaf..978e11a 100644 --- a/parser_test.go +++ b/parser_test.go @@ -369,6 +369,7 @@ func TestParser_Parse(t *testing.T) { Group: "NAHOM", Source: "bdremux", Languages: []string{"ita", "eng"}, + HdrType: []string{"DV"}, Hdr: true, Season: -1, }, @@ -403,6 +404,23 @@ func TestParser_Parse(t *testing.T) { Container: "mp4", }, }, + { + name: "Ant-Man.and.the.Wasp.Quantumania.2023.2160p.MA.WEB-DL.DDP5.1.Atmos.DV.HDR10.H.265-CMRG.mkv", + want: Torrent{ + Title: "Ant-Man and the Wasp Quantumania", + Year: 2023, + Resolution: "4k", + Container: "mkv", + Source: "web-dl", + Codec: "h265", + Audio: "DDP5.1 Atmos", + HdrType: []string{"DV", "HDR10"}, + Hdr: true, + Season: -1, + Group: "CMRG", + ContentType: Movie, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -446,7 +464,7 @@ func TestContentType(t *testing.T) { func TestTorrentScanAndValue(t *testing.T) { var torrent Torrent - jsonStr := `{"title":"Pirates of the Caribbean Dead Mans Chest","alternativeTitle":"","contentType":0,"year":0,"resolution":"4k","extended":false,"unrated":false,"proper":false,"repack":false,"convert":false,"hardcoded":false,"retail":false,"remastered":false,"region":"","container":"mkv","source":"web-dl","codec":"hevc","audio":"dts-hd","group":"WATCHER","season":-1,"seasons":null,"episode":0,"languages":null,"hdr":true,"colorDepth":"","date":""}` + jsonStr := `{"title":"Pirates of the Caribbean Dead Mans Chest","alternativeTitle":"","contentType":0,"year":0,"resolution":"4k","extended":false,"unrated":false,"proper":false,"repack":false,"convert":false,"hardcoded":false,"retail":false,"remastered":false,"region":"","container":"mkv","source":"web-dl","codec":"hevc","audio":"dts-hd","group":"WATCHER","season":-1,"seasons":null,"episode":0,"languages":null,"hdr":true,"hdrType":null,"colorDepth":"","date":""}` err := torrent.Scan(jsonStr) if err != nil {