2023-01-13 19:23:36 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-01-15 14:08:45 +01:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
2023-01-13 19:23:36 +01:00
|
|
|
"path/filepath"
|
2023-01-13 20:04:57 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
2023-01-13 19:23:36 +01:00
|
|
|
|
|
|
|
"git.milar.in/nyaanime/logic"
|
|
|
|
"git.milar.in/nyaanime/model"
|
|
|
|
"git.milar.in/nyaanime/parsers"
|
2023-01-13 20:04:57 +01:00
|
|
|
"github.com/fatih/color"
|
2023-01-13 19:23:36 +01:00
|
|
|
)
|
|
|
|
|
2023-01-13 20:04:57 +01:00
|
|
|
func HandleFile(path string) (b *strings.Builder) {
|
|
|
|
b = &strings.Builder{}
|
2023-01-15 13:59:48 +01:00
|
|
|
b.WriteString(color.MagentaString("%s file found: %s\n", time.Now().Format("2006-01-02 15:04:05"), path))
|
2023-01-13 20:04:57 +01:00
|
|
|
|
2023-01-13 19:23:36 +01:00
|
|
|
for _, parser := range parsers.Parsers {
|
|
|
|
parsedFile, ok := parser.FileParser(&parser, path)
|
|
|
|
if !ok {
|
2023-01-13 20:04:57 +01:00
|
|
|
b.WriteString(color.YellowString("\tnot parsable with parser '%s'\n", parser.Identity))
|
2023-01-13 19:23:36 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-01-13 20:04:57 +01:00
|
|
|
b.WriteString(color.GreenString("\tparsable with parser '%s'\n", parser.Identity))
|
|
|
|
|
2023-01-13 19:23:36 +01:00
|
|
|
anime, err := logic.SearchAnimeByTitle(parsedFile.OriginalAnimeTitle)
|
|
|
|
if err != nil {
|
2023-01-14 12:11:10 +01:00
|
|
|
b.WriteString(color.RedString("\tanime not found: '%s'\n", parsedFile.OriginalAnimeTitle))
|
2023-01-13 19:23:36 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedFile.Anime = anime
|
2023-01-13 20:04:57 +01:00
|
|
|
HandleParsedFile(b, parsedFile)
|
|
|
|
return
|
2023-01-13 19:23:36 +01:00
|
|
|
}
|
2023-01-13 20:04:57 +01:00
|
|
|
|
|
|
|
return
|
2023-01-13 19:23:36 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 20:04:57 +01:00
|
|
|
func HandleParsedFile(b *strings.Builder, parsedFile *model.ParsedFile) {
|
2023-01-13 19:23:36 +01:00
|
|
|
newFilePrio := logic.NewFilePriority(parsedFile)
|
|
|
|
oldFilePrio, animeEpNotExistLocally := logic.GetAnimeEpProps(parsedFile.AnimeEpisode())
|
|
|
|
|
2023-01-14 12:11:10 +01:00
|
|
|
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"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 19:23:36 +01:00
|
|
|
if !animeEpNotExistLocally || newFilePrio.Priority > oldFilePrio.Priority {
|
2023-01-15 13:59:48 +01:00
|
|
|
// TODO remove old file when overwriting existing episode (might have other file extension)
|
2023-01-14 12:11:10 +01:00
|
|
|
if err := OrganizeAnimeEpisode(b, parsedFile); err != nil {
|
2023-01-15 13:59:48 +01:00
|
|
|
b.WriteString(color.RedString("\terror: %s\n", err.Error()))
|
2023-01-14 12:11:10 +01:00
|
|
|
}
|
2023-01-13 19:23:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 20:04:57 +01:00
|
|
|
func OrganizeAnimeEpisode(b *strings.Builder, parsedFile *model.ParsedFile) error {
|
|
|
|
start := time.Now()
|
|
|
|
|
2023-01-13 19:23:36 +01:00
|
|
|
oldFile := filepath.Join(DownloadPath, parsedFile.File)
|
|
|
|
newFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), filepath.Ext(parsedFile.File))
|
2023-01-15 14:08:45 +01:00
|
|
|
lockFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), "lock")
|
2023-01-13 19:23:36 +01:00
|
|
|
|
2023-01-13 20:04:57 +01:00
|
|
|
b.WriteString(color.BlueString("\tmove file\n\t from: '%s'\n\t to: '%s'\n", oldFile, newFile))
|
2023-01-13 19:23:36 +01:00
|
|
|
|
2023-01-15 14:08:45 +01:00
|
|
|
if err := os.MkdirAll(filepath.Dir(newFile), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-14 12:23:00 +01:00
|
|
|
|
2023-01-15 14:10:27 +01:00
|
|
|
b.WriteString("1")
|
|
|
|
|
2023-01-15 14:08:45 +01:00
|
|
|
if err := os.Chown(filepath.Dir(newFile), Uid, Gid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-14 12:23:00 +01:00
|
|
|
|
2023-01-15 14:10:27 +01:00
|
|
|
b.WriteString("2")
|
|
|
|
|
2023-01-15 14:08:45 +01:00
|
|
|
if _, err := os.Stat(newFile); !errors.Is(err, os.ErrNotExist) {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-14 12:23:00 +01:00
|
|
|
|
2023-01-15 14:10:27 +01:00
|
|
|
b.WriteString("3")
|
|
|
|
|
2023-01-15 14:08:45 +01:00
|
|
|
inputFile, err := os.Open(oldFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer inputFile.Close()
|
2023-01-14 12:23:00 +01:00
|
|
|
|
2023-01-15 14:10:27 +01:00
|
|
|
b.WriteString("4")
|
|
|
|
|
2023-01-15 14:08:45 +01:00
|
|
|
outputFile, err := os.Create(newFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer outputFile.Close()
|
2023-01-14 12:23:00 +01:00
|
|
|
|
2023-01-15 14:10:27 +01:00
|
|
|
b.WriteString("5")
|
|
|
|
|
2023-01-15 14:08:45 +01:00
|
|
|
if err := os.Chown(newFile, Uid, Gid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-14 12:23:00 +01:00
|
|
|
|
2023-01-15 14:10:27 +01:00
|
|
|
b.WriteString("6")
|
|
|
|
|
2023-01-15 14:08:45 +01:00
|
|
|
written, err := io.Copy(outputFile, inputFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-15 13:59:48 +01:00
|
|
|
|
2023-01-15 14:10:27 +01:00
|
|
|
b.WriteString("7 " + FormatBytes(written))
|
|
|
|
|
2023-01-15 14:08:45 +01:00
|
|
|
if err = os.Remove(oldFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-15 13:59:48 +01:00
|
|
|
|
2023-01-15 14:10:27 +01:00
|
|
|
b.WriteString("8")
|
|
|
|
|
2023-01-15 14:08:45 +01:00
|
|
|
if err = os.Remove(lockFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-13 19:23:36 +01:00
|
|
|
|
2023-01-15 14:08:45 +01:00
|
|
|
b.WriteString(color.BlueString("\t done (copying %s took %s)\n", FormatBytes(written), time.Since(start).Truncate(100*time.Millisecond)))
|
2023-01-13 19:23:36 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|