Files
py-media-grader/cleaner/main.go

77 lines
1.7 KiB
Go

package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
logger "git.site.quack-lab.dev/dave/cylogger"
)
func main() {
flag.Parse()
logger.InitFlag()
if flag.NArg() == 0 {
fmt.Println("Usage: cleaner <files>")
os.Exit(1)
}
// regex to match "2025-07-08"
re := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
editedRe := regexp.MustCompile(`_edited_\d{5}`)
for _, file := range flag.Args() {
filelog := logger.Default.WithPrefix(file)
filelog.Info("Processing file")
info, err := os.Stat(file)
if err != nil {
filelog.Error("ERROR: %v\n", err)
continue
}
if info.IsDir() {
filelog.Info("SKIP (directory): %s\n", file)
continue
}
name := filepath.Base(file)
match := re.FindStringSubmatch(name)
filelog.Debug("Match: %v", match)
if match == nil {
filelog.Info("SKIP (no date pattern): %s\n", name)
continue
}
namePart := match[0]
editMatch := editedRe.FindStringSubmatch(name)
filelog.Debug("Edit match: %v", editMatch)
if editMatch != nil {
namePart = namePart + editMatch[0]
filelog.Info("Video has edited part, new name: %s", namePart)
}
newName := namePart + filepath.Ext(name)
filelog.Debug("New name: %s", newName)
if name == newName {
filelog.Info("SKIP (already named): %s\n", name)
continue
}
filelog.Debug("Checking if target exists: %s", newName)
if _, err := os.Stat(newName); err == nil {
filelog.Info("SKIP (target exists): %s -> %s\n", name, newName)
continue
}
filelog.Info("Renaming to: %s", newName)
err = os.Rename(name, newName)
if err != nil {
filelog.Error("ERROR renaming %s: %v\n", name, err)
} else {
filelog.Info("RENAMED: %s -> %s\n", name, newName)
}
filelog.Info("All done")
continue
}
}