Add and fix some tests

This commit is contained in:
Jan Bader 2025-04-30 13:52:57 +02:00
parent 9550fea87d
commit eeb05ae78b
3 changed files with 50 additions and 22 deletions

View File

@ -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

View File

@ -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,8 +82,10 @@ func newMediaElement(p string) *mediaElement {
if len(match) > 0 {
year, err := strconv.Atoi(match[1])
if err == nil {
if year >= 1800 {
element.Year = year
}
}
indexOfYear := strings.Index(name, match[0])
name = strings.Replace(name, match[0], "", -1)
title = name[indexOfYear:]
@ -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":

View File

@ -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))
}