debug output improved

This commit is contained in:
milarin 2023-01-14 10:58:59 +01:00
parent 4e71f3c65b
commit 12f59dae33
3 changed files with 77 additions and 55 deletions

2
go.mod
View File

@ -3,7 +3,6 @@ module git.milar.in/nyaanime/organizer
go 1.19 go 1.19
require ( require (
git.milar.in/milarin/adverr v1.1.0
git.milar.in/milarin/envvars/v2 v2.0.0 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-20230113102709-a719289ef360
git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3 git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3
@ -13,6 +12,7 @@ require (
) )
require ( require (
git.milar.in/milarin/adverr v1.1.0 // indirect
git.milar.in/milarin/anilist v1.5.1 // indirect git.milar.in/milarin/anilist v1.5.1 // indirect
git.milar.in/milarin/channel v0.0.14 // indirect git.milar.in/milarin/channel v0.0.14 // indirect
git.milar.in/milarin/gmath v0.0.3 // indirect git.milar.in/milarin/gmath v0.0.3 // indirect

View File

@ -1,9 +1,13 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"log" "io"
"os"
"path/filepath" "path/filepath"
"strings"
"time"
"git.milar.in/nyaanime/logic" "git.milar.in/nyaanime/logic"
"git.milar.in/nyaanime/model" "git.milar.in/nyaanime/model"
@ -11,93 +15,98 @@ import (
"github.com/fatih/color" "github.com/fatih/color"
) )
func HandleFile(path string) { func HandleFile(path string) (b *strings.Builder) {
color.Magenta("\nfile found: %s\n", filepath.Base(path)) b = &strings.Builder{}
b.WriteString(color.MagentaString("\nfile found: %s\n", filepath.Base(path)))
for _, parser := range parsers.Parsers { for _, parser := range parsers.Parsers {
parsedFile, ok := parser.FileParser(&parser, path) parsedFile, ok := parser.FileParser(&parser, path)
if !ok { 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 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) anime, err := logic.SearchAnimeByTitle(parsedFile.OriginalAnimeTitle)
if err != nil { if err != nil {
color.Red("\tanime not found: '%s'\n", parsedFile.OriginalAnimeTitle) b.WriteString(color.YellowString("\tanime not found: '%s'\n", parsedFile.OriginalAnimeTitle))
continue continue
} }
parsedFile.Anime = anime parsedFile.Anime = anime
HandleParsedFile(parsedFile) HandleParsedFile(b, parsedFile)
return return
} }
return
} }
func HandleParsedFile(parsedFile *model.ParsedFile) { func HandleParsedFile(b *strings.Builder, parsedFile *model.ParsedFile) {
newFilePrio := logic.NewFilePriority(parsedFile) newFilePrio := logic.NewFilePriority(parsedFile)
oldFilePrio, animeEpNotExistLocally := logic.GetAnimeEpProps(parsedFile.AnimeEpisode()) oldFilePrio, animeEpNotExistLocally := logic.GetAnimeEpProps(parsedFile.AnimeEpisode())
if !animeEpNotExistLocally || newFilePrio.Priority > oldFilePrio.Priority { if !animeEpNotExistLocally || newFilePrio.Priority > oldFilePrio.Priority {
fmt.Println("\tmove file") go func(b *strings.Builder, parsedFile *model.ParsedFile) {
// go func(parsedFile *model.ParsedFile) { if err := OrganizeAnimeEpisode(b, parsedFile); err != nil {
// if err := OrganizeAnimeEpisode(parsedFile); err != nil { b.WriteString(color.RedString("\terror: %s", err.Error()))
// adverr.Println(err) }
// } }(b, parsedFile)
// }(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) oldFile := filepath.Join(DownloadPath, parsedFile.File)
newFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), filepath.Ext(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 { if err := os.MkdirAll(filepath.Dir(newFile), os.ModePerm); err != nil {
// return err return err
// } }
// if err := os.Chown(filepath.Dir(newFile), Uid, Gid); err != nil { if err := os.Chown(filepath.Dir(newFile), Uid, Gid); err != nil {
// return err return err
// } }
// if _, err := os.Stat(newFile); !errors.Is(err, os.ErrNotExist) { if _, err := os.Stat(newFile); !errors.Is(err, os.ErrNotExist) {
// fmt.Fprintln(os.Stderr, "file already exists:", newFile) fmt.Fprintln(os.Stderr, "file already exists:", newFile)
// return err return err
// } }
// inputFile, err := os.Open(oldFile) inputFile, err := os.Open(oldFile)
// if err != nil { if err != nil {
// return err return err
// } }
// defer inputFile.Close() defer inputFile.Close()
// outputFile, err := os.Create(newFile) outputFile, err := os.Create(newFile)
// if err != nil { if err != nil {
// return err return err
// } }
// defer outputFile.Close() defer outputFile.Close()
// if err := os.Chown(newFile, Uid, Gid); err != nil { if err := os.Chown(newFile, Uid, Gid); err != nil {
// return err return err
// } }
// _, err = io.Copy(outputFile, inputFile) _, err = io.Copy(outputFile, inputFile)
// if err != nil { if err != nil {
// return err return err
// } }
// if err = os.Remove(oldFile); err != nil { if err = os.Remove(oldFile); err != nil {
// return err return err
// } }
// if err = os.Remove(lockFile); err != nil { if err = os.Remove(lockFile); err != nil {
// return err return err
// } }
log.Printf("file '%s' moved\n", newFile) b.WriteString(color.BlueString("\tdone (took %s)\n", time.Since(start)))
return nil return nil
} }

17
main.go
View File

@ -1,11 +1,21 @@
package main package main
import ( import (
"fmt"
"os/exec"
"strings"
"git.milar.in/milarin/channel"
"git.milar.in/nyaanime/logic" "git.milar.in/nyaanime/logic"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
) )
func main() { 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 // get access token once at startup to be sure that an access token is obtainable at all
if _, err := logic.GetAnilistAccessToken(); err != nil { if _, err := logic.GetAnilistAccessToken(); err != nil {
panic(err) // TODO error handling panic(err) // TODO error handling
@ -16,7 +26,10 @@ func main() {
panic(err) // TODO error handling panic(err) // TODO error handling
} }
for file := range fsChan { outputChan := channel.Map(fsChan, HandleFile)
HandleFile(file) channel.Each(outputChan, PrintStringBuilder)
} }
func PrintStringBuilder(b *strings.Builder) {
fmt.Println(b.String())
} }