Skip to content

Commit

Permalink
added series year and scene flags, moved to text/template
Browse files Browse the repository at this point in the history
Signed-off-by: gryffyn <[email protected]>
  • Loading branch information
gryffyn committed Jun 4, 2022
1 parent 255db5a commit ec2991b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea/
test/
osiris
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<title>\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
```
78 changes: 46 additions & 32 deletions osiris.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
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 {
Dryrun bool `short:"d" long:"dryrun" description:"don't modify files"`
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 {
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit ec2991b

Please sign in to comment.