diff --git a/.fuse_hidden000024c900000005 b/.fuse_hidden000024c900000005 deleted file mode 100755 index 163d703..0000000 Binary files a/.fuse_hidden000024c900000005 and /dev/null differ diff --git a/envvars.go b/envvars.go index ddb8e24..82efaf8 100644 --- a/envvars.go +++ b/envvars.go @@ -2,6 +2,7 @@ package main import ( "os/user" + "runtime" "strconv" "git.milar.in/milarin/envvars/v2" @@ -10,6 +11,8 @@ import ( var ( DownloadPath = envvars.String("DOWNLOAD_PATH", "") + ThreadCount = envvars.Int("THREADS", runtime.NumCPU()) + Uid = envvars.Object("UID", 1000, func(s string) (int, error) { if uid, err := strconv.Atoi(s); err == nil { return uid, nil diff --git a/file_priority_string.go b/file_priority_string.go new file mode 100644 index 0000000..d6744e2 --- /dev/null +++ b/file_priority_string.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "strings" + + "git.milar.in/nyaanime/logic" +) + +func FilePrio2Str(fp *logic.FilePriority) string { + return fmt.Sprintf("priority: %d | resolution: %s | languages: %s | subtitles: %s", + fp.Priority, + fp.Properties.GetResolution().String(), + strings.Join(fp.Properties.GetLanguages(), ", "), + strings.Join(fp.Properties.GetSubtitles(), ", "), + ) +} diff --git a/go.mod b/go.mod index 579dadd..cb218de 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,9 @@ module git.milar.in/nyaanime/organizer go 1.19 require ( + git.milar.in/milarin/channel v0.0.14 git.milar.in/milarin/envvars/v2 v2.0.0 - git.milar.in/nyaanime/logic v0.0.0-20230113102709-a719289ef360 + git.milar.in/nyaanime/logic v0.0.0-20230114105336-6bfb7dce349f git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3 git.milar.in/nyaanime/parsers v0.0.0-20230113101942-2c9bc6925201 github.com/fatih/color v1.13.0 @@ -14,11 +15,13 @@ require ( require ( git.milar.in/milarin/adverr v1.1.0 // indirect git.milar.in/milarin/anilist v1.5.1 // indirect - git.milar.in/milarin/channel v0.0.14 // indirect git.milar.in/milarin/gmath v0.0.3 // indirect git.milar.in/milarin/slices v0.0.6 // indirect + git.milar.in/milarin/tprint v0.0.7 // indirect github.com/mattn/go-colorable v0.1.9 // indirect github.com/mattn/go-isatty v0.0.14 // indirect + github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/rivo/uniseg v0.4.3 // indirect golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect gopkg.in/vansante/go-ffprobe.v2 v2.1.1 // indirect ) diff --git a/go.sum b/go.sum index 91b7246..eb54dce 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,10 @@ git.milar.in/milarin/gmath v0.0.3 h1:ii6rKNItS55O/wtIFhD1cTN2BMwDZjTBmiOocKURvxM git.milar.in/milarin/gmath v0.0.3/go.mod h1:HDLftG5RLpiNGKiIWh+O2G1PYkNzyLDADO8Cd/1abiE= git.milar.in/milarin/slices v0.0.6 h1:AQoSarZ58WHYol9c6woWJSe8wFpPC2RC4cvIlZpfg9s= git.milar.in/milarin/slices v0.0.6/go.mod h1:NOr53AOeur/qscu/FBj3lsFR262PNYBccLYSTCAXRk4= -git.milar.in/nyaanime/logic v0.0.0-20230113102709-a719289ef360 h1:pHbxMy5TnpOAc3TNEOoNIfcSe5epAEb2PSmedpWvnF4= -git.milar.in/nyaanime/logic v0.0.0-20230113102709-a719289ef360/go.mod h1:SkgdD87uNP60swIBtrDRZPBF5rLIgBLL6OetP1sETLY= +git.milar.in/milarin/tprint v0.0.7 h1:dvm4l4BhXOie4vtnRMZii0WTLTz2wju8wyUPB/lNeVo= +git.milar.in/milarin/tprint v0.0.7/go.mod h1:UwW/B+0cTCbN5hi0bQBE9rIRgLkq+x4V751rrS2KVoI= +git.milar.in/nyaanime/logic v0.0.0-20230114105336-6bfb7dce349f h1:tc8hQpZprt8/yedVSPmkftKOI2+P7twFc7BEov5RTBc= +git.milar.in/nyaanime/logic v0.0.0-20230114105336-6bfb7dce349f/go.mod h1:cLuIBDTtpXdzprOIsz73r6ZCE5s0k+o6HA7fH9OuW/M= git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3 h1:mXcEA47FQQzeSDXE3UvhNfIt4fBfpDSq1/f0r+jbHpY= git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3/go.mod h1:kPWLDvFrhc1Uf77gxsBOxNeJ5JTVF2HhVs1IdVcw0tg= git.milar.in/nyaanime/parsers v0.0.0-20230113101942-2c9bc6925201 h1:glTG4IeuIvD4mVwJyCF5SYMawCRcZZ01pz4AUyWTEP8= @@ -25,6 +27,11 @@ github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= +github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw= +github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/handle_file.go b/handle_file.go index a1fdb49..84bcb00 100644 --- a/handle_file.go +++ b/handle_file.go @@ -1,10 +1,6 @@ package main import ( - "errors" - "fmt" - "io" - "os" "path/filepath" "strings" "time" @@ -17,7 +13,7 @@ import ( func HandleFile(path string) (b *strings.Builder) { b = &strings.Builder{} - b.WriteString(color.MagentaString("\nfile found: %s\n", filepath.Base(path))) + b.WriteString(color.MagentaString("\nfile found: %s\n", path)) for _, parser := range parsers.Parsers { parsedFile, ok := parser.FileParser(&parser, path) @@ -30,7 +26,7 @@ func HandleFile(path string) (b *strings.Builder) { anime, err := logic.SearchAnimeByTitle(parsedFile.OriginalAnimeTitle) if err != nil { - b.WriteString(color.YellowString("\tanime not found: '%s'\n", parsedFile.OriginalAnimeTitle)) + b.WriteString(color.RedString("\tanime not found: '%s'\n", parsedFile.OriginalAnimeTitle)) continue } @@ -46,12 +42,21 @@ func HandleParsedFile(b *strings.Builder, parsedFile *model.ParsedFile) { newFilePrio := logic.NewFilePriority(parsedFile) oldFilePrio, animeEpNotExistLocally := logic.GetAnimeEpProps(parsedFile.AnimeEpisode()) + if animeEpNotExistLocally { + b.WriteString(color.YellowString("\tfile exists locally\n")) + b.WriteString(color.YellowString("\t local file: %s\n", FilePrio2Str(oldFilePrio))) + b.WriteString(color.YellowString("\t new file: %s\n", FilePrio2Str(newFilePrio))) + if newFilePrio.Priority > oldFilePrio.Priority { + b.WriteString(color.GreenString("\t overwrite local file\n")) + } else { + b.WriteString(color.YellowString("\t ignore new file\n")) + } + } + if !animeEpNotExistLocally || newFilePrio.Priority > oldFilePrio.Priority { - go func(b *strings.Builder, parsedFile *model.ParsedFile) { - if err := OrganizeAnimeEpisode(b, parsedFile); err != nil { - b.WriteString(color.RedString("\terror: %s", err.Error())) - } - }(b, parsedFile) + if err := OrganizeAnimeEpisode(b, parsedFile); err != nil { + b.WriteString(color.RedString("\terror: %s", err.Error())) + } } } @@ -60,53 +65,52 @@ func OrganizeAnimeEpisode(b *strings.Builder, parsedFile *model.ParsedFile) erro oldFile := filepath.Join(DownloadPath, parsedFile.File) newFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), filepath.Ext(parsedFile.File)) - lockFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), "lock") + //lockFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), "lock") b.WriteString(color.BlueString("\tmove file\n\t from: '%s'\n\t to: '%s'\n", oldFile, newFile)) - if err := os.MkdirAll(filepath.Dir(newFile), os.ModePerm); err != nil { - return err - } + // if err := os.MkdirAll(filepath.Dir(newFile), os.ModePerm); err != nil { + // return err + // } - if err := os.Chown(filepath.Dir(newFile), Uid, Gid); err != nil { - return err - } + // if err := os.Chown(filepath.Dir(newFile), Uid, Gid); err != nil { + // return err + // } - if _, err := os.Stat(newFile); !errors.Is(err, os.ErrNotExist) { - fmt.Fprintln(os.Stderr, "file already exists:", newFile) - return err - } + // if _, err := os.Stat(newFile); !errors.Is(err, os.ErrNotExist) { + // return err + // } - inputFile, err := os.Open(oldFile) - if err != nil { - return err - } - defer inputFile.Close() + // inputFile, err := os.Open(oldFile) + // if err != nil { + // return err + // } + // defer inputFile.Close() - outputFile, err := os.Create(newFile) - if err != nil { - return err - } - defer outputFile.Close() + // outputFile, err := os.Create(newFile) + // if err != nil { + // return err + // } + // defer outputFile.Close() - if err := os.Chown(newFile, Uid, Gid); err != nil { - return err - } + // if err := os.Chown(newFile, Uid, Gid); err != nil { + // return err + // } - _, err = io.Copy(outputFile, inputFile) - if err != nil { - return err - } + // _, err = io.Copy(outputFile, inputFile) + // if err != nil { + // return err + // } - if err = os.Remove(oldFile); err != nil { - return err - } + // if err = os.Remove(oldFile); err != nil { + // return err + // } - if err = os.Remove(lockFile); err != nil { - return err - } + // if err = os.Remove(lockFile); err != nil { + // return err + // } - b.WriteString(color.BlueString("\tdone (took %s)\n", time.Since(start))) + b.WriteString(color.BlueString("\t done (took %s)\n", time.Since(start))) return nil } diff --git a/main.go b/main.go index 8d02475..3bee774 100644 --- a/main.go +++ b/main.go @@ -21,15 +21,24 @@ func main() { panic(err) // TODO error handling } + logic.PrintPriorityTables() + fsChan, err := WatchDirectory(fsnotify.Create, DownloadPath) if err != nil { panic(err) // TODO error handling } - outputChan := channel.Map(fsChan, HandleFile) + outputChan := channel.MapWithRunner(fsChan, InitializeRunner(), HandleFile) channel.Each(outputChan, PrintStringBuilder) } func PrintStringBuilder(b *strings.Builder) { fmt.Println(b.String()) } + +func InitializeRunner() channel.Runner { + if ThreadCount <= 0 { + return channel.NewUnlimitedRunner() + } + return channel.NewLimitedRunner(ThreadCount) +}