2022-08-22 14:14:14 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-08-23 11:48:26 +02:00
|
|
|
"errors"
|
2022-08-22 14:14:14 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.milar.in/milarin/anilist"
|
|
|
|
"git.milar.in/nyaanime/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AnimePathPatternData struct {
|
|
|
|
Title anilist.MediaTitle
|
|
|
|
Episode int
|
|
|
|
Ext string
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAnimeEpFilepath(animeEp model.AnimeEpisode, ext string) string {
|
|
|
|
tmplData := AnimePathPatternData{
|
|
|
|
Title: animeEp.Anime.Title,
|
|
|
|
Episode: animeEp.Episode,
|
|
|
|
Ext: ext,
|
|
|
|
}
|
|
|
|
|
|
|
|
b := new(strings.Builder)
|
|
|
|
if err := AnimeEpFilepathPattern.Execute(b, tmplData); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-08-23 11:48:26 +02:00
|
|
|
return filepath.Join(AnimePath, b.String())
|
2022-08-22 14:14:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func AnimeEpExistsLocally(animeEp model.AnimeEpisode) bool {
|
2022-08-23 11:48:26 +02:00
|
|
|
animeEpPath := GetAnimeEpFilepath(animeEp, "*")
|
2022-08-22 14:14:14 +02:00
|
|
|
|
|
|
|
files, err := filepath.Glob(animeEpPath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(files) > 0
|
|
|
|
}
|
|
|
|
|
2022-08-23 13:24:03 +02:00
|
|
|
func GetAnimeEpProps(animeEp model.AnimeEpisode) (*FilePriority, bool) {
|
2022-08-23 11:48:26 +02:00
|
|
|
animeEpPath := GetAnimeEpFilepath(animeEp, "*")
|
2022-08-22 14:14:14 +02:00
|
|
|
files, err := filepath.Glob(animeEpPath)
|
|
|
|
if err != nil {
|
2022-08-23 13:24:03 +02:00
|
|
|
panic(ErrInvalidGlobSyntax.Wrap(err, animeEpPath))
|
2022-08-22 14:14:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var mostPrio *FilePriority
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
props, err := AnalyzeFile(file)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !HasFileEssentialProperties(props) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fp := NewFilePriority(props)
|
|
|
|
|
|
|
|
if mostPrio == nil || fp.Priority > mostPrio.Priority {
|
|
|
|
mostPrio = fp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 13:24:03 +02:00
|
|
|
return mostPrio, mostPrio != nil
|
2022-08-22 14:14:14 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 13:24:03 +02:00
|
|
|
func IsCurrentlyDownloading(animeEp model.AnimeEpisode) bool {
|
2022-08-23 11:48:26 +02:00
|
|
|
animeEpPath := GetAnimeEpFilepath(animeEp, "lock")
|
|
|
|
_, err := os.Stat(animeEpPath)
|
|
|
|
return !errors.Is(err, os.ErrNotExist)
|
|
|
|
}
|
2022-08-22 14:14:14 +02:00
|
|
|
|
2022-08-23 13:24:03 +02:00
|
|
|
func SetCurrentlyDownloading(animeEp model.AnimeEpisode) error {
|
2022-08-23 11:48:26 +02:00
|
|
|
animeEpPath := GetAnimeEpFilepath(animeEp, "lock")
|
|
|
|
file, err := os.Create(animeEpPath)
|
|
|
|
if err != nil {
|
|
|
|
defer file.Close()
|
2022-08-22 14:14:14 +02:00
|
|
|
}
|
2022-08-23 11:48:26 +02:00
|
|
|
return err
|
2022-08-22 14:14:14 +02:00
|
|
|
}
|