diff --git a/main.go b/main.go index 2368528..63135ad 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "path/filepath" ) func main() { @@ -17,9 +18,43 @@ func main() { fallthrough case "rs": suffix := args[1] + removeSuffix(suffix) break default: fmt.Println("Invalid action specified") 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 + } +} diff --git a/media.go b/media.go new file mode 100644 index 0000000..b32009e --- /dev/null +++ b/media.go @@ -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 +} diff --git a/util.go b/util.go new file mode 100644 index 0000000..f429b5f --- /dev/null +++ b/util.go @@ -0,0 +1,12 @@ +package main + +import ( + "strings" +) + +func trimSuffix(s, suffix string) string { + if strings.HasSuffix(s, suffix) { + s = s[:len(s)-len(suffix)] + } + return s +}