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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var dryrun bool
|
||||||
|
var execCmd string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
args := os.Args[1:]
|
args := os.Args[1:]
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
@ -13,32 +20,56 @@ func main() {
|
|||||||
return
|
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":
|
case "removesuffix":
|
||||||
fallthrough
|
fallthrough
|
||||||
case "rs":
|
case "rs":
|
||||||
suffix := args[1]
|
suffix := args[1]
|
||||||
|
filepath.Walk(".", removeSuffix(suffix))
|
||||||
removeSuffix(suffix)
|
removeSuffix(suffix)
|
||||||
break
|
break
|
||||||
|
case "unpack":
|
||||||
|
fallthrough
|
||||||
|
case "up":
|
||||||
|
filepath.Walk(".", unpack)
|
||||||
default:
|
default:
|
||||||
fmt.Println("Invalid action specified")
|
fmt.Println("Invalid action specified")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeSuffix(suffix string) {
|
func skipDir(filename string) (bool, error) {
|
||||||
filepath.Walk(".", removeSuffixFunc(suffix))
|
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 {
|
return func(p string, f os.FileInfo, err error) error {
|
||||||
filename := f.Name()
|
filename := f.Name()
|
||||||
if filename == "." || filename == ".." {
|
if skip, err := skipDir(filename); skip {
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
element := newMediaElement(p)
|
element := newMediaElement(p)
|
||||||
fmt.Println(element.String())
|
fmt.Println(element.Path())
|
||||||
|
|
||||||
dir := filepath.Dir(p)
|
dir := filepath.Dir(p)
|
||||||
ext := filepath.Ext(filename)
|
ext := filepath.Ext(filename)
|
||||||
@ -58,3 +89,64 @@ func removeSuffixFunc(suffix string) filepath.WalkFunc {
|
|||||||
return nil
|
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)
|
ext := filepath.Ext(name)
|
||||||
name = trimSuffix(name, ext)
|
name = trimSuffix(name, ext)
|
||||||
|
|
||||||
dash := strings.Index(name, " - ")
|
dash := strings.Index(name, "-")
|
||||||
episode := ""
|
episode := ""
|
||||||
if dash != -1 {
|
if dash != -1 {
|
||||||
episode = name[dash+3:]
|
episode = strings.TrimSpace(name[dash+1:])
|
||||||
name = name[:dash]
|
name = strings.TrimSpace(name[:dash])
|
||||||
}
|
}
|
||||||
|
|
||||||
dash = strings.Index(episode, " - ")
|
dash = strings.Index(episode, "-")
|
||||||
title := ""
|
title := ""
|
||||||
if dash != -1 {
|
if dash != -1 {
|
||||||
title = episode[dash+3:]
|
title = strings.TrimSpace(episode[dash+1:])
|
||||||
episode = episode[:dash]
|
episode = strings.TrimSpace(episode[:dash])
|
||||||
}
|
}
|
||||||
|
|
||||||
element := &mediaElement{
|
element := &mediaElement{
|
||||||
@ -40,7 +40,6 @@ func newMediaElement(p string) *mediaElement {
|
|||||||
Extension: ext,
|
Extension: ext,
|
||||||
Name: name,
|
Name: name,
|
||||||
Episode: episode,
|
Episode: episode,
|
||||||
Title: title,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
words := strings.Split(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
|
return element
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,3 +88,17 @@ func (element *mediaElement) String() string {
|
|||||||
}
|
}
|
||||||
return result
|
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