Work on media-parsing
This commit is contained in:
parent
1ed0b0b201
commit
73cc5d7f55
35
main.go
35
main.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -17,9 +18,43 @@ func main() {
|
|||||||
fallthrough
|
fallthrough
|
||||||
case "rs":
|
case "rs":
|
||||||
suffix := args[1]
|
suffix := args[1]
|
||||||
|
removeSuffix(suffix)
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
fmt.Println("Invalid action specified")
|
fmt.Println("Invalid action specified")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func removeSuffix(suffix string) {
|
||||||
|
filepath.Walk(".", removeSuffixFunc(suffix))
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeSuffixFunc(suffix string) filepath.WalkFunc {
|
||||||
|
return func(p string, f os.FileInfo, err error) error {
|
||||||
|
filename := f.Name()
|
||||||
|
if filename == "." || filename == ".." {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
element := newMediaElement(p)
|
||||||
|
fmt.Println(element.String())
|
||||||
|
|
||||||
|
dir := filepath.Dir(p)
|
||||||
|
ext := filepath.Ext(filename)
|
||||||
|
filenameNoExt := trimSuffix(filename, ext)
|
||||||
|
filenameNoSuff := trimSuffix(filenameNoExt, suffix)
|
||||||
|
if filenameNoSuff != filenameNoExt {
|
||||||
|
newFilename := filenameNoSuff + ext
|
||||||
|
|
||||||
|
err := os.Rename(filepath.Join(dir, filename), filepath.Join(dir, newFilename))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Could not rename %s: %v\n", filename, err)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Renamed " + filename + " to " + newFilename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
82
media.go
Normal file
82
media.go
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type mediaElement struct {
|
||||||
|
Directory string
|
||||||
|
Name string
|
||||||
|
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)
|
||||||
|
|
||||||
|
dash := strings.Index(name, " - ")
|
||||||
|
episode := ""
|
||||||
|
if dash != -1 {
|
||||||
|
episode = name[dash+3:]
|
||||||
|
name = name[:dash]
|
||||||
|
}
|
||||||
|
|
||||||
|
dash = strings.Index(episode, " - ")
|
||||||
|
title := ""
|
||||||
|
if dash != -1 {
|
||||||
|
title = episode[dash+3:]
|
||||||
|
episode = episode[:dash]
|
||||||
|
}
|
||||||
|
|
||||||
|
element := &mediaElement{
|
||||||
|
Directory: dir,
|
||||||
|
Extension: ext,
|
||||||
|
Name: name,
|
||||||
|
Episode: episode,
|
||||||
|
Title: title,
|
||||||
|
}
|
||||||
|
|
||||||
|
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:
|
||||||
|
titleWords = append(titleWords, word)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("%v\n", titleWords)
|
||||||
|
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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user