Skip to content

Commit

Permalink
feat: add resolution constants
Browse files Browse the repository at this point in the history
  • Loading branch information
ProfChaos committed Oct 7, 2024
1 parent b370b41 commit e497505
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 41 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.direnv
.envrc
flake.lock
flake.nix
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
26 changes: 12 additions & 14 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
49 changes: 37 additions & 12 deletions resolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)}))

}
8 changes: 4 additions & 4 deletions resolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 {
Expand Down

0 comments on commit e497505

Please sign in to comment.