debug output improved

This commit is contained in:
milarin 2023-01-14 12:11:10 +01:00
parent da27f82479
commit 2e56da71c9
7 changed files with 94 additions and 51 deletions

Binary file not shown.

View File

@ -2,6 +2,7 @@ package main
import ( import (
"os/user" "os/user"
"runtime"
"strconv" "strconv"
"git.milar.in/milarin/envvars/v2" "git.milar.in/milarin/envvars/v2"
@ -10,6 +11,8 @@ import (
var ( var (
DownloadPath = envvars.String("DOWNLOAD_PATH", "") DownloadPath = envvars.String("DOWNLOAD_PATH", "")
ThreadCount = envvars.Int("THREADS", runtime.NumCPU())
Uid = envvars.Object("UID", 1000, func(s string) (int, error) { Uid = envvars.Object("UID", 1000, func(s string) (int, error) {
if uid, err := strconv.Atoi(s); err == nil { if uid, err := strconv.Atoi(s); err == nil {
return uid, nil return uid, nil

17
file_priority_string.go Normal file
View File

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

7
go.mod
View File

@ -3,8 +3,9 @@ module git.milar.in/nyaanime/organizer
go 1.19 go 1.19
require ( require (
git.milar.in/milarin/channel v0.0.14
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-20230114105336-6bfb7dce349f
git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3 git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3
git.milar.in/nyaanime/parsers v0.0.0-20230113101942-2c9bc6925201 git.milar.in/nyaanime/parsers v0.0.0-20230113101942-2c9bc6925201
github.com/fatih/color v1.13.0 github.com/fatih/color v1.13.0
@ -14,11 +15,13 @@ require (
require ( require (
git.milar.in/milarin/adverr v1.1.0 // indirect 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/gmath v0.0.3 // indirect git.milar.in/milarin/gmath v0.0.3 // indirect
git.milar.in/milarin/slices v0.0.6 // 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-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // 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 golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
gopkg.in/vansante/go-ffprobe.v2 v2.1.1 // indirect gopkg.in/vansante/go-ffprobe.v2 v2.1.1 // indirect
) )

11
go.sum
View File

@ -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/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 h1:AQoSarZ58WHYol9c6woWJSe8wFpPC2RC4cvIlZpfg9s=
git.milar.in/milarin/slices v0.0.6/go.mod h1:NOr53AOeur/qscu/FBj3lsFR262PNYBccLYSTCAXRk4= 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/milarin/tprint v0.0.7 h1:dvm4l4BhXOie4vtnRMZii0WTLTz2wju8wyUPB/lNeVo=
git.milar.in/nyaanime/logic v0.0.0-20230113102709-a719289ef360/go.mod h1:SkgdD87uNP60swIBtrDRZPBF5rLIgBLL6OetP1sETLY= 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 h1:mXcEA47FQQzeSDXE3UvhNfIt4fBfpDSq1/f0r+jbHpY=
git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3/go.mod h1:kPWLDvFrhc1Uf77gxsBOxNeJ5JTVF2HhVs1IdVcw0tg= 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 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.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 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= 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-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-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-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View File

@ -1,10 +1,6 @@
package main package main
import ( import (
"errors"
"fmt"
"io"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
@ -17,7 +13,7 @@ import (
func HandleFile(path string) (b *strings.Builder) { func HandleFile(path string) (b *strings.Builder) {
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 { for _, parser := range parsers.Parsers {
parsedFile, ok := parser.FileParser(&parser, path) parsedFile, ok := parser.FileParser(&parser, path)
@ -30,7 +26,7 @@ func HandleFile(path string) (b *strings.Builder) {
anime, err := logic.SearchAnimeByTitle(parsedFile.OriginalAnimeTitle) anime, err := logic.SearchAnimeByTitle(parsedFile.OriginalAnimeTitle)
if err != nil { 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 continue
} }
@ -46,12 +42,21 @@ 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 {
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 { if !animeEpNotExistLocally || newFilePrio.Priority > oldFilePrio.Priority {
go func(b *strings.Builder, parsedFile *model.ParsedFile) { if err := OrganizeAnimeEpisode(b, parsedFile); err != nil {
if err := OrganizeAnimeEpisode(b, parsedFile); err != nil { b.WriteString(color.RedString("\terror: %s", err.Error()))
b.WriteString(color.RedString("\terror: %s", err.Error())) }
}
}(b, parsedFile)
} }
} }
@ -60,53 +65,52 @@ func OrganizeAnimeEpisode(b *strings.Builder, parsedFile *model.ParsedFile) erro
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")
b.WriteString(color.BlueString("\tmove file\n\t from: '%s'\n\t 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) // 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
} // }
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 return nil
} }

11
main.go
View File

@ -21,15 +21,24 @@ func main() {
panic(err) // TODO error handling panic(err) // TODO error handling
} }
logic.PrintPriorityTables()
fsChan, err := WatchDirectory(fsnotify.Create, DownloadPath) fsChan, err := WatchDirectory(fsnotify.Create, DownloadPath)
if err != nil { if err != nil {
panic(err) // TODO error handling panic(err) // TODO error handling
} }
outputChan := channel.Map(fsChan, HandleFile) outputChan := channel.MapWithRunner(fsChan, InitializeRunner(), HandleFile)
channel.Each(outputChan, PrintStringBuilder) channel.Each(outputChan, PrintStringBuilder)
} }
func PrintStringBuilder(b *strings.Builder) { func PrintStringBuilder(b *strings.Builder) {
fmt.Println(b.String()) fmt.Println(b.String())
} }
func InitializeRunner() channel.Runner {
if ThreadCount <= 0 {
return channel.NewUnlimitedRunner()
}
return channel.NewLimitedRunner(ThreadCount)
}