From 12f59dae338116bcd5679166ee603732db647fd7 Mon Sep 17 00:00:00 2001 From: milarin Date: Sat, 14 Jan 2023 10:58:59 +0100 Subject: [PATCH] debug output improved --- go.mod | 2 +- handle_file.go | 111 ++++++++++++++++++++++++++----------------------- main.go | 19 +++++++-- 3 files changed, 77 insertions(+), 55 deletions(-) diff --git a/go.mod b/go.mod index f95c905..579dadd 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module git.milar.in/nyaanime/organizer go 1.19 require ( - git.milar.in/milarin/adverr v1.1.0 git.milar.in/milarin/envvars/v2 v2.0.0 git.milar.in/nyaanime/logic v0.0.0-20230113102709-a719289ef360 git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3 @@ -13,6 +12,7 @@ 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 diff --git a/handle_file.go b/handle_file.go index fc6e779..a1fdb49 100644 --- a/handle_file.go +++ b/handle_file.go @@ -1,9 +1,13 @@ package main import ( + "errors" "fmt" - "log" + "io" + "os" "path/filepath" + "strings" + "time" "git.milar.in/nyaanime/logic" "git.milar.in/nyaanime/model" @@ -11,93 +15,98 @@ import ( "github.com/fatih/color" ) -func HandleFile(path string) { - color.Magenta("\nfile found: %s\n", filepath.Base(path)) +func HandleFile(path string) (b *strings.Builder) { + b = &strings.Builder{} + b.WriteString(color.MagentaString("\nfile found: %s\n", filepath.Base(path))) + for _, parser := range parsers.Parsers { parsedFile, ok := parser.FileParser(&parser, path) if !ok { - color.Red("\tnot parsable with parser '%s'\n", parser.Identity) + b.WriteString(color.YellowString("\tnot parsable with parser '%s'\n", parser.Identity)) continue } - color.Green("\tparsable with parser '%s'\n", parser.Identity) + b.WriteString(color.GreenString("\tparsable with parser '%s'\n", parser.Identity)) anime, err := logic.SearchAnimeByTitle(parsedFile.OriginalAnimeTitle) if err != nil { - color.Red("\tanime not found: '%s'\n", parsedFile.OriginalAnimeTitle) + b.WriteString(color.YellowString("\tanime not found: '%s'\n", parsedFile.OriginalAnimeTitle)) continue } parsedFile.Anime = anime - HandleParsedFile(parsedFile) + HandleParsedFile(b, parsedFile) return } + + return } -func HandleParsedFile(parsedFile *model.ParsedFile) { +func HandleParsedFile(b *strings.Builder, parsedFile *model.ParsedFile) { newFilePrio := logic.NewFilePriority(parsedFile) oldFilePrio, animeEpNotExistLocally := logic.GetAnimeEpProps(parsedFile.AnimeEpisode()) if !animeEpNotExistLocally || newFilePrio.Priority > oldFilePrio.Priority { - fmt.Println("\tmove file") - // go func(parsedFile *model.ParsedFile) { - // if err := OrganizeAnimeEpisode(parsedFile); err != nil { - // adverr.Println(err) - // } - // }(parsedFile) + 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) } } -func OrganizeAnimeEpisode(parsedFile *model.ParsedFile) error { +func OrganizeAnimeEpisode(b *strings.Builder, parsedFile *model.ParsedFile) error { + start := time.Now() + 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") - log.Printf("move file '%s' to '%s'\n", oldFile, newFile) + 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) { + fmt.Fprintln(os.Stderr, "file already exists:", newFile) + 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 + } - log.Printf("file '%s' moved\n", newFile) + b.WriteString(color.BlueString("\tdone (took %s)\n", time.Since(start))) return nil } diff --git a/main.go b/main.go index 2430264..8d02475 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,21 @@ package main import ( + "fmt" + "os/exec" + "strings" + + "git.milar.in/milarin/channel" "git.milar.in/nyaanime/logic" "github.com/fsnotify/fsnotify" ) func main() { + // check for ffprobe in PATH + if _, err := exec.LookPath("ffprobe"); err != nil { + panic(err) // TODO error handling + } + // get access token once at startup to be sure that an access token is obtainable at all if _, err := logic.GetAnilistAccessToken(); err != nil { panic(err) // TODO error handling @@ -16,7 +26,10 @@ func main() { panic(err) // TODO error handling } - for file := range fsChan { - HandleFile(file) - } + outputChan := channel.Map(fsChan, HandleFile) + channel.Each(outputChan, PrintStringBuilder) +} + +func PrintStringBuilder(b *strings.Builder) { + fmt.Println(b.String()) }