From 5498f85304eda7e6b70789a7ca2264fb8daf2160 Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Tue, 21 Sep 2021 19:12:00 +0200 Subject: parser: Parse resolution and codec --- cmd/lftpq/main_test.go | 12 +++++-- parser/parser.go | 69 ++++++++++++++++++++++++++++++-------- parser/parser_test.go | 91 ++++++++++++++++++++++++++++++-------------------- queue/queue_test.go | 8 +++-- 4 files changed, 125 insertions(+), 55 deletions(-) diff --git a/cmd/lftpq/main_test.go b/cmd/lftpq/main_test.go index 911801d..8b78541 100644 --- a/cmd/lftpq/main_test.go +++ b/cmd/lftpq/main_test.go @@ -214,7 +214,9 @@ wait "Name": "bar", "Year": 2017, "Season": 0, - "Episode": 0 + "Episode": 0, + "Resolution": "", + "Codec": "" }, "Duplicate": false, "Merged": false @@ -232,7 +234,9 @@ wait "Name": "foo", "Year": 2018, "Season": 0, - "Episode": 0 + "Episode": 0, + "Resolution": "", + "Codec": "" }, "Duplicate": false, "Merged": false @@ -322,7 +326,9 @@ wait "Name": "foo", "Year": 2017, "Season": 0, - "Episode": 0 + "Episode": 0, + "Resolution": "", + "Codec": "" }, "Duplicate": false, "Merged": false diff --git a/parser/parser.go b/parser/parser.go index 621992d..1404542 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -19,16 +19,19 @@ var ( regexp.MustCompile(`^(?P.+?)\.(?P\d{1,2})x(?P\d{2})`), // 1x04, 01x04 regexp.MustCompile(`^(?P.+?)\.Part\.?(?P\d{1,2})`), // Part4, Part11, Part.4, Part.11 } + splitPattern = regexp.MustCompile(`[-_.]`) ) type Parser func(s string) (Media, error) type Media struct { - Release string - Name string - Year int - Season int - Episode int + Release string + Name string + Year int + Season int + Episode int + Resolution string + Codec string } func (m *Media) IsEmpty() bool { @@ -43,7 +46,12 @@ func (m *Media) Equal(o Media) bool { if m.IsEmpty() { return false } - return m.Name == o.Name && m.Season == o.Season && m.Episode == o.Episode && m.Year == o.Year + return m.Name == o.Name && + m.Season == o.Season && + m.Episode == o.Episode && + m.Year == o.Year && + m.Resolution == o.Resolution && + m.Codec == o.Codec } func (m *Media) PathIn(dir *template.Template) (string, error) { @@ -75,9 +83,11 @@ func Movie(s string) (Media, error) { return Media{}, fmt.Errorf("invalid input: %q: %s", s, err) } return Media{ - Release: s, - Name: name, - Year: year, + Release: s, + Name: name, + Year: year, + Resolution: resolution(s), + Codec: codec(s), }, nil } @@ -114,11 +124,44 @@ func Show(s string) (Media, error) { } } return Media{ - Release: s, - Name: name, - Season: season, - Episode: episode, + Release: s, + Name: name, + Season: season, + Episode: episode, + Resolution: resolution(s), + Codec: codec(s), }, nil } return Media{}, fmt.Errorf("invalid input: %q", s) } + +func findPart(s string, partFunc func(part string) bool) string { + s = strings.ToLower(s) + parts := splitPattern.Split(s, -1) + for _, part := range parts { + if partFunc(part) { + return part + } + } + return "" +} + +func resolution(s string) string { + return findPart(s, func(part string) bool { + switch part { + case "720p", "1080p", "2160p": + return true + } + return false + }) +} + +func codec(s string) string { + return findPart(s, func(part string) bool { + switch part { + case "xvid", "x264", "x265": + return true + } + return false + }) +} diff --git a/parser/parser_test.go b/parser/parser_test.go index e1bdfae..caa8a02 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -56,15 +56,17 @@ func TestDefault(t *testing.T) { } func TestMovie(t *testing.T) { - s := "Apocalypse.Now.1979.1080p.BluRay-GRP" + s := "Apocalypse.Now.1979.1080p.x264.BluRay-GRP" movie, err := Movie(s) if err != nil { t.Fatal(err) } expected := Media{ - Release: s, - Name: "Apocalypse.Now", - Year: 1979, + Release: s, + Name: "Apocalypse.Now", + Year: 1979, + Resolution: "1080p", + Codec: "x264", } if !reflect.DeepEqual(movie, expected) { t.Fatalf("Expected %+v, got %+v", expected, movie) @@ -85,52 +87,66 @@ func TestShow(t *testing.T) { }{ {"Gotham.S01E01.720p.HDTV.X264-DIMENSION", Media{ - Release: "Gotham.S01E01.720p.HDTV.X264-DIMENSION", - Name: "Gotham", - Season: 1, - Episode: 1, + Release: "Gotham.S01E01.720p.HDTV.X264-DIMENSION", + Name: "Gotham", + Season: 1, + Episode: 1, + Resolution: "720p", + Codec: "x264", }}, {"Top_Gear.21x02.720p_HDTV_x264-FoV", Media{ - Release: "Top_Gear.21x02.720p_HDTV_x264-FoV", - Name: "Top_Gear", - Season: 21, - Episode: 2, + Release: "Top_Gear.21x02.720p_HDTV_x264-FoV", + Name: "Top_Gear", + Season: 21, + Episode: 2, + Resolution: "720p", + Codec: "x264", }}, {"Eastbound.and.Down.S02E05.720p.BluRay.X264-REWARD", Media{ - Release: "Eastbound.and.Down.S02E05.720p.BluRay.X264-REWARD", - Name: "Eastbound.and.Down", - Season: 2, - Episode: 5, + Release: "Eastbound.and.Down.S02E05.720p.BluRay.X264-REWARD", + Name: "Eastbound.and.Down", + Season: 2, + Episode: 5, + Resolution: "720p", + Codec: "x264", }}, {"Olive.Kitteridge.Part.4.720p.HDTV.x264-KILLERS", Media{ - Release: "Olive.Kitteridge.Part.4.720p.HDTV.x264-KILLERS", - Name: "Olive.Kitteridge", - Season: 1, - Episode: 4, + Release: "Olive.Kitteridge.Part.4.720p.HDTV.x264-KILLERS", + Name: "Olive.Kitteridge", + Season: 1, + Episode: 4, + Resolution: "720p", + Codec: "x264", }}, {"Marilyn.The.Secret.Life.of.Marilyn.Monroe.2015.Part1.720p.HDTV.x264-W4F", Media{ - Release: "Marilyn.The.Secret.Life.of.Marilyn.Monroe.2015.Part1.720p.HDTV.x264-W4F", - Name: "Marilyn.The.Secret.Life.of.Marilyn.Monroe.2015", - Season: 1, - Episode: 1, + Release: "Marilyn.The.Secret.Life.of.Marilyn.Monroe.2015.Part1.720p.HDTV.x264-W4F", + Name: "Marilyn.The.Secret.Life.of.Marilyn.Monroe.2015", + Season: 1, + Episode: 1, + Resolution: "720p", + Codec: "x264", }}, {"The.Jinx-The.Life.and.Deaths.of.Robert.Durst.E04.1080p.BluRay.x264-ROVERS", Media{ - Release: "The.Jinx-The.Life.and.Deaths.of.Robert.Durst.E04.1080p.BluRay.x264-ROVERS", - Name: "The.Jinx-The.Life.and.Deaths.of.Robert.Durst", - Season: 1, - Episode: 4, + Release: "The.Jinx-The.Life.and.Deaths.of.Robert.Durst.E04.1080p.BluRay.x264-ROVERS", + Name: "The.Jinx-The.Life.and.Deaths.of.Robert.Durst", + Season: 1, + Episode: 4, + Resolution: "1080p", + Codec: "x264", }}, {"Adventure.Time.With.Finn.And.Jake.S01.SUBPACK.720p.BluRay.x264-DEiMOS", Media{ - Release: "Adventure.Time.With.Finn.And.Jake.S01.SUBPACK.720p.BluRay.x264-DEiMOS", - Name: "Adventure.Time.With.Finn.And.Jake", - Season: 1, - Episode: 0, + Release: "Adventure.Time.With.Finn.And.Jake.S01.SUBPACK.720p.BluRay.x264-DEiMOS", + Name: "Adventure.Time.With.Finn.And.Jake", + Season: 1, + Episode: 0, + Resolution: "720p", + Codec: "x264", }}, {"Orange.Is.The.New.Black.S02.NORDiC.SUBPACK.BluRay-REQ", Media{ @@ -141,10 +157,12 @@ func TestShow(t *testing.T) { }}, {"Lost.S01E24.Exodus.Part.2.720p.BluRay.x264-SiNNERS", Media{ - Release: "Lost.S01E24.Exodus.Part.2.720p.BluRay.x264-SiNNERS", - Name: "Lost", - Season: 1, - Episode: 24, + Release: "Lost.S01E24.Exodus.Part.2.720p.BluRay.x264-SiNNERS", + Name: "Lost", + Season: 1, + Episode: 24, + Resolution: "720p", + Codec: "x264", }}, {"Friends.S01E16.S01E17.UNCUT.DVDrip.XviD-SAiNTS", Media{ @@ -152,6 +170,7 @@ func TestShow(t *testing.T) { Name: "Friends", Season: 1, Episode: 16, + Codec: "xvid", }}, } for _, tt := range tests { diff --git a/queue/queue_test.go b/queue/queue_test.go index b98237c..3a008cd 100644 --- a/queue/queue_test.go +++ b/queue/queue_test.go @@ -211,7 +211,7 @@ func TestMergePreferringRemoteCopy(t *testing.T) { file{name: "The.Wire.S01E01.720p.BluRay.baz"}, }, nil } - q := newQueue(s, []os.FileInfo{file{name: "/remote/The.Wire.S01E01.foo"}}, readDir) + q := newQueue(s, []os.FileInfo{file{name: "/remote/The.Wire.S01E01.720p.BluRay.foo"}}, readDir) if l := len(q.Items); l != 3 { t.Fatalf("Expected length 3, got %d", l) } @@ -235,7 +235,7 @@ func TestMergePreferringLocalCopy(t *testing.T) { file{name: "The.Wire.S01E02.720p.BluRay.baz"}, }, nil } - q := newQueue(s, []os.FileInfo{file{name: "/remote/The.Wire.S01E01.foo"}}, readDir) + q := newQueue(s, []os.FileInfo{file{name: "/remote/The.Wire.S01E01.720p.BluRay.foo"}}, readDir) if l := len(q.Items); l != 2 { t.Fatalf("Expected length 2, got %d", l) } @@ -402,7 +402,9 @@ func TestMarshalJSON(t *testing.T) { "Name": "The.Wire", "Year": 0, "Season": 1, - "Episode": 1 + "Episode": 1, + "Resolution": "", + "Codec": "" }, "Duplicate": false, "Merged": false -- cgit v1.2.3