Compare commits

...

17 Commits

Author SHA1 Message Date
Bel LaPointe
dad5803297 fix err msg 2026-01-27 15:12:26 -07:00
bel
2e4e4b9b06 i said PLS no multi handle file 2025-05-09 07:52:36 -06:00
bel
61f9b9c724 hrm 2025-05-09 07:39:22 -06:00
Bel LaPointe
5b9bead96f ntfy webhook format 2025-04-22 20:40:19 -06:00
Bel LaPointe
54bbca8fea accept $RECURSIVE_MISSING_WEBHOOK $RECURSIVE_MISSING_WEBHOOK_CACHE_D 2025-04-22 20:28:53 -06:00
bel
3e8e33816e dogs 2025-04-05 11:47:51 -06:00
bel
00fdd4f976 test more 2025-04-05 11:44:40 -06:00
bel
1e3d6ee0a2 se 2025-04-05 11:26:48 -06:00
Bel LaPointe
d3c9b1a564 dual first 2025-04-05 11:20:10 -06:00
bel
e653c35275 debug, dry, more env 2025-04-05 11:16:36 -06:00
Bel LaPointe
596865ede3 noisy log may as well be useful 2025-04-05 11:06:00 -06:00
bel
62ea963807 typo 2025-04-05 11:04:10 -06:00
Bel LaPointe
82a13aea65 yaml files overriden via env 2025-04-05 11:03:27 -06:00
Bel LaPointe
d40a1f8fd4 test one smart default parser 2025-04-05 10:59:33 -06:00
Bel LaPointe
e85fec9bbf for each pattern { for each entry { try } } so patterns serve as tier list 2025-04-05 10:44:22 -06:00
bel
5f5015e152 default for me 2025-04-05 01:05:10 -06:00
bel
12bb9c808b go run ./ i to init conf file 2025-04-05 01:03:19 -06:00
2 changed files with 355 additions and 41 deletions

228
main.go
View File

@@ -6,8 +6,12 @@ import (
"encoding/json"
"flag"
"fmt"
"io"
"io/fs"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"path"
@@ -19,6 +23,27 @@ import (
yaml "gopkg.in/yaml.v3"
)
var (
Debug = os.Getenv("DEBUG") == "true"
ConstTitle = os.Getenv("YAML_C_TITLE")
ConstSeason = os.Getenv("YAML_C_SEASON")
ConstEpisode = os.Getenv("YAML_C_EPISODE")
ConstOutd = os.Getenv("YAML_O")
Dry = os.Getenv("YAML_D") == "true" || os.Getenv("DRY") == "true"
ConstPatterns = os.Getenv("YAML_P")
WebhookOnRecursiveMiss = os.Getenv("RECURSIVE_MISSING_WEBHOOK")
WebhookOnRecursiveMissCacheD = os.Getenv("RECURSIVE_MISSING_WEBHOOK_CACHE_D")
)
type Yaml struct {
C Fields
O string
D bool
P []string
}
const YamlFile = ".show-ingestion.yaml"
type Fields struct {
Title string
Season string
@@ -34,30 +59,74 @@ func main() {
foo := Main
if len(os.Args) == 2 && os.Args[1] == "r" {
foo = Recursive
} else if len(os.Args) == 2 && os.Args[1] == "i" {
foo = Stage
}
if err := foo(ctx); err != nil {
panic(err)
}
}
func Stage(ctx context.Context) error {
if _, err := os.Stat(YamlFile); err == nil {
return nil
}
b, _ := yaml.Marshal(Yaml{
D: true,
O: "/volume1/video/Bel/Anime/{{.Title}}/Season_{{.Season}}",
})
return ioutil.WriteFile(YamlFile, b, os.ModePerm)
}
func Recursive(ctx context.Context) error {
q := []string{"./"}
for len(q) > 0 {
d := q[0]
q = q[1:]
p := path.Join(d, ".show-ingestion.yaml")
p := path.Join(d, YamlFile)
if _, err := os.Stat(p); err != nil {
} else if err := func() error {
var y struct {
C Fields
O string
D bool
P []string
log.Printf("%s has no %s", d, YamlFile)
if WebhookOnRecursiveMiss != "" && WebhookOnRecursiveMissCacheD != "" {
cacheP := regexp.MustCompile(`[^a-zA-Z0-9]`).ReplaceAllString(p, `_`)
cacheP = path.Join(WebhookOnRecursiveMissCacheD, cacheP)
if _, err := os.Stat(cacheP); err != nil {
req, err := http.NewRequest(http.MethodPut, WebhookOnRecursiveMiss, strings.NewReader(p))
if err != nil {
panic(err)
}
u, err := url.Parse(WebhookOnRecursiveMiss)
if err != nil {
return fmt.Errorf("WebhookOnRecursiveMiss (%s) invalid: %w", WebhookOnRecursiveMiss, err)
}
user := u.User
u.User = nil
if username := user.Username(); username != "" {
password, _ := user.Password()
req.SetBasicAuth(username, password)
}
req.URL = u
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to call %s for missing %s: %w", WebhookOnRecursiveMiss, p, err)
}
defer resp.Body.Close()
defer io.Copy(io.Discard, resp.Body)
if resp.StatusCode > 250 {
b, _ := io.ReadAll(resp.Body)
return fmt.Errorf("unexpected status code from %s for %s: (%d) %s", WebhookOnRecursiveMiss, p, resp.StatusCode, b)
}
os.MkdirAll(path.Dir(cacheP), os.ModePerm)
ioutil.WriteFile(cacheP, []byte{}, os.ModePerm)
}
}
b, _ := os.ReadFile(path.Join(d, ".show-ingestion.yaml"))
if err := yaml.Unmarshal(b, &y); err != nil {
return fmt.Errorf("%s: %w", p, err)
} else if err := func() error {
y, err := NewYaml(path.Join(d, YamlFile))
if err != nil {
return err
}
was, err := os.Getwd()
@@ -70,7 +139,7 @@ func Recursive(ctx context.Context) error {
}
defer os.Chdir(was)
log.Printf("Run(%s, %s, %+v, %+v, %v)", y.O, d, y.P, y.C, y.D)
log.Printf("Run(outd=%s, ind=%s, patterns=%+v, const=%+v, dry=%v)", y.O, d, y.P, y.C, y.D)
if err := Run(ctx, y.O, "./", y.P, y.C, y.D); err != nil {
return err
}
@@ -115,6 +184,15 @@ func Main(ctx context.Context) error {
)
}
const (
PatternGroupTitleHyphenSEDual = `^(\[[^\]]*\] )?(?P<title>.*?)( -)?[ \.](S(?P<season>[0-9]{2})E)?(?P<episode>[0-9]{2})[^0-9].*[dD][uU][aA][lL].*`
PatternGroupTitleHyphenSE = `^(\[[^\]]*\] )?(?P<title>.*?)( -)?[ \.](S(?P<season>[0-9]{2})E)?(?P<episode>[0-9]{2})[^0-9].*`
PatternTitleSEDual = `^(?P<title>.*) S(?P<season>[0-9]+)E(?P<episode>[0-9]+).*[dD][uU][aA][lL].*`
PatternTitleSE = `^(?P<title>.*) S(?P<season>[0-9]+)E(?P<episode>[0-9]+).*`
SEDual = `^S(?P<season>[0-9]+)E(?P<episode>[0-9]+).*[dD][uU][aA][lL].*`
SE = `^S(?P<season>[0-9]+)E(?P<episode>[0-9]+).*`
)
func Run(ctx context.Context, outd, ind string, patterns []string, overrides Fields, dry bool) error {
mvNLn := RealMvNLn
if dry {
@@ -124,8 +202,12 @@ func Run(ctx context.Context, outd, ind string, patterns []string, overrides Fie
outd,
ind,
append(patterns,
`^\[[^\]]*\] (?P<title>.*) - (?P<episode>[0-9]+).*`,
`^(?P<title>.*) S(?P<season>[0-9]+)E(?P<episode>[0-9]+).*`,
PatternGroupTitleHyphenSEDual,
PatternGroupTitleHyphenSE,
PatternTitleSEDual,
PatternTitleSE,
SEDual,
SE,
),
overrides,
mvNLn,
@@ -137,40 +219,34 @@ func RunWith(ctx context.Context, outd, ind string, patterns []string, overrides
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, mvNLn); err != nil {
return err
done := map[int]bool{}
for _, pattern := range patterns {
for i, entry := range entries {
if done[i] {
continue
}
if !entry.Type().IsRegular() && !(Debug && Dry) {
continue
}
if match, err := one(ctx, outd, path.Join(ind, entry.Name()), []string{pattern}, overrides, mvNLn); err != nil {
return err
} else if match {
done[i] = true
}
}
}
return nil
}
func one(ctx context.Context, outd, inf string, patterns []string, overrides Fields, mvNLn MvNLn) error {
func one(ctx context.Context, outd, inf string, patterns []string, overrides Fields, mvNLn MvNLn) (bool, error) {
f := path.Base(inf)
for _, pattern := range patterns {
re := regexp.MustCompile(pattern)
if !re.MatchString(f) {
continue
}
var found Fields
groupNames := re.SubexpNames()
groups := re.FindStringSubmatch(f)
for i := 1; i < len(groupNames); i++ {
v := groups[i]
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])
found, match := Parse(f, pattern)
if !match {
if Debug {
log.Printf("%q does not match %q", pattern, f)
}
continue
}
for _, wr := range [][2]*string{
@@ -184,13 +260,45 @@ func one(ctx context.Context, outd, inf string, patterns []string, overrides Fie
}
if found.Title == "" || found.Season == "" || found.Episode == "" {
if Debug {
log.Printf("%q does not match all %q: %+v", pattern, f, found)
}
continue
}
found.Title = strings.ReplaceAll(found.Title, ".", " ")
found.Title = strings.Join(strings.Fields(found.Title), "_")
return foundOne(ctx, outd, inf, found, mvNLn)
if Debug {
log.Printf("%q matches %q as %+v", pattern, f, found)
}
return true, foundOne(ctx, outd, inf, found, mvNLn)
}
return nil
return false, nil
}
func Parse(f string, pattern string) (Fields, bool) {
re := regexp.MustCompile(pattern)
if !re.MatchString(f) {
return Fields{}, false
}
var found Fields
groupNames := re.SubexpNames()
groups := re.FindStringSubmatch(f)
for i := 1; i < len(groupNames); i++ {
v := groups[i]
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])
}
}
return found, true
}
func foundOne(ctx context.Context, outd, inf string, fields Fields, mvNLn MvNLn) error {
@@ -227,10 +335,16 @@ func DryMvNLn() func(string, string) error {
outd := map[string]struct{}{}
return func(outf, inf string) error {
if _, err := os.Stat(outf); err == nil {
if Debug {
fmt.Fprintf(os.Stderr, "no mv %q\n %q\n", inf, outf)
}
return nil
}
if _, ok := outd[outf]; ok {
if Debug {
fmt.Fprintf(os.Stderr, "no mv %q\n %q\n", inf, outf)
}
return nil
}
outd[outf] = struct{}{}
@@ -250,3 +364,35 @@ func readDir(d string) ([]fs.DirEntry, error) {
}
return result, err
}
func NewYaml(p string) (Yaml, error) {
var y Yaml
b, _ := os.ReadFile(p)
if err := yaml.Unmarshal(b, &y); err != nil {
return y, fmt.Errorf("%s: %w", p, err)
}
if ConstTitle != "" {
y.C.Title = ConstTitle
}
if ConstSeason != "" {
y.C.Season = ConstSeason
}
if ConstEpisode != "" {
y.C.Episode = ConstEpisode
}
if ConstOutd != "" {
y.O = ConstOutd
}
if Dry {
y.D = true
}
if ConstPatterns != "" {
y.P = strings.Split(ConstPatterns, ",")
}
return y, nil
}

View File

@@ -3,6 +3,9 @@ package main_test
import (
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path"
"slices"
@@ -79,6 +82,17 @@ func TestRunWith(t *testing.T) {
"Australian_Survivor_S12E12.mkv",
},
},
"hard w group": {
given: []string{
"[Yameii] Dr. Stone - S04E12 [English Dub] [CR WEB-DL 720p] [F6EF1948].mkv",
},
patterns: []string{
main.PatternGroupTitleHyphenSE,
},
want: []string{
"Dr_Stone_S04E12.mkv",
},
},
"easy w group": {
given: []string{
"[SubsPlease] Tokidoki Bosotto Russia-go de Dereru Tonari no Alya-san - 01 (720p) [A12844D5].mkv",
@@ -149,6 +163,44 @@ func TestRunWith(t *testing.T) {
}
func TestRecursive(t *testing.T) {
webhooks := []string{}
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut {
t.Errorf("unexpected webhook method %s", r.Method)
}
if r.URL.User.String() != "" {
t.Errorf("unexpected auth on url %s", r.URL.String())
}
if u, p, _ := r.BasicAuth(); u != "u" || p != "p" {
t.Errorf("webhook didnt translate u:p to basic auth")
}
b, _ := ioutil.ReadAll(r.Body)
t.Logf("%s { %s }", r.URL.String(), b)
webhooks = append(webhooks, string(b))
}))
t.Cleanup(s.Close)
t.Cleanup(func() {
t.Logf("webhooks: %+v", webhooks)
if len(webhooks) == 0 {
t.Errorf("expected webhook calls but got none")
}
deduped := slices.Clone(webhooks)
slices.Sort(deduped)
slices.Compact(deduped)
if len(deduped) != len(webhooks) {
t.Errorf("expected no duplicate webhooks but got %+v", webhooks)
}
})
u, _ := url.Parse(s.URL)
u.User = url.UserPassword("u", "p")
main.WebhookOnRecursiveMiss = u.String()
main.WebhookOnRecursiveMissCacheD = t.TempDir()
t.Cleanup(func() {
main.WebhookOnRecursiveMiss = ""
main.WebhookOnRecursiveMissCacheD = ""
})
was, _ := os.Getwd()
t.Cleanup(func() { os.Chdir(was) })
os.Chdir(t.TempDir())
@@ -194,8 +246,16 @@ func TestRecursive(t *testing.T) {
os.MkdirAll("./dirB/showE", os.ModePerm)
write("./dirB/showE/title S03E06.e")
// defaults
write("./dirA/showF/.show-ingestion.yaml", `{
"o": "`+outd+`/F"
}`)
write("./dirA/showF/[Yameii] Dr. Stone - S04E12 [English Dub] [CR WEB-DL 720p] [F6EF1948].mkv")
if err := main.Recursive(context.Background()); err != nil {
t.Fatal(err)
} else if err := main.Recursive(context.Background()); err != nil {
t.Fatalf("failed second run: %v", err)
}
exists(t, path.Join(outd, "A", "A_SAEA.a"))
@@ -203,6 +263,8 @@ func TestRecursive(t *testing.T) {
exists(t, path.Join(outd, "C", "t_SsEe.c"))
notExists(t, path.Join(outd, "D", "title_S02E04.d"))
notExists(t, path.Join(outd, "title_S03E06.e"))
exists(t, path.Join(outd, "F", "Dr_Stone_S04E12.mkv"))
notExists(t, path.Join(outd, "F", "[Yameii]_Dr_Stone_-_S04E12.mkv"))
}
func write(f string, b ...string) {
@@ -242,3 +304,109 @@ func ls(d string) []string {
slices.Sort(result)
return result
}
func TestParse(t *testing.T) {
cases := map[string]struct {
pattern string
want main.Fields
}{
"[SubsPlease] Tokidoki Bosotto Russia-go de Dereru Tonari no Alya-san - 01 (720p) [A12844D5].mkv": {
pattern: main.PatternGroupTitleHyphenSE,
want: main.Fields{
Title: "Tokidoki Bosotto Russia-go de Dereru Tonari no Alya-san",
Season: "",
Episode: "01",
},
},
"Survivor.AU.S12E11.1080p.HEVC.x265-MeGusta[EZTVx.to].mkv": {
pattern: main.PatternGroupTitleHyphenSE,
want: main.Fields{
Title: "Survivor.AU",
Season: "12",
Episode: "11",
},
},
"DAN DA DAN (2024) S01E01v2 (1080p WEB-DL H264 AAC DDP 2.0 Dual-Audio) [MALD].mkv": {
pattern: main.PatternGroupTitleHyphenSE,
want: main.Fields{
Title: "DAN DA DAN (2024)",
Season: "01",
Episode: "01",
},
},
"ZENSHU.S01E01.1080p.AMZN.WEB-DL.MULTi.DDP2.0.H.264.MSubs-ToonsHub.mkv": {
pattern: main.PatternGroupTitleHyphenSE,
want: main.Fields{
Title: "ZENSHU",
Season: "01",
Episode: "01",
},
},
"[Yameii] My Hero Academia - S07E08 [English Dub] [CR WEB-DL 720p] [DE5FFC3E].mkv": {
pattern: main.PatternGroupTitleHyphenSE,
want: main.Fields{
Title: "My Hero Academia",
Season: "07",
Episode: "08",
},
},
"Ranma1-2.2024.S01E03.Because.Theres.Someone.He.Likes.1080p.NF.WEB-DL.AAC2.0.H.264-VARYG.mkv": {
pattern: main.PatternGroupTitleHyphenSE,
want: main.Fields{
Title: "Ranma1-2.2024",
Season: "01",
Episode: "03",
},
},
"[Yameii] The Apothecary Diaries - S02E03 [English Dub] [CR WEB-DL 720p] [FD3E7434].mkv": {
pattern: main.PatternGroupTitleHyphenSE,
want: main.Fields{
Title: "The Apothecary Diaries",
Season: "02",
Episode: "03",
},
},
"The.Dinner.Table.Detective.S01E01.Welcome.to.the.murderous.party.File.1.1080p.AMZN.WEB-DL.DDP2.0.H.264-VARYG.mkv": {
pattern: main.PatternGroupTitleHyphenSE,
want: main.Fields{
Title: "The.Dinner.Table.Detective",
Season: "01",
Episode: "01",
},
},
"[Reza] Wistoria Wand and Sword - S01E01.mkv": {
pattern: main.PatternGroupTitleHyphenSE,
want: main.Fields{
Title: "Wistoria Wand and Sword",
Season: "01",
Episode: "01",
},
},
"[EMBER] Ao no Hako - 01.mkv": {
pattern: main.PatternGroupTitleHyphenSE,
want: main.Fields{
Title: "Ao no Hako",
Season: "",
Episode: "01",
},
},
"Niehime to Kemono no Ou - 12 [darkflux].mkv": {
pattern: main.PatternGroupTitleHyphenSE,
want: main.Fields{
Title: "Niehime to Kemono no Ou",
Season: "",
Episode: "12",
},
},
}
for f, d := range cases {
c := d
t.Run(f, func(t *testing.T) {
got, _ := main.Parse(f, c.pattern)
if got != c.want {
t.Errorf("expected \n\t%+v but got \n\t%+v", c.want, got)
}
})
}
}