diff --git a/.gitignore b/.gitignore index 9f11b75..a82d751 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .idea/ +test/ +osiris diff --git a/CHANGELOG.md b/CHANGELOG.md index b166537..52d6942 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [0.2.0] - 2022-06-04 +### Added +- flags for enabling series year and scene info + +### Changed +- moved to text/template + ## [0.1.0] - 2022-01-21 ### Added - Intitial release. diff --git a/README.md b/README.md index cceea24..ad9c50a 100644 --- a/README.md +++ b/README.md @@ -21,15 +21,17 @@ Usage: osiris [OPTIONS] [regex] [filename...] Application Options: - -d, --dryrun don't modify files - -s, --silent don't print file names - --no-color disables colored output - -f, --film uses film output format - -y, --year= release year override - -t, --title= release title override + -d, --dryrun don't modify files + -s, --silent don't print file names + --no-color disables colored output + -f, --film uses film output format + -Y, --seriesyear whether series year is output + -S, --scene whether scene info is output + -y, --year= release year override + -t, --title= release title override Help Options: - -h, --help Show this help message + -h, --help Show this help message ``` ## regex parameters @@ -48,15 +50,15 @@ the regex argument takes in a regex with specifically-named named capture groups odin follows a standard-ish format, namely -`Series Title - S01E01 - Episode Title (SCENE INFO).ext` +`Series Title - S01E01 - Episode Title.ext` or in the case of a film (`-f, --film`) -`Film Title (YEAR) (SCENE INFO).ext` +`Film Title (YEAR).ext` ## example ```shell $ osiris -d '(?P\w+)\.(?P<ep>S\d{2}E\d{2})\.(?P<eptitle>[\w\.]+)\.(?P<scene>WEB-.+)' Title.S01E01.Episode.Title.WEB-DL.H264.SCENE.mkv -Title.S01E01.Episode.Title.WEB-DL.H264.SCENE.mkv -> ./Title - S01E01 - Episode Title (WEB-DL H264 SCENE).mkv +Title.S01E01.Episode.Title.WEB-DL.H264.SCENE.mkv -> Title - S01E01 - Episode Title.mkv ``` diff --git a/osiris.go b/osiris.go index d86f4a2..86041e5 100644 --- a/osiris.go +++ b/osiris.go @@ -1,29 +1,31 @@ package main import ( + "bytes" "fmt" "log" "os" "path" "regexp" "strings" + "text/template" "github.com/fatih/color" "github.com/jessevdk/go-flags" ) +type releaseOptions struct { + Scene bool + SeriesYear bool +} + type release struct { Title string Year string Episode string EpisodeTitle string Scene string -} - -type inputMetadata struct { - Film *bool - Year *string - Title *string + Options releaseOptions } type args struct { @@ -31,6 +33,8 @@ type args struct { Silent bool `short:"s" long:"silent" description:"don't print file names"` NoColor bool `long:"no-color" description:"disables colored output"` Film bool `short:"f" long:"film" description:"uses film output format"` + SeriesYear bool `short:"Y" long:"seriesyear" description:"whether series year is output"` + Scene bool `short:"S" long:"scene" description:"whether scene info is output"` Year string `short:"y" long:"year" description:"release year override"` Title string `short:"t" long:"title" description:"release title override"` Positional struct { @@ -39,6 +43,12 @@ type args struct { } `positional-args:"true"` } +var ( + seriesTemplate = "{{ .Title }}{{if .Options.SeriesYear}} ({{ .Year }}){{end}} - {{ .Episode}}{{if ." + + "EpisodeTitle}} - {{ .EpisodeTitle}}{{end}}{{if .Options.Scene }} ({{ .Scene }}){{end}}" + filmTemplate = "{{ .Title }} ({{ .Year }}){{if .Options.Scene }} ({{ .Scene }}){{end}}" +) + func main() { var args args _, err := flags.Parse(&args) @@ -51,19 +61,13 @@ func main() { } } - inmet := inputMetadata{ - Film: &args.Film, - Year: &args.Year, - Title: &args.Title, - } - re, err := regexp.Compile(args.Positional.Regex) if err != nil { log.Fatalf("Failed to parse regex: %v\n", err) } for _, filename := range args.Positional.Filename { - newfilename := getFilename(filename, re, &inmet) + newfilename := getFilename(filename, re, &args) if !args.Silent { printRename(&filename, &newfilename) } @@ -73,7 +77,7 @@ func main() { } } -func getFilename(filepath string, re *regexp.Regexp, inputmetadata *inputMetadata) string { +func getFilename(filepath string, re *regexp.Regexp, args *args) string { fpath := path.Dir(filepath) fext := path.Ext(filepath) fnext := strings.TrimSuffix(filepath, fext) @@ -86,34 +90,44 @@ func getFilename(filepath string, re *regexp.Regexp, inputmetadata *inputMetadat } } + scene := regexp.MustCompile(`\D(\.)\D`).ReplaceAllStringFunc(metadata["scene"], func(s string) string { + return strings.ReplaceAll(s, ".", " ") + }) + r := release{ - Title: strings.ReplaceAll(metadata["title"], ".", " "), - Year: metadata["year"], - Episode: metadata["ep"], + Title: strings.TrimSpace(strings.ReplaceAll(metadata["title"], ".", " ")), + Year: strings.TrimSpace(metadata["year"]), + Episode: strings.TrimSpace(metadata["ep"]), EpisodeTitle: strings.TrimSpace(strings.ReplaceAll(metadata["eptitle"], ".", " ")), - Scene: strings.TrimSpace(strings.ReplaceAll(metadata["scene"], ".", " ")), + Scene: strings.TrimSpace(scene), + Options: releaseOptions{ + Scene: args.Scene, + SeriesYear: args.SeriesYear, + }, } - if r.Year == "" && *inputmetadata.Year != "" { - r.Year = *inputmetadata.Year + if r.Year == "" && args.Year != "" { + r.Year = args.Year } - if r.Title == "" && *inputmetadata.Title != "" { - r.Title = *inputmetadata.Title + if r.Title == "" && args.Title != "" { + r.Title = args.Title } - var newname string - if !*inputmetadata.Film { - newname = fmt.Sprintf("%s - %s", r.Title, r.Episode) - if r.EpisodeTitle != "" { - newname = fmt.Sprintf("%s - %s", newname, r.EpisodeTitle) - } + var tmpl *string + + if args.Film { + tmpl = &filmTemplate } else { - newname = fmt.Sprintf("%s (%s)", r.Title, r.Year) + tmpl = &seriesTemplate } - if r.Scene != "" { - newname = fmt.Sprintf("%s (%s)", newname, r.Scene) + + releaseTemplate, _ := template.New("release").Parse(*tmpl) + var newname bytes.Buffer + err := releaseTemplate.Execute(&newname, r) + if err != nil { + log.Fatalln(err) } - return fmt.Sprintf("%s/%s%s", fpath, newname, fext) + return fmt.Sprintf("%s/%s%s", fpath, newname.String(), fext) } func printRename(filepath, newfilepath *string) {