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)
|
filename = strings.TrimSuffix(filename, ext)
|
||||||
ext = filepath.Ext(filename)
|
ext = filepath.Ext(filename)
|
||||||
if ext == "" {
|
if ext == "" {
|
||||||
fmt.Println(p)
|
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error executing: %v\n", err)
|
fmt.Printf("Error executing: %v\n", err)
|
||||||
}
|
}
|
||||||
fmt.Println(p)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,12 +146,10 @@ func unpack(p string, f os.FileInfo, err error) error {
|
|||||||
ext = ext[5:]
|
ext = ext[5:]
|
||||||
part, err := strconv.Atoi(ext)
|
part, err := strconv.Atoi(ext)
|
||||||
if err == nil && part == 1 {
|
if err == nil && part == 1 {
|
||||||
fmt.Println(p)
|
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error executing: %v\n", err)
|
fmt.Printf("Error executing: %v\n", err)
|
||||||
}
|
}
|
||||||
fmt.Println(p)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
82
media.go
82
media.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -18,42 +19,50 @@ type mediaElement struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newMediaElement(p string) *mediaElement {
|
func newMediaElement(p string) *mediaElement {
|
||||||
dir := filepath.Dir(p)
|
episodeMatch := regexp.MustCompile(`[.\- ]?(S\d\dE\d\d)[.\- ]?`)
|
||||||
name := filepath.Base(p)
|
yearMatch := regexp.MustCompile(`[.\- ]?(\d{4})[.\- ]?`)
|
||||||
ext := filepath.Ext(name)
|
|
||||||
name = strings.TrimSuffix(name, ext)
|
|
||||||
|
|
||||||
name = strings.ReplaceAll(name, ".", " ")
|
text := filepath.Base(p)
|
||||||
dash := strings.Index(name, "-")
|
ext := filepath.Ext(text)
|
||||||
episode := ""
|
text = strings.TrimSuffix(text, ext)
|
||||||
if dash != -1 {
|
text = strings.ReplaceAll(text, ".", " ")
|
||||||
episode = strings.TrimSpace(name[dash+1:])
|
text = strings.ReplaceAll(text, "-", " ")
|
||||||
name = strings.TrimSpace(name[:dash])
|
|
||||||
}
|
|
||||||
|
|
||||||
dash = strings.Index(episode, "-")
|
|
||||||
title := ""
|
|
||||||
if dash != -1 {
|
|
||||||
title = strings.TrimSpace(episode[dash+1:])
|
|
||||||
episode = strings.TrimSpace(episode[:dash])
|
|
||||||
}
|
|
||||||
|
|
||||||
element := &mediaElement{
|
element := &mediaElement{
|
||||||
Directory: dir,
|
Directory: filepath.Dir(p),
|
||||||
Extension: ext,
|
Extension: ext,
|
||||||
Name: name,
|
|
||||||
Episode: episode,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if episode == "" {
|
title := ""
|
||||||
title = name
|
// get first group of regex match from episodeMatch
|
||||||
element.Name = ""
|
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, " ")
|
words := strings.Split(title, " ")
|
||||||
titleWords := []string{}
|
titleWords := []string{}
|
||||||
for i := len(words) - 1; i >= 0; i-- {
|
for i := len(words) - 1; i >= 0; i-- {
|
||||||
word := words[i]
|
word := words[i]
|
||||||
switch word {
|
upperWord := strings.ToUpper(word)
|
||||||
|
switch upperWord {
|
||||||
case "EN":
|
case "EN":
|
||||||
fallthrough
|
fallthrough
|
||||||
case "DE":
|
case "DE":
|
||||||
@ -62,16 +71,18 @@ func newMediaElement(p string) *mediaElement {
|
|||||||
fallthrough
|
fallthrough
|
||||||
case "3D":
|
case "3D":
|
||||||
fallthrough
|
fallthrough
|
||||||
|
case "1080P":
|
||||||
|
fallthrough
|
||||||
|
case "X265":
|
||||||
|
fallthrough
|
||||||
case "UNCUT":
|
case "UNCUT":
|
||||||
element.Tags = append(element.Tags, word)
|
element.Tags = append(element.Tags, upperWord)
|
||||||
|
case "GERMAN":
|
||||||
|
element.Tags = append(element.Tags, "DE")
|
||||||
default:
|
default:
|
||||||
if len(word) == 4 && word >= "1800" && word <= "2100" {
|
|
||||||
element.Year, _ = strconv.Atoi(word)
|
|
||||||
} else {
|
|
||||||
titleWords = append(titleWords, word)
|
titleWords = append(titleWords, word)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
title = ""
|
title = ""
|
||||||
first := true
|
first := true
|
||||||
@ -82,7 +93,9 @@ func newMediaElement(p string) *mediaElement {
|
|||||||
title += titleWords[i]
|
title += titleWords[i]
|
||||||
first = false
|
first = false
|
||||||
}
|
}
|
||||||
element.Title = title
|
if element.Episode != "" {
|
||||||
|
element.Title = strings.TrimSpace(title)
|
||||||
|
}
|
||||||
return element
|
return element
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,6 +106,7 @@ func (element *mediaElement) String() string {
|
|||||||
result += fmt.Sprintf("Episode: %s\n", element.Episode)
|
result += fmt.Sprintf("Episode: %s\n", element.Episode)
|
||||||
result += fmt.Sprintf("Title: %s\n", element.Title)
|
result += fmt.Sprintf("Title: %s\n", element.Title)
|
||||||
result += fmt.Sprintf("Extension: %s\n", element.Extension)
|
result += fmt.Sprintf("Extension: %s\n", element.Extension)
|
||||||
|
result += fmt.Sprintf("Year: %d\n", element.Year)
|
||||||
result += "Tags:"
|
result += "Tags:"
|
||||||
for _, tag := range element.Tags {
|
for _, tag := range element.Tags {
|
||||||
result += " " + tag
|
result += " " + tag
|
||||||
@ -111,12 +125,12 @@ func (element *mediaElement) Path() string {
|
|||||||
}
|
}
|
||||||
result += element.Title
|
result += element.Title
|
||||||
}
|
}
|
||||||
for i := len(element.Tags) - 1; i >= 0; i-- {
|
|
||||||
result += " " + element.Tags[i]
|
|
||||||
}
|
|
||||||
if element.Year != 0 {
|
if element.Year != 0 {
|
||||||
result += " (" + strconv.Itoa(element.Year) + ")"
|
result += " (" + strconv.Itoa(element.Year) + ")"
|
||||||
}
|
}
|
||||||
|
for i := len(element.Tags) - 1; i >= 0; i-- {
|
||||||
|
result += " " + element.Tags[i]
|
||||||
|
}
|
||||||
result += element.Extension
|
result += element.Extension
|
||||||
return filepath.Join(element.Directory, result)
|
return filepath.Join(element.Directory, result)
|
||||||
|
|
||||||
|
@ -9,3 +9,16 @@ func Test(t *testing.T) {
|
|||||||
t.Error("Expected " + result + ", got " + m.Path())
|
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