feat: use info from dir if longer than filename

This commit is contained in:
Jan Bader 2025-01-29 13:54:17 +01:00
parent b680f59f9d
commit 812f53ac16

View File

@ -22,41 +22,51 @@ func newMediaElement(p string) *mediaElement {
episodeMatch := regexp.MustCompile(`[.\- ]?(S\d\dE\d\d)[.\- ]?`) episodeMatch := regexp.MustCompile(`[.\- ]?(S\d\dE\d\d)[.\- ]?`)
yearMatch := regexp.MustCompile(`[.\- ]?(\d{4})[.\- ]?`) yearMatch := regexp.MustCompile(`[.\- ]?(\d{4})[.\- ]?`)
text := filepath.Base(p) name := filepath.Base(p)
ext := filepath.Ext(text) ext := filepath.Ext(name)
text = strings.TrimSuffix(text, ext) name = strings.TrimSuffix(name, ext)
text = strings.ReplaceAll(text, ".", " ")
text = strings.ReplaceAll(text, "-", " ") 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)
}
name = strings.ReplaceAll(name, ".", " ")
name = strings.ReplaceAll(name, "-", " ")
element := &mediaElement{ element := &mediaElement{
Directory: filepath.Dir(p), Directory: parentDir,
Extension: ext, Extension: ext,
} }
title := "" title := ""
// get first group of regex match from episodeMatch // get first group of regex match from episodeMatch
match := episodeMatch.FindStringSubmatch(text) match := episodeMatch.FindStringSubmatch(name)
if len(match) > 0 { if len(match) > 0 {
element.Episode = match[1] element.Episode = match[1]
indexOfEpisode := strings.Index(text, match[0]) indexOfEpisode := strings.Index(name, match[0])
text = strings.Replace(text, match[0], "", -1) name = strings.Replace(name, match[0], "", -1)
title = text[indexOfEpisode:] title = name[indexOfEpisode:]
text = text[:indexOfEpisode] name = name[:indexOfEpisode]
} }
match = yearMatch.FindStringSubmatch(text) match = yearMatch.FindStringSubmatch(name)
if len(match) > 0 { if len(match) > 0 {
year, err := strconv.Atoi(match[1]) year, err := strconv.Atoi(match[1])
if err == nil { if err == nil {
element.Year = year element.Year = year
} }
indexOfYear := strings.Index(text, match[0]) indexOfYear := strings.Index(name, match[0])
text = strings.Replace(text, match[0], "", -1) name = strings.Replace(name, match[0], "", -1)
title = text[indexOfYear:] title = name[indexOfYear:]
text = text[:indexOfYear] name = name[:indexOfYear]
} }
element.Name = strings.TrimSpace(text) element.Name = strings.TrimSpace(name)
words := strings.Split(title, " ") words := strings.Split(title, " ")
titleWords := []string{} titleWords := []string{}
for i := len(words) - 1; i >= 0; i-- { for i := len(words) - 1; i >= 0; i-- {