Compare commits
15 Commits
5f5015e152
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dad5803297 | ||
|
|
2e4e4b9b06 | ||
|
|
61f9b9c724 | ||
|
|
5b9bead96f | ||
|
|
54bbca8fea | ||
|
|
3e8e33816e | ||
|
|
00fdd4f976 | ||
|
|
1e3d6ee0a2 | ||
|
|
d3c9b1a564 | ||
|
|
e653c35275 | ||
|
|
596865ede3 | ||
|
|
62ea963807 | ||
|
|
82a13aea65 | ||
|
|
d40a1f8fd4 | ||
|
|
e85fec9bbf |
192
main.go
192
main.go
@@ -6,9 +6,12 @@ import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
@@ -20,6 +23,18 @@ 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
|
||||
@@ -71,11 +86,47 @@ func Recursive(ctx context.Context) error {
|
||||
|
||||
p := path.Join(d, YamlFile)
|
||||
if _, err := os.Stat(p); err != nil {
|
||||
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)
|
||||
}
|
||||
}
|
||||
} else if err := func() error {
|
||||
var y Yaml
|
||||
b, _ := os.ReadFile(path.Join(d, YamlFile))
|
||||
if err := yaml.Unmarshal(b, &y); err != nil {
|
||||
return fmt.Errorf("%s: %w", p, err)
|
||||
y, err := NewYaml(path.Join(d, YamlFile))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
was, err := os.Getwd()
|
||||
@@ -88,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
|
||||
}
|
||||
@@ -133,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 {
|
||||
@@ -142,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,
|
||||
@@ -155,23 +219,67 @@ func RunWith(ctx context.Context, outd, ind string, patterns []string, overrides
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if !entry.Type().IsRegular() {
|
||||
done := map[int]bool{}
|
||||
for _, pattern := range patterns {
|
||||
for i, entry := range entries {
|
||||
if done[i] {
|
||||
continue
|
||||
}
|
||||
if err := one(ctx, outd, path.Join(ind, entry.Name()), patterns, overrides, mvNLn); err != nil {
|
||||
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 {
|
||||
found, match := Parse(f, pattern)
|
||||
if !match {
|
||||
if Debug {
|
||||
log.Printf("%q does not match %q", pattern, f)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
for _, wr := range [][2]*string{
|
||||
[2]*string{&found.Title, &overrides.Title},
|
||||
[2]*string{&found.Season, &overrides.Season},
|
||||
[2]*string{&found.Episode, &overrides.Episode},
|
||||
} {
|
||||
if *wr[1] != "" {
|
||||
*wr[0] = *wr[1]
|
||||
}
|
||||
}
|
||||
|
||||
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), "_")
|
||||
|
||||
if Debug {
|
||||
log.Printf("%q matches %q as %+v", pattern, f, found)
|
||||
}
|
||||
return true, foundOne(ctx, outd, inf, found, mvNLn)
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func Parse(f string, pattern string) (Fields, bool) {
|
||||
re := regexp.MustCompile(pattern)
|
||||
if !re.MatchString(f) {
|
||||
continue
|
||||
return Fields{}, false
|
||||
}
|
||||
|
||||
var found Fields
|
||||
@@ -187,28 +295,10 @@ func one(ctx context.Context, outd, inf string, patterns []string, overrides Fie
|
||||
case "episode":
|
||||
found.Episode = v
|
||||
default:
|
||||
return fmt.Errorf("unexpected capture group %q", groupNames[i])
|
||||
//return fmt.Errorf("unexpected capture group %q", groupNames[i])
|
||||
}
|
||||
}
|
||||
|
||||
for _, wr := range [][2]*string{
|
||||
[2]*string{&found.Title, &overrides.Title},
|
||||
[2]*string{&found.Season, &overrides.Season},
|
||||
[2]*string{&found.Episode, &overrides.Episode},
|
||||
} {
|
||||
if *wr[1] != "" {
|
||||
*wr[0] = *wr[1]
|
||||
}
|
||||
}
|
||||
|
||||
if found.Title == "" || found.Season == "" || found.Episode == "" {
|
||||
continue
|
||||
}
|
||||
found.Title = strings.Join(strings.Fields(found.Title), "_")
|
||||
|
||||
return foundOne(ctx, outd, inf, found, mvNLn)
|
||||
}
|
||||
return nil
|
||||
return found, true
|
||||
}
|
||||
|
||||
func foundOne(ctx context.Context, outd, inf string, fields Fields, mvNLn MvNLn) error {
|
||||
@@ -245,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{}{}
|
||||
@@ -268,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
|
||||
}
|
||||
|
||||
168
main_test.go
168
main_test.go
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user