args are OUTD IND {} patterns..
This commit is contained in:
52
main.go
52
main.go
@@ -2,8 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"maps"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
@@ -12,26 +12,38 @@ import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type Fields struct {
|
||||
Title string
|
||||
Season string
|
||||
Episode string
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT)
|
||||
defer can()
|
||||
|
||||
var overrides Fields
|
||||
json.Unmarshal([]byte(os.Args[3]), &overrides)
|
||||
|
||||
if err := Run(ctx,
|
||||
os.Args[1],
|
||||
os.Args[2],
|
||||
os.Args[3:],
|
||||
nil,
|
||||
os.Args[4:],
|
||||
overrides,
|
||||
); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Run(ctx context.Context, outd, ind string, patterns []string, overrides map[string]string) error {
|
||||
func Run(ctx context.Context, outd, ind string, patterns []string, overrides Fields) error {
|
||||
entries, err := os.ReadDir(ind)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if !entry.Type().IsRegular() {
|
||||
continue
|
||||
}
|
||||
if err := one(ctx, outd, path.Join(ind, entry.Name()), patterns, overrides); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -39,7 +51,7 @@ func Run(ctx context.Context, outd, ind string, patterns []string, overrides map
|
||||
return nil
|
||||
}
|
||||
|
||||
func one(ctx context.Context, outd, inf string, patterns []string, overrides map[string]string) error {
|
||||
func one(ctx context.Context, outd, inf string, patterns []string, overrides Fields) error {
|
||||
f := path.Base(inf)
|
||||
for _, pattern := range patterns {
|
||||
re := regexp.MustCompile(pattern)
|
||||
@@ -47,30 +59,38 @@ func one(ctx context.Context, outd, inf string, patterns []string, overrides map
|
||||
continue
|
||||
}
|
||||
|
||||
found := maps.Clone(overrides)
|
||||
found := overrides
|
||||
groupNames := re.SubexpNames()
|
||||
groups := re.FindStringSubmatch(f)
|
||||
for i := 1; i < len(groupNames); i++ {
|
||||
k := groupNames[i]
|
||||
v := groups[i]
|
||||
found[k] = v
|
||||
switch groupNames[i] {
|
||||
case "title":
|
||||
found.Title = v
|
||||
case "season":
|
||||
found.Season = v
|
||||
case "episode":
|
||||
found.Episode = v
|
||||
default:
|
||||
return fmt.Errorf("unexpected capture group %q", groupNames[i])
|
||||
}
|
||||
}
|
||||
|
||||
if found["title"] == "" || found["season"] == "" || found["episode"] == "" {
|
||||
if found.Title == "" || found.Season == "" || found.Episode == "" {
|
||||
continue
|
||||
}
|
||||
found["title"] = strings.Join(strings.Fields(found["title"]), "_")
|
||||
found.Title = strings.Join(strings.Fields(found.Title), "_")
|
||||
|
||||
if err := mvNLn(ctx, outd, inf, found["title"], found["season"], found["episode"]); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return mvNLn(ctx, outd, inf, found)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func mvNLn(ctx context.Context, outd, inf string, title, season, episode string) error {
|
||||
outf := path.Join(outd, fmt.Sprintf("%s_S%sE%s%s", title, season, episode, path.Ext(inf)))
|
||||
func mvNLn(ctx context.Context, outd, inf string, fields Fields) error {
|
||||
outf := path.Join(outd, fmt.Sprintf("%s_S%sE%s%s", fields.Title, fields.Season, fields.Episode, path.Ext(inf)))
|
||||
if _, err := os.Stat(outf); err == nil {
|
||||
return nil // fmt.Errorf("conflict: %s already exists", path.Base(outf))
|
||||
}
|
||||
if err := os.Rename(inf, outf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user