Skip to content

Commit

Permalink
fixed cfg overrides, fixed yml parsing, readme updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gryffyn committed Jun 11, 2022
1 parent f36c9da commit 9d28c77
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 31 deletions.
50 changes: 32 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# osiris
# Osiris

[![Build Status](https://ci.gryffyn.io/api/badges/gryffyn/osiris/status.svg?ref=refs/heads/main)](https://ci.gryffyn.io/gryffyn/osiris)
A tool for renaming films / tv series based on named regex capture groups.
A tool for renaming films / tv series based on named regex capture groups and Go templates.

## installation
## Installation

`go install git.gryffyn.io/gryffyn/osiris@latest`

Expand All @@ -15,7 +15,7 @@ cd osiris
go build
```

## usage
## Usage
```
Usage:
osiris [OPTIONS] [regex] [filename...]
Expand All @@ -29,34 +29,48 @@ Application Options:
-S, --scene whether scene info is output
-y, --year= release year override
-t, --title= release title override
-c, --config= config file (default ~/.config/osiris/osiris.yml)
Help Options:
-h, --help Show this help message
```

## regex parameters
## Input Regex

the regex argument takes in a regex with specifically-named named capture groups.

| name | example | required | series | film | description |
|---------|-----------------------|:--------:|--------|------|-----------------------------|
| title | `(?P<title>\w+)` || | | title of the series/film |
| year | `(?P<year>\d{4})` || | | release year |
| ep | `(?P<ep>S\d{2}E\d{2})` || | | episode number (ex. S01E01) |
| eptitle | `(?P<eptitle>\w+)` || | | episode title |
| scene | `(?P<scene>[\w-\.]+)` || | | scene / release info |
| name | Output Template Name | example | required | series | film | description |
|---------|----------------------|------------------------|:--------:|--------|------|-----------------------------|
| title | Title | `(?P<title>\w+)` | | | | title of the series/film |
| year | Year | `(?P<year>\d{4})` | | | | release year |
| ep | Episode | `(?P<ep>S\d{2}E\d{2})` | | | | episode number (ex. S01E01) |
| eptitle | EpisodeTitle | `(?P<eptitle>\w+)` | | | | episode title |
| scene | Scene | `(?P<scene>[\w-\.]+)` | | | | scene / release info |

## output naming format
## Output Templates

odin follows a standard-ish format, namely
### Default

`Series Title - S01E01 - Episode Title.ext`
| Type | Template | Example |
|--------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------|
| Series | {{ .Title }}{{if .Options.SeriesYear}} ({{ .Year }}){{end}} - {{ .Episode}}{{if EpisodeTitle}} - {{ .EpisodeTitle}}{{end}}{{if .Options.Scene }} ({{ .Scene }}){{end}} | Series Title - S01E01 - Episode Title.ext |
| Film | {{ .Title }} ({{ .Year }}){{if .Options.Scene }} ({{ .Scene }}){{end}} | Film Title (YEAR).ext |

or in the case of a film (`-f, --film`)

`Film Title (YEAR).ext`
## Config File Example

## example
The configuration file is by default loaded from a file named `osiris.yml` the current user's configuration directory (`~/.config/osiris` on Linux). The config file location can be overridden with the `-c` flag.

```yaml
---
seriesYear: false
scene: false
templates:
series: "{{ .Title }}{{if .Options.SeriesYear}} ({{ .Year }}){{end}} - {{ .Episode}}{{if EpisodeTitle}} - {{ .EpisodeTitle}}{{end}}{{if .Options.Scene }} ({{ .Scene }}){{end}}"
film: "{{ .Title }} ({{ .Year }}){{if .Options.Scene }} ({{ .Scene }}){{end}}"
```
## Usage 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
Expand Down
25 changes: 25 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import "gopkg.in/yaml.v2"

type config struct {
SeriesYear *bool `yaml:"seriesYear,omitempty"`
Scene *bool `yaml:"scene,omitempty"`
Templates struct {
Series *string `yaml:"series,omitempty"`
Film *string `yaml:"film,omitempty"`
} `yaml:"templates"`
}

func (c *config) Parse(data []byte) error {
return yaml.Unmarshal(data, c)
}

func (c *config) Argparse(args *args) {
if args.Scene {
c.Scene = &args.Scene
}
if args.SeriesYear {
c.SeriesYear = &args.SeriesYear
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ go 1.14
require (
github.com/fatih/color v1.13.0
github.com/jessevdk/go-flags v1.5.0
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
58 changes: 45 additions & 13 deletions osiris.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path"
Expand Down Expand Up @@ -37,6 +38,7 @@ type args struct {
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"`
ConfigFile string `short:"c" long:"config" description:"config file (default ~/.config/osiris/osiris.yml"`
Positional struct {
Regex string `positional-arg-name:"regex" required:"true"`
Filename []string `positional-arg-name:"filename" required:"true"`
Expand All @@ -61,13 +63,37 @@ func main() {
}
}

var data []byte
var cfgFile string
if args.ConfigFile != "" {
cfgFile = args.ConfigFile
} else {
cfgdir, err := os.UserConfigDir()
if err != nil {
log.Fatalln(err)
}
cfgFile = path.Join(cfgdir, "osiris", "osiris.yml")
}

data, err = ioutil.ReadFile(cfgFile)
if err != nil {
log.Fatalln(err)
}

cfg := config{}
err = cfg.Parse(data)
if err != nil {
log.Fatalf("Error reading config file (%s): %v", cfgFile, err)
}
cfg.Argparse(&args)

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, &args)
newfilename := getFilename(filename, re, &cfg, args.Year, args.Title, args.Film)
if !args.Silent {
printRename(&filename, &newfilename)
}
Expand All @@ -77,7 +103,7 @@ func main() {
}
}

func getFilename(filepath string, re *regexp.Regexp, args *args) string {
func getFilename(filepath string, re *regexp.Regexp, cfg *config, year, title string, film bool) string {
fpath := path.Dir(filepath)
fext := path.Ext(filepath)
fnext := strings.TrimSuffix(filepath, fext)
Expand All @@ -101,26 +127,32 @@ func getFilename(filepath string, re *regexp.Regexp, args *args) string {
EpisodeTitle: strings.TrimSpace(strings.ReplaceAll(metadata["eptitle"], ".", " ")),
Scene: strings.TrimSpace(scene),
Options: releaseOptions{
Scene: args.Scene,
SeriesYear: args.SeriesYear,
Scene: *cfg.Scene,
SeriesYear: *cfg.SeriesYear,
},
}
if r.Year == "" && args.Year != "" {
r.Year = args.Year
if r.Year == "" && year != "" {
r.Year = year
}
if r.Title == "" && args.Title != "" {
r.Title = args.Title
if r.Title == "" && title != "" {
r.Title = title
}

var tmpl *string
var tmpl string

if args.Film {
tmpl = &filmTemplate
if film {
if *cfg.Templates.Film != "" {
tmpl = *cfg.Templates.Film
}
tmpl = filmTemplate
} else {
tmpl = &seriesTemplate
if *cfg.Templates.Series != "" {
tmpl = *cfg.Templates.Series
}
tmpl = seriesTemplate
}

releaseTemplate, _ := template.New("release").Parse(*tmpl)
releaseTemplate, _ := template.New("release").Parse(tmpl)
var newname bytes.Buffer
err := releaseTemplate.Execute(&newname, r)
if err != nil {
Expand Down

0 comments on commit 9d28c77

Please sign in to comment.