Implement normalize & try to implement unpack
This commit is contained in:
parent
73cc5d7f55
commit
66babd15c8
106
main.go
106
main.go
@ -1,11 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var dryrun bool
|
||||
var execCmd string
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
if len(args) == 0 {
|
||||
@ -13,32 +20,56 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
switch args[0] {
|
||||
flag.BoolVar(&dryrun, "n", false, "Show what would have been done.")
|
||||
flag.StringVar(&execCmd, "exec", "", "Run commant on matching file.")
|
||||
flag.Parse()
|
||||
|
||||
fmt.Println(execCmd)
|
||||
fmt.Println(dryrun)
|
||||
|
||||
switch flag.Arg(0) {
|
||||
case "normalize":
|
||||
fallthrough
|
||||
case "n":
|
||||
filepath.Walk(".", normalize)
|
||||
case "removesuffix":
|
||||
fallthrough
|
||||
case "rs":
|
||||
suffix := args[1]
|
||||
filepath.Walk(".", removeSuffix(suffix))
|
||||
removeSuffix(suffix)
|
||||
break
|
||||
case "unpack":
|
||||
fallthrough
|
||||
case "up":
|
||||
filepath.Walk(".", unpack)
|
||||
default:
|
||||
fmt.Println("Invalid action specified")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func removeSuffix(suffix string) {
|
||||
filepath.Walk(".", removeSuffixFunc(suffix))
|
||||
func skipDir(filename string) (bool, error) {
|
||||
if filename == "." || filename == ".." {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if filename == "Bin" {
|
||||
return true, filepath.SkipDir
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func removeSuffixFunc(suffix string) filepath.WalkFunc {
|
||||
func removeSuffix(suffix string) filepath.WalkFunc {
|
||||
return func(p string, f os.FileInfo, err error) error {
|
||||
filename := f.Name()
|
||||
if filename == "." || filename == ".." {
|
||||
return nil
|
||||
if skip, err := skipDir(filename); skip {
|
||||
return err
|
||||
}
|
||||
|
||||
element := newMediaElement(p)
|
||||
fmt.Println(element.String())
|
||||
fmt.Println(element.Path())
|
||||
|
||||
dir := filepath.Dir(p)
|
||||
ext := filepath.Ext(filename)
|
||||
@ -58,3 +89,64 @@ func removeSuffixFunc(suffix string) filepath.WalkFunc {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func normalize(p string, f os.FileInfo, err error) error {
|
||||
filename := f.Name()
|
||||
if skip, err := skipDir(filename); skip {
|
||||
return err
|
||||
}
|
||||
|
||||
element := newMediaElement(p)
|
||||
newPath := element.Path()
|
||||
if newPath != p {
|
||||
if dryrun {
|
||||
fmt.Printf("Would move %s to %s\n", p, newPath)
|
||||
} else {
|
||||
os.Rename(p, newPath)
|
||||
fmt.Printf("%s => %s\n", p, newPath)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func unpack(p string, f os.FileInfo, err error) error {
|
||||
filename := f.Name()
|
||||
if skip, err := skipDir(filename); skip {
|
||||
return err
|
||||
}
|
||||
|
||||
ext := filepath.Ext(filename)
|
||||
ext = strings.ToLower(ext)
|
||||
if ext != ".rar" {
|
||||
return nil
|
||||
}
|
||||
|
||||
cmd := exec.Command(execCmd, p)
|
||||
filename = 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
|
||||
}
|
||||
|
||||
if strings.HasPrefix(ext, ".part") {
|
||||
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
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
38
media.go
38
media.go
@ -21,18 +21,18 @@ func newMediaElement(p string) *mediaElement {
|
||||
ext := filepath.Ext(name)
|
||||
name = trimSuffix(name, ext)
|
||||
|
||||
dash := strings.Index(name, " - ")
|
||||
dash := strings.Index(name, "-")
|
||||
episode := ""
|
||||
if dash != -1 {
|
||||
episode = name[dash+3:]
|
||||
name = name[:dash]
|
||||
episode = strings.TrimSpace(name[dash+1:])
|
||||
name = strings.TrimSpace(name[:dash])
|
||||
}
|
||||
|
||||
dash = strings.Index(episode, " - ")
|
||||
dash = strings.Index(episode, "-")
|
||||
title := ""
|
||||
if dash != -1 {
|
||||
title = episode[dash+3:]
|
||||
episode = episode[:dash]
|
||||
title = strings.TrimSpace(episode[dash+1:])
|
||||
episode = strings.TrimSpace(episode[:dash])
|
||||
}
|
||||
|
||||
element := &mediaElement{
|
||||
@ -40,7 +40,6 @@ func newMediaElement(p string) *mediaElement {
|
||||
Extension: ext,
|
||||
Name: name,
|
||||
Episode: episode,
|
||||
Title: title,
|
||||
}
|
||||
|
||||
words := strings.Split(title, " ")
|
||||
@ -63,7 +62,16 @@ func newMediaElement(p string) *mediaElement {
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("%v\n", titleWords)
|
||||
title = ""
|
||||
first := true
|
||||
for i := len(titleWords) - 1; i >= 0; i-- {
|
||||
if !first {
|
||||
title += " "
|
||||
}
|
||||
title += titleWords[i]
|
||||
first = false
|
||||
}
|
||||
element.Title = title
|
||||
return element
|
||||
}
|
||||
|
||||
@ -80,3 +88,17 @@ func (element *mediaElement) String() string {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (element *mediaElement) Path() string {
|
||||
result := filepath.Join(element.Directory, element.Name)
|
||||
if element.Episode != "" {
|
||||
result += " - " + element.Episode
|
||||
}
|
||||
if element.Title != "" {
|
||||
result += " - " + element.Title
|
||||
}
|
||||
for i := len(element.Tags) - 1; i >= 0; i-- {
|
||||
result += " " + element.Tags[i]
|
||||
}
|
||||
return result + element.Extension
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user