From eeb05ae78b94f9fca1520b6d8f62c5376dc8b398 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Wed, 30 Apr 2025 13:52:57 +0200 Subject: [PATCH] Add and fix some tests --- main.go | 4 ++-- media.go | 57 +++++++++++++++++++++++++++++++++++++-------------- media_test.go | 11 +++++----- 3 files changed, 50 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index be2d8be..9f95b82 100644 --- a/main.go +++ b/main.go @@ -72,7 +72,7 @@ func removeSuffix(suffix string) filepath.WalkFunc { return err } - element := newMediaElement(p) + element := newMediaElementWithParent(p) fmt.Println(element.Path()) dir := filepath.Dir(p) @@ -102,7 +102,7 @@ func normalize(p string, f os.DirEntry, err error) error { return err } - element := newMediaElement(p) + element := newMediaElementWithParent(p) newPath := element.Path() if newPath == p { return nil diff --git a/media.go b/media.go index 4dcf523..4989c88 100644 --- a/media.go +++ b/media.go @@ -4,6 +4,7 @@ import ( "fmt" "path/filepath" "regexp" + "slices" "strconv" "strings" ) @@ -18,23 +19,45 @@ type mediaElement struct { Extension string } -func newMediaElement(p string) *mediaElement { - episodeMatch := regexp.MustCompile(`[.\- ]?(S\d\dE\d\d)[.\- ]?`) - yearMatch := regexp.MustCompile(`[.\- ]?(\d{4})[.\- ]?`) - +func newMediaElementWithParent(p string) *mediaElement { name := filepath.Base(p) + parentDir := filepath.Dir(p) ext := filepath.Ext(name) name = strings.TrimSuffix(name, ext) - parentDir := filepath.Dir(p) - // use parent directory if it starts similarly and is longer dir := filepath.Base(parentDir) - if len(dir) >= len(name) && dir[0:3] == name[0:3] { - parentDir = filepath.Dir(parentDir) - name = dir - fmt.Println(dir) + + fileElement := newMediaElement(name, ext, parentDir) + //fmt.Println("file: ", fileElement) + directoryElement := newMediaElement(dir, ext, parentDir) + //fmt.Println("directory: ", directoryElement) + + if fileElement.Name == "" { + fileElement.Name = directoryElement.Name } + if fileElement.Episode == "" { + if directoryElement.Episode != "" { + fileElement.Name = directoryElement.Name + } + fileElement.Episode = directoryElement.Episode + fileElement.Title = directoryElement.Title + } + + // append to fileElement.Tags if not already present + for _, tag := range directoryElement.Tags { + if !slices.Contains(fileElement.Tags, tag) { + fileElement.Tags = append(fileElement.Tags, tag) + } + } + + return fileElement +} + +func newMediaElement(name, ext, parentDir string) *mediaElement { + episodeMatch := regexp.MustCompile(`[.\- ]?(S\d\dE\d\d)[.\- ]?`) + yearMatch := regexp.MustCompile(`[.\- ]?(\d{4})[.\- ]?`) + name = strings.ReplaceAll(name, ".", " ") name = strings.ReplaceAll(name, "-", " ") name = strings.ReplaceAll(name, "_", " ") @@ -59,7 +82,9 @@ func newMediaElement(p string) *mediaElement { if len(match) > 0 { year, err := strconv.Atoi(match[1]) if err == nil { - element.Year = year + if year >= 1800 { + element.Year = year + } } indexOfYear := strings.Index(name, match[0]) name = strings.Replace(name, match[0], "", -1) @@ -75,13 +100,15 @@ func newMediaElement(p string) *mediaElement { upperWord := strings.ToUpper(word) switch upperWord { case "SYNCED": - fallthrough case "BD": - fallthrough case "DL": - fallthrough case "TVS": - continue + case "TVR": + case "AC3": + case "5": + case "1": + case "WEB": + // ignore case "EN": fallthrough case "DE": diff --git a/media_test.go b/media_test.go index 3bdd362..2383bd7 100644 --- a/media_test.go +++ b/media_test.go @@ -7,7 +7,7 @@ import ( ) func Test(t *testing.T) { - m := newMediaElement("Misfits - S02E03 - EN FORCED") + m := newMediaElementWithParent("Misfits - S02E03 - EN FORCED") result := "Misfits - S02E03 EN FORCED" if m.Path() != result { t.Error("Expected " + result + ", got " + m.Path()) @@ -18,13 +18,14 @@ func TestExamples(t *testing.T) { // list of strings with expected output examples := [][]string{ {"Speak.No.Evil.2024.German.EAC3.DL.1080p.BluRay.x265-VECTOR.nfo", "Speak No Evil (2024) DE 1080p x265.nfo"}, - {"Speak.No.Evil.2024.German.EAC3.DL.1080p.BluRay.x265-VECTOR/Speak.No.Evil.2024.nfo", "Speak No Evil (2024) DE 1080p x265.nfo"}, + {"Speak.No.Evil.2024.German.EAC3.DL.1080p.BluRay.x265-VECTOR/Speak.No.Evil.2024.nfo", "Speak.No.Evil.2024.German.EAC3.DL.1080p.BluRay.x265-VECTOR/Speak No Evil (2024) DE 1080p x265.nfo"}, {"Speak.No.Evil.2024.nfo", "Speak No Evil (2024).nfo"}, - {"Miracles.from.Heaven.2016.1080p.BluRay.X264-AMIABLE/Miracles.from.Heaven.2016.1080p.BluRay.X264-AMIABLE.mkv", "Miracles from Heaven (2016) 1080p x264.mkv"}, - {"Level_16_2018_1080p_BluRay_H264_AAC/Level.16.2018.1080p.BluRay.H264.AAC.mp4", "Level 16 (2018) 1080p x264.mp4"}, + {"Miracles.from.Heaven.2016.1080p.BluRay.X264-AMIABLE/Miracles.from.Heaven.2016.1080p.BluRay.X264-AMIABLE.mkv", "Miracles.from.Heaven.2016.1080p.BluRay.X264-AMIABLE/Miracles from Heaven (2016) 1080p x264.mkv"}, + {"Level_16_2018_1080p_BluRay_H264_AAC/Level.16.2018.1080p.BluRay.H264.AAC.mp4", "Level_16_2018_1080p_BluRay_H264_AAC/Level 16 (2018) 1080p x264.mp4"}, + {"Scorpion.S02E24.Duell.der.Genies.GERMAN.5.1.DL.AC3.1080p.WEB-DL.x264-TvR/tvr-scorpion-s02e24-1080p.mkv", "Scorpion.S02E24.Duell.der.Genies.GERMAN.5.1.DL.AC3.1080p.WEB-DL.x264-TvR/Scorpion - S02E24 - Duell der Genies DE 1080p x264.mkv"}, } for _, example := range examples { - element := newMediaElement(example[0]) + element := newMediaElementWithParent(example[0]) if expected, got := example[1], element.Path(); expected != got { t.Errorf("Result not expected for %s:\n%v", example[0], diff.LineDiff(expected, got)) }