diff --git a/.fuse_hidden000024c900000005 b/.fuse_hidden000024c900000005 new file mode 100755 index 0000000..163d703 Binary files /dev/null and b/.fuse_hidden000024c900000005 differ diff --git a/go.mod b/go.mod index 42aea90..579dadd 100644 --- a/go.mod +++ b/go.mod @@ -3,19 +3,22 @@ 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 git.milar.in/nyaanime/parsers v0.0.0-20230113101942-2c9bc6925201 + github.com/fatih/color v1.13.0 github.com/fsnotify/fsnotify v1.5.4 ) 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 + github.com/mattn/go-colorable v0.1.9 // indirect + github.com/mattn/go-isatty v0.0.14 // 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 cbd2ae6..91b7246 100644 --- a/go.sum +++ b/go.sum @@ -16,8 +16,18 @@ git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3 h1:mXcEA47FQQzeSD 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= git.milar.in/nyaanime/parsers v0.0.0-20230113101942-2c9bc6925201/go.mod h1:GG4vtUIfxopZc/+Y8OAa//vWJw/m6aeoGN7fw6SLiEM= +github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= +github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI= github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= +github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U= +github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +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= +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= golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0= golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/vansante/go-ffprobe.v2 v2.1.1 h1:DIh5fMn+tlBvG7pXyUZdemVmLdERnf2xX6XOFF+0BBU= diff --git a/handle_file.go b/handle_file.go index bfa3571..a1fdb49 100644 --- a/handle_file.go +++ b/handle_file.go @@ -6,50 +6,63 @@ import ( "io" "os" "path/filepath" + "strings" + "time" - "git.milar.in/milarin/adverr" "git.milar.in/nyaanime/logic" "git.milar.in/nyaanime/model" "git.milar.in/nyaanime/parsers" + "github.com/fatih/color" ) -func HandleFile(path string) { +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 { + b.WriteString(color.YellowString("\tnot parsable with parser '%s'\n", parser.Identity)) continue } + b.WriteString(color.GreenString("\tparsable with parser '%s'\n", parser.Identity)) + anime, err := logic.SearchAnimeByTitle(parsedFile.OriginalAnimeTitle) if err != nil { + 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(animeEpNotExistLocally, newFilePrio, oldFilePrio) - go func(parsedFile *model.ParsedFile) { - if err := OrganizeAnimeEpisode(parsedFile); err != nil { - adverr.Println(err) + go func(b *strings.Builder, parsedFile *model.ParsedFile) { + if err := OrganizeAnimeEpisode(b, parsedFile); err != nil { + b.WriteString(color.RedString("\terror: %s", err.Error())) } - }(parsedFile) + }(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") - fmt.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 @@ -93,7 +106,7 @@ func OrganizeAnimeEpisode(parsedFile *model.ParsedFile) error { return err } - fmt.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()) }