This commit is contained in:
milarin 2023-01-14 10:33:29 +01:00
parent 3817f3ac36
commit 4e71f3c65b
6 changed files with 62 additions and 56 deletions

Binary file not shown.

3
go.mod
View File

@ -8,6 +8,7 @@ require (
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
)
@ -16,6 +17,8 @@ require (
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
)

10
go.sum
View File

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

View File

@ -1,37 +1,37 @@
package main
import (
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
"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) bool {
func HandleFile(path string) {
color.Magenta("\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)
continue
}
color.Green("\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)
continue
}
parsedFile.Anime = anime
HandleParsedFile(parsedFile)
return true
return
}
return false
}
func HandleParsedFile(parsedFile *model.ParsedFile) {
@ -39,62 +39,63 @@ func HandleParsedFile(parsedFile *model.ParsedFile) {
oldFilePrio, animeEpNotExistLocally := logic.GetAnimeEpProps(parsedFile.AnimeEpisode())
if !animeEpNotExistLocally || newFilePrio.Priority > oldFilePrio.Priority {
go func(parsedFile *model.ParsedFile) {
if err := OrganizeAnimeEpisode(parsedFile); err != nil {
adverr.Println(err)
}
}(parsedFile)
fmt.Println("\tmove file")
// go func(parsedFile *model.ParsedFile) {
// if err := OrganizeAnimeEpisode(parsedFile); err != nil {
// adverr.Println(err)
// }
// }(parsedFile)
}
}
func OrganizeAnimeEpisode(parsedFile *model.ParsedFile) error {
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)
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)

10
main.go
View File

@ -1,8 +1,6 @@
package main
import (
"log"
"git.milar.in/nyaanime/logic"
"github.com/fsnotify/fsnotify"
)
@ -19,12 +17,6 @@ func main() {
}
for file := range fsChan {
fileHandled := HandleFile(file)
if fileHandled {
log.Println("file handled:", file)
} else {
log.Println("file ignored:", file)
}
HandleFile(file)
}
}