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
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

View File

@ -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
}

17
main.go
View File

@ -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())
}