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

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