From e497505c68d50ed2f1f32e913fbe7ed86b7c6067 Mon Sep 17 00:00:00 2001 From: Dan Audne Date: Mon, 7 Oct 2024 20:17:06 +0200 Subject: [PATCH] feat: add resolution constants --- .gitignore | 4 ++++ go.mod | 2 +- parser.go | 26 ++++++++++++------------ parser_test.go | 20 +++++++++---------- resolution.go | 49 ++++++++++++++++++++++++++++++++++------------ resolution_test.go | 8 ++++---- 6 files changed, 68 insertions(+), 41 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e96fd0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.direnv +.envrc +flake.lock +flake.nix diff --git a/go.mod b/go.mod index 97637a1..aa397ce 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/ProfChaos/torrent-name-parser -go 1.21 +go 1.23 require github.com/google/go-cmp v0.6.0 diff --git a/parser.go b/parser.go index 48e5ad0..97aae79 100644 --- a/parser.go +++ b/parser.go @@ -37,7 +37,7 @@ type Torrent struct { AlternativeTitle string `json:"alternativeTitle"` ContentType ContentType `json:"contentType"` Year int `json:"year"` - Resolution string `json:"resolution"` + Resolution Resolution `json:"resolution"` Extended bool `json:"extended"` Unrated bool `json:"unrated"` Proper bool `json:"proper"` @@ -331,30 +331,28 @@ func (p *parser) parseNumbers(attr string, loc [][]int, options FindNumbersOptio } func (p *parser) shouldReturnNil(name string, loc []int) ([]string, bool) { - if len(loc) == 0 { + length := len(loc) + if length == 0 { return nil, true } - if len(loc) == 6 && p.MatchedRange(loc[4], loc[5]) { - return nil, true - } else if len(loc) == 4 && p.MatchedRange(loc[2], loc[3]) { - return nil, true - } else if len(loc) == 2 && p.MatchedRange(loc[0], loc[1]) { + if length > 1 && p.MatchedRange(loc[length-2], loc[length-1]) { return nil, true } p.setLowestIndex(loc[0]) matches := make([]string, 0) - if len(loc) == 6 { - matches = append(matches, p.Name[loc[2]:loc[3]], p.Name[loc[4]:loc[5]]) - p.AddMatchedIndex(name, []int{loc[2], loc[5]}) - } else if len(loc) == 4 { - matches = append(matches, p.Name[loc[2]:loc[3]]) - p.AddMatchedIndex(name, []int{loc[2], loc[3]}) - } else { + // If we don't have any groups in the regex + if length == 2 { matches = append(matches, p.Name[loc[0]:loc[1]]) p.AddMatchedIndex(name, []int{loc[0], loc[1]}) + } else { + // Support for multiple groups + for i := 2; i < length; i += 2 { + matches = append(matches, p.Name[loc[i]:loc[i+1]]) + p.AddMatchedIndex(name, []int{loc[i], loc[i+1]}) + } } return matches, false diff --git a/parser_test.go b/parser_test.go index 4934bd7..0389b3f 100644 --- a/parser_test.go +++ b/parser_test.go @@ -18,7 +18,7 @@ func TestParser_Parse(t *testing.T) { want: Torrent{ Title: "blade runner 2049", Year: 2017, - Resolution: "4k", + Resolution: "2160p", ContentType: Movie, Season: -1, Group: "terminal", @@ -32,7 +32,7 @@ func TestParser_Parse(t *testing.T) { want: Torrent{ Title: "Wonder Woman 1984", Year: 2020, - Resolution: "4k", + Resolution: "2160p", ContentType: Movie, Season: -1, Group: "TOMMY", @@ -205,7 +205,7 @@ func TestParser_Parse(t *testing.T) { want: Torrent{ Title: "Shingeki no Kyojin", Audio: "flac", - Resolution: "1080", + Resolution: "1080p", Container: "mkv", Group: "BlurayDesuYo", Season: 3, @@ -224,7 +224,7 @@ func TestParser_Parse(t *testing.T) { Container: "mp4", Episode: 33, Group: "Ohys-Raws", - Resolution: "720", + Resolution: "720p", Title: "JoJo no Kimyou na Bouken Ougon no Kaze", ContentType: TV, }, @@ -279,7 +279,7 @@ func TestParser_Parse(t *testing.T) { Container: "mkv", Group: "BLUTONiUM", Title: "Star Wars Episode IX The Rise of Skywalker", - Resolution: "4k", + Resolution: "2160p", Source: "web-dl", ContentType: Movie, }, @@ -321,7 +321,7 @@ func TestParser_Parse(t *testing.T) { Title: "Pirates of the Caribbean Dead Mans Chest", ContentType: Movie, Year: 2006, - Resolution: "4k", + Resolution: "2160p", Container: "mkv", Source: "web-dl", Codec: "hevc", @@ -348,7 +348,7 @@ func TestParser_Parse(t *testing.T) { want: Torrent{ Title: "Altered Carbon", ContentType: TV, - Resolution: "4k", + Resolution: "2160p", Group: "TrollUHD", Source: "webrip", Codec: "x265", @@ -364,7 +364,7 @@ func TestParser_Parse(t *testing.T) { Title: "The Wizard of Oz", ContentType: Movie, Year: 1939, - Resolution: "4k", + Resolution: "2160p", Codec: "x265", Group: "NAHOM", Source: "bdremux", @@ -409,7 +409,7 @@ func TestParser_Parse(t *testing.T) { want: Torrent{ Title: "Ant-Man and the Wasp Quantumania", Year: 2023, - Resolution: "4k", + Resolution: "2160p", Container: "mkv", Source: "web-dl", Codec: "h265", @@ -441,7 +441,7 @@ func TestParser_Parse(t *testing.T) { want: Torrent{ Title: "Crouching Tiger Hidden Dragon", Year: 2000, - Resolution: "4k", + Resolution: "2160p", Source: "bluray", Codec: "x265", Group: "DEPTH", diff --git a/resolution.go b/resolution.go index 6cbfefd..bafe609 100644 --- a/resolution.go +++ b/resolution.go @@ -7,25 +7,50 @@ import ( var ( resolutionX = regexp.MustCompile(`(?i)[0-9]{3,4}x([0-9]{3,4})`) - resolution4k = regexp.MustCompile(`(?i)\b(4k|2160p)\b`) - resolution8k = regexp.MustCompile(`(?i)\b(8k|4320p)\b`) + resolution4k = regexp.MustCompile(`(?i)\b4k\b`) + resolution8k = regexp.MustCompile(`(?i)\b8k\b`) resolutionGeneral = regexp.MustCompile(`(?i)[0-9]{3,4}[pi]`) ) -func (p *parser) GetResolution() string { - resolution := p.FindString("resolution", resolutionX, FindStringOptions{}) +type Resolution string + +const ( + Resolution480p Resolution = "480p" + Resolution576p Resolution = "576p" + Resolution720p Resolution = "720p" + Resolution1080i Resolution = "1080i" + Resolution1080p Resolution = "1080p" + Resolution4k Resolution = "2160p" + Resolution8k Resolution = "4320p" + ResolutionUnknown Resolution = "" +) + +func (r Resolution) Verify() bool { + switch r { + case Resolution480p, Resolution576p, Resolution720p, Resolution1080i, Resolution1080p, Resolution4k, Resolution8k: + return true + default: + return false + } +} + +func (p *parser) GetResolution() Resolution { + resolution := p.FindString("resolution", resolutionX, FindStringOptions{Handler: func(str string) string { + return strings.ToLower(str) + "p" + }}) if resolution != "" { - return resolution + return Resolution(resolution) } - resolution = p.FindString("resolution", resolution4k, FindStringOptions{Value: "4k"}) + resolution = p.FindString("resolution", resolutionGeneral, FindStringOptions{Handler: func(str string) string { + return strings.ToLower(str) + }}) if resolution != "" { - return resolution + return Resolution(resolution) } - resolution = p.FindString("resolution", resolution8k, FindStringOptions{Value: "8k"}) + resolution = p.FindString("resolution", resolution4k, FindStringOptions{Value: string(Resolution4k)}) if resolution != "" { - return resolution + return Resolution(resolution) } - return p.FindString("resolution", resolutionGeneral, FindStringOptions{Handler: func(str string) string { - return strings.ToLower(str) - }}) + return Resolution(p.FindString("resolution", resolution8k, FindStringOptions{Value: string(Resolution8k)})) + } diff --git a/resolution_test.go b/resolution_test.go index ee65f8b..850df10 100644 --- a/resolution_test.go +++ b/resolution_test.go @@ -5,7 +5,7 @@ import "testing" func TestParser_GetResolution(t *testing.T) { tests := []struct { name string - want string + want Resolution }{ { name: "Annabelle.2014.1080p.PROPER.HC.WEBRip.x264.AAC.2.0-RARBG", @@ -21,15 +21,15 @@ func TestParser_GetResolution(t *testing.T) { }, { name: "The Smurfs 2 2013 COMPLETE FULL BLURAY UHD (4K) - IPT EXCLUSIVE", - want: "4k", + want: "2160p", }, { name: "A.Movie.2014.4320p.PROPER.HC.WEBRip.x264.AAC.2.0-RARBG", - want: "8k", + want: "4320p", }, { name: "[BlurayDesuYo] Shingeki no Kyojin (Season 3) 38 (BD 1920x1080 10bit FLAC) [619BE7E0].mkv", - want: "1080", + want: "1080p", }, } for _, tt := range tests {