feat: Use regexp for year/episode match
This commit is contained in:
parent
3ab632853d
commit
f0ec6d4519
4
main.go
4
main.go
@ -135,12 +135,10 @@ func unpack(p string, f os.FileInfo, err error) error {
|
||||
filename = strings.TrimSuffix(filename, ext)
|
||||
ext = filepath.Ext(filename)
|
||||
if ext == "" {
|
||||
fmt.Println(p)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Printf("Error executing: %v\n", err)
|
||||
}
|
||||
fmt.Println(p)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -148,12 +146,10 @@ func unpack(p string, f os.FileInfo, err error) error {
|
||||
ext = ext[5:]
|
||||
part, err := strconv.Atoi(ext)
|
||||
if err == nil && part == 1 {
|
||||
fmt.Println(p)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Printf("Error executing: %v\n", err)
|
||||
}
|
||||
fmt.Println(p)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
84
media.go
84
media.go
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@ -18,42 +19,50 @@ type mediaElement struct {
|
||||
}
|
||||
|
||||
func newMediaElement(p string) *mediaElement {
|
||||
dir := filepath.Dir(p)
|
||||
name := filepath.Base(p)
|
||||
ext := filepath.Ext(name)
|
||||
name = strings.TrimSuffix(name, ext)
|
||||
episodeMatch := regexp.MustCompile(`[.\- ]?(S\d\dE\d\d)[.\- ]?`)
|
||||
yearMatch := regexp.MustCompile(`[.\- ]?(\d{4})[.\- ]?`)
|
||||
|
||||
name = strings.ReplaceAll(name, ".", " ")
|
||||
dash := strings.Index(name, "-")
|
||||
episode := ""
|
||||
if dash != -1 {
|
||||
episode = strings.TrimSpace(name[dash+1:])
|
||||
name = strings.TrimSpace(name[:dash])
|
||||
}
|
||||
|
||||
dash = strings.Index(episode, "-")
|
||||
title := ""
|
||||
if dash != -1 {
|
||||
title = strings.TrimSpace(episode[dash+1:])
|
||||
episode = strings.TrimSpace(episode[:dash])
|
||||
}
|
||||
text := filepath.Base(p)
|
||||
ext := filepath.Ext(text)
|
||||
text = strings.TrimSuffix(text, ext)
|
||||
text = strings.ReplaceAll(text, ".", " ")
|
||||
text = strings.ReplaceAll(text, "-", " ")
|
||||
|
||||
element := &mediaElement{
|
||||
Directory: dir,
|
||||
Directory: filepath.Dir(p),
|
||||
Extension: ext,
|
||||
Name: name,
|
||||
Episode: episode,
|
||||
}
|
||||
|
||||
if episode == "" {
|
||||
title = name
|
||||
element.Name = ""
|
||||
title := ""
|
||||
// get first group of regex match from episodeMatch
|
||||
match := episodeMatch.FindStringSubmatch(text)
|
||||
if len(match) > 0 {
|
||||
element.Episode = match[1]
|
||||
indexOfEpisode := strings.Index(text, match[0])
|
||||
text = strings.Replace(text, match[0], "", -1)
|
||||
title = text[indexOfEpisode:]
|
||||
text = text[:indexOfEpisode]
|
||||
}
|
||||
|
||||
match = yearMatch.FindStringSubmatch(text)
|
||||
if len(match) > 0 {
|
||||
year, err := strconv.Atoi(match[1])
|
||||
if err == nil {
|
||||
element.Year = year
|
||||
}
|
||||
indexOfYear := strings.Index(text, match[0])
|
||||
text = strings.Replace(text, match[0], "", -1)
|
||||
title = text[indexOfYear:]
|
||||
text = text[:indexOfYear]
|
||||
}
|
||||
|
||||
element.Name = strings.TrimSpace(text)
|
||||
words := strings.Split(title, " ")
|
||||
titleWords := []string{}
|
||||
for i := len(words) - 1; i >= 0; i-- {
|
||||
word := words[i]
|
||||
switch word {
|
||||
upperWord := strings.ToUpper(word)
|
||||
switch upperWord {
|
||||
case "EN":
|
||||
fallthrough
|
||||
case "DE":
|
||||
@ -62,14 +71,16 @@ func newMediaElement(p string) *mediaElement {
|
||||
fallthrough
|
||||
case "3D":
|
||||
fallthrough
|
||||
case "1080P":
|
||||
fallthrough
|
||||
case "X265":
|
||||
fallthrough
|
||||
case "UNCUT":
|
||||
element.Tags = append(element.Tags, word)
|
||||
element.Tags = append(element.Tags, upperWord)
|
||||
case "GERMAN":
|
||||
element.Tags = append(element.Tags, "DE")
|
||||
default:
|
||||
if len(word) == 4 && word >= "1800" && word <= "2100" {
|
||||
element.Year, _ = strconv.Atoi(word)
|
||||
} else {
|
||||
titleWords = append(titleWords, word)
|
||||
}
|
||||
titleWords = append(titleWords, word)
|
||||
}
|
||||
}
|
||||
|
||||
@ -82,7 +93,9 @@ func newMediaElement(p string) *mediaElement {
|
||||
title += titleWords[i]
|
||||
first = false
|
||||
}
|
||||
element.Title = title
|
||||
if element.Episode != "" {
|
||||
element.Title = strings.TrimSpace(title)
|
||||
}
|
||||
return element
|
||||
}
|
||||
|
||||
@ -93,6 +106,7 @@ func (element *mediaElement) String() string {
|
||||
result += fmt.Sprintf("Episode: %s\n", element.Episode)
|
||||
result += fmt.Sprintf("Title: %s\n", element.Title)
|
||||
result += fmt.Sprintf("Extension: %s\n", element.Extension)
|
||||
result += fmt.Sprintf("Year: %d\n", element.Year)
|
||||
result += "Tags:"
|
||||
for _, tag := range element.Tags {
|
||||
result += " " + tag
|
||||
@ -111,12 +125,12 @@ func (element *mediaElement) Path() string {
|
||||
}
|
||||
result += element.Title
|
||||
}
|
||||
for i := len(element.Tags) - 1; i >= 0; i-- {
|
||||
result += " " + element.Tags[i]
|
||||
}
|
||||
if element.Year != 0 {
|
||||
result += " (" + strconv.Itoa(element.Year) + ")"
|
||||
}
|
||||
for i := len(element.Tags) - 1; i >= 0; i-- {
|
||||
result += " " + element.Tags[i]
|
||||
}
|
||||
result += element.Extension
|
||||
return filepath.Join(element.Directory, result)
|
||||
|
||||
|
@ -9,3 +9,16 @@ func Test(t *testing.T) {
|
||||
t.Error("Expected " + result + ", got " + m.Path())
|
||||
}
|
||||
}
|
||||
|
||||
func TestExamples(t *testing.T) {
|
||||
// list of strings with expected output
|
||||
examples := [][]string{
|
||||
{"Speak.No.Evil.2024.German.EAC3.DL.1080p.BluRay.x265-VECTOR.mkv", "Speak No Evil (2024) DE 1080P X265.mkv"},
|
||||
}
|
||||
for _, example := range examples {
|
||||
element := newMediaElement(example[0])
|
||||
if expected, got := example[1], element.Path(); expected != got {
|
||||
t.Errorf("Expected %s, got %s", expected, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user