Compare commits
3 Commits
179919fb97
...
34fdb578a4
Author | SHA1 | Date | |
---|---|---|---|
34fdb578a4 | |||
5d38408a2a | |||
a69e593d70 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
.env
|
.env
|
||||||
|
organizer
|
||||||
|
20
bytechan_writer.go
Normal file
20
bytechan_writer.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
|
func NewWriterFromByteChan(ch chan byte) *ByteChanWriter {
|
||||||
|
return &ByteChanWriter{ch}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ByteChanWriter struct {
|
||||||
|
ch chan byte
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ io.Writer = &ByteChanWriter{}
|
||||||
|
|
||||||
|
func (w *ByteChanWriter) Write(p []byte) (n int, err error) {
|
||||||
|
for _, b := range p {
|
||||||
|
w.ch <- b
|
||||||
|
}
|
||||||
|
return len(p), nil
|
||||||
|
}
|
@ -13,6 +13,8 @@ var (
|
|||||||
|
|
||||||
ThreadCount = envvars.Int("THREADS", runtime.NumCPU())
|
ThreadCount = envvars.Int("THREADS", runtime.NumCPU())
|
||||||
|
|
||||||
|
DeleteLowPriorityFiles = envvars.Bool("DELETE_LOW_PRIORITY_FILES", false)
|
||||||
|
|
||||||
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
|
||||||
|
2
go.mod
2
go.mod
@ -7,7 +7,7 @@ require (
|
|||||||
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-20230114105336-6bfb7dce349f
|
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-20230115135225-d80026a240a2
|
||||||
github.com/fatih/color v1.13.0
|
github.com/fatih/color v1.13.0
|
||||||
github.com/fsnotify/fsnotify v1.5.4
|
github.com/fsnotify/fsnotify v1.5.4
|
||||||
)
|
)
|
||||||
|
2
go.sum
2
go.sum
@ -18,6 +18,8 @@ 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/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=
|
||||||
git.milar.in/nyaanime/parsers v0.0.0-20230113101942-2c9bc6925201/go.mod h1:GG4vtUIfxopZc/+Y8OAa//vWJw/m6aeoGN7fw6SLiEM=
|
git.milar.in/nyaanime/parsers v0.0.0-20230113101942-2c9bc6925201/go.mod h1:GG4vtUIfxopZc/+Y8OAa//vWJw/m6aeoGN7fw6SLiEM=
|
||||||
|
git.milar.in/nyaanime/parsers v0.0.0-20230115135225-d80026a240a2 h1:Q95JBR9mXENAjRhvzPAsFjPfxY0ljUiLVlhfAO4q6UY=
|
||||||
|
git.milar.in/nyaanime/parsers v0.0.0-20230115135225-d80026a240a2/go.mod h1:GG4vtUIfxopZc/+Y8OAa//vWJw/m6aeoGN7fw6SLiEM=
|
||||||
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
|
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
|
||||||
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
|
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 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
|
||||||
|
@ -2,10 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.milar.in/nyaanime/logic"
|
"git.milar.in/nyaanime/logic"
|
||||||
@ -14,63 +14,80 @@ import (
|
|||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleFile(path string) (b *strings.Builder) {
|
func HandleFile(path string) <-chan byte {
|
||||||
b = &strings.Builder{}
|
out := make(chan byte, 1024)
|
||||||
b.WriteString(color.MagentaString("\nfile found: %s\n", path))
|
defer close(out)
|
||||||
|
w := NewWriterFromByteChan(out)
|
||||||
|
|
||||||
|
fmt.Fprint(w, color.MagentaString("%s file found: %s\n", time.Now().Format("2006-01-02 15:04:05"), 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 {
|
||||||
b.WriteString(color.YellowString("\tnot parsable with parser '%s'\n", parser.Identity))
|
fmt.Fprint(w, color.YellowString("\tnot parsable with parser '%s'\n", parser.Identity))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
b.WriteString(color.GreenString("\tparsable with parser '%s'\n", parser.Identity))
|
fmt.Fprint(w, 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 {
|
||||||
b.WriteString(color.RedString("\tanime not found: '%s'\n", parsedFile.OriginalAnimeTitle))
|
fmt.Fprint(w, color.RedString("\tanime not found: '%s'\n", parsedFile.OriginalAnimeTitle))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedFile.Anime = anime
|
parsedFile.Anime = anime
|
||||||
HandleParsedFile(b, parsedFile)
|
HandleParsedFile(w, parsedFile)
|
||||||
return
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleParsedFile(b *strings.Builder, parsedFile *model.ParsedFile) {
|
func HandleParsedFile(w io.Writer, parsedFile *model.ParsedFile) {
|
||||||
newFilePrio := logic.NewFilePriority(parsedFile)
|
newFilePrio := logic.NewFilePriority(parsedFile)
|
||||||
oldFilePrio, animeEpNotExistLocally := logic.GetAnimeEpProps(parsedFile.AnimeEpisode())
|
oldFilePrio, animeEpNotExistLocally := logic.GetAnimeEpProps(parsedFile.AnimeEpisode())
|
||||||
|
|
||||||
|
// debug output
|
||||||
if animeEpNotExistLocally {
|
if animeEpNotExistLocally {
|
||||||
b.WriteString(color.YellowString("\tfile exists locally\n"))
|
fmt.Fprint(w, color.YellowString("\tfile exists locally\n"))
|
||||||
b.WriteString(color.YellowString("\t local file: %s\n", FilePrio2Str(oldFilePrio)))
|
fmt.Fprint(w, color.YellowString("\t local file: %s\n", FilePrio2Str(oldFilePrio)))
|
||||||
b.WriteString(color.YellowString("\t new file: %s\n", FilePrio2Str(newFilePrio)))
|
fmt.Fprint(w, color.YellowString("\t new file: %s\n", FilePrio2Str(newFilePrio)))
|
||||||
if newFilePrio.Priority > oldFilePrio.Priority {
|
if newFilePrio.Priority > oldFilePrio.Priority {
|
||||||
b.WriteString(color.GreenString("\t overwrite local file\n"))
|
fmt.Fprint(w, color.GreenString("\t overwrite local file\n"))
|
||||||
} else {
|
} else {
|
||||||
b.WriteString(color.YellowString("\t ignore new file\n"))
|
fmt.Fprint(w, color.YellowString("\t ignore new file\n"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// delete files with lower priority from DownloadPath
|
||||||
|
if DeleteLowPriorityFiles && animeEpNotExistLocally && oldFilePrio.Priority >= newFilePrio.Priority {
|
||||||
|
fmt.Fprint(w, color.YellowString("\tdelete file with lower priority '%s'", parsedFile.File))
|
||||||
|
err := os.Remove(parsedFile.File)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprint(w, color.RedString(" failed: '%s'\n", err.Error()))
|
||||||
|
} else {
|
||||||
|
fmt.Fprint(w, color.GreenString(" done\n"))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if !animeEpNotExistLocally || newFilePrio.Priority > oldFilePrio.Priority {
|
if !animeEpNotExistLocally || newFilePrio.Priority > oldFilePrio.Priority {
|
||||||
if err := OrganizeAnimeEpisode(b, parsedFile); err != nil {
|
// TODO remove old file when overwriting existing episode (might have other file extension)
|
||||||
b.WriteString(color.RedString("\terror: %s", err.Error()))
|
if err := OrganizeAnimeEpisode(w, parsedFile); err != nil {
|
||||||
|
fmt.Fprint(w, color.RedString("\terror: %s\n", err.Error()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func OrganizeAnimeEpisode(b *strings.Builder, parsedFile *model.ParsedFile) error {
|
func OrganizeAnimeEpisode(w io.Writer, parsedFile *model.ParsedFile) error {
|
||||||
start := time.Now()
|
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")
|
||||||
|
|
||||||
b.WriteString(color.BlueString("\tmove file\n\t from: '%s'\n\t to: '%s'\n", oldFile, newFile))
|
fmt.Fprint(w, 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
|
||||||
@ -80,7 +97,7 @@ func OrganizeAnimeEpisode(b *strings.Builder, parsedFile *model.ParsedFile) erro
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(newFile); !errors.Is(err, os.ErrNotExist) {
|
if _, err := os.Stat(newFile); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,7 +117,7 @@ func OrganizeAnimeEpisode(b *strings.Builder, parsedFile *model.ParsedFile) erro
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = io.Copy(outputFile, inputFile)
|
written, err := io.Copy(outputFile, inputFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -109,11 +126,11 @@ func OrganizeAnimeEpisode(b *strings.Builder, parsedFile *model.ParsedFile) erro
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = os.Remove(lockFile); err != nil {
|
if err = os.Remove(lockFile); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
b.WriteString(color.BlueString("\t done (took %s)\n", time.Since(start)))
|
fmt.Fprint(w, color.BlueString("\t done (copied %s in %s)\n", FormatBytes(written), time.Since(start).Truncate(100*time.Millisecond)))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
9
main.go
9
main.go
@ -1,9 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.milar.in/milarin/channel"
|
"git.milar.in/milarin/channel"
|
||||||
"git.milar.in/nyaanime/logic"
|
"git.milar.in/nyaanime/logic"
|
||||||
@ -29,11 +28,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
outputChan := channel.MapWithRunner(fsChan, InitializeRunner(), HandleFile)
|
outputChan := channel.MapWithRunner(fsChan, InitializeRunner(), HandleFile)
|
||||||
channel.Each(outputChan, PrintStringBuilder)
|
channel.Each(outputChan, PrintByteChanFunc(os.Stdout))
|
||||||
}
|
|
||||||
|
|
||||||
func PrintStringBuilder(b *strings.Builder) {
|
|
||||||
fmt.Println(b.String())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitializeRunner() channel.Runner {
|
func InitializeRunner() channel.Runner {
|
||||||
|
43
utils.go
Normal file
43
utils.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"git.milar.in/milarin/gmath"
|
||||||
|
)
|
||||||
|
|
||||||
|
func FormatBytes[T gmath.Integer](bytes T) string {
|
||||||
|
value := float64(bytes)
|
||||||
|
|
||||||
|
if value >= 1000000000000 {
|
||||||
|
return fmt.Sprintf("%.02fT", value/1000000000000)
|
||||||
|
} else if value >= 1000000000 {
|
||||||
|
return fmt.Sprintf("%.02fG", value/1000000000)
|
||||||
|
} else if value >= 1000000 {
|
||||||
|
return fmt.Sprintf("%.02fM", value/1000000)
|
||||||
|
} else if value >= 1000 {
|
||||||
|
return fmt.Sprintf("%.02fK", value/1000)
|
||||||
|
} else {
|
||||||
|
return fmt.Sprintf("%.02fB", value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrintByteChan(w io.Writer, ch <-chan byte) error {
|
||||||
|
for b := range ch {
|
||||||
|
if _, err := w.Write([]byte{b}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintln(w)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrintByteChanFunc(w io.Writer) func(ch <-chan byte) {
|
||||||
|
return func(ch <-chan byte) {
|
||||||
|
if err := PrintByteChan(w, ch); err != nil {
|
||||||
|
panic(err) // TODO error handling
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user