124 lines
2.5 KiB
Go
124 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type mediaElement struct {
|
|
Directory string
|
|
Name string
|
|
Year int
|
|
Episode string
|
|
Title string
|
|
Tags []string
|
|
Extension string
|
|
}
|
|
|
|
func newMediaElement(p string) *mediaElement {
|
|
dir := filepath.Dir(p)
|
|
name := filepath.Base(p)
|
|
ext := filepath.Ext(name)
|
|
name = trimSuffix(name, ext)
|
|
|
|
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])
|
|
}
|
|
|
|
element := &mediaElement{
|
|
Directory: dir,
|
|
Extension: ext,
|
|
Name: name,
|
|
Episode: episode,
|
|
}
|
|
|
|
if episode == "" {
|
|
title = name
|
|
element.Name = ""
|
|
}
|
|
words := strings.Split(title, " ")
|
|
titleWords := []string{}
|
|
for i := len(words) - 1; i >= 0; i-- {
|
|
word := words[i]
|
|
switch word {
|
|
case "EN":
|
|
fallthrough
|
|
case "DE":
|
|
fallthrough
|
|
case "FORCED":
|
|
fallthrough
|
|
case "3D":
|
|
fallthrough
|
|
case "UNCUT":
|
|
element.Tags = append(element.Tags, word)
|
|
default:
|
|
if len(word) == 4 && word >= "1800" && word <= "2100" {
|
|
element.Year, _ = strconv.Atoi(word)
|
|
} else {
|
|
titleWords = append(titleWords, word)
|
|
}
|
|
}
|
|
}
|
|
|
|
title = ""
|
|
first := true
|
|
for i := len(titleWords) - 1; i >= 0; i-- {
|
|
if !first {
|
|
title += " "
|
|
}
|
|
title += titleWords[i]
|
|
first = false
|
|
}
|
|
element.Title = title
|
|
return element
|
|
}
|
|
|
|
func (element *mediaElement) String() string {
|
|
result := ""
|
|
result += fmt.Sprintf("Directory: %s\n", element.Directory)
|
|
result += fmt.Sprintf("Name: %s\n", element.Name)
|
|
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 += "Tags:"
|
|
for _, tag := range element.Tags {
|
|
result += " " + tag
|
|
}
|
|
return result
|
|
}
|
|
|
|
func (element *mediaElement) Path() string {
|
|
result := element.Name
|
|
if element.Episode != "" {
|
|
result += " - " + element.Episode
|
|
}
|
|
if element.Title != "" {
|
|
if result != "" {
|
|
result += " - "
|
|
}
|
|
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) + ")"
|
|
}
|
|
result += element.Extension
|
|
return filepath.Join(element.Directory, result)
|
|
|
|
}
|