diff --git a/anime_episode_filepath.go b/anime_episode_filepath.go new file mode 100644 index 0000000..a01c12a --- /dev/null +++ b/anime_episode_filepath.go @@ -0,0 +1,40 @@ +package main + +import ( + "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) + } + return b.String() +} + +func AnimeEpExistsLocally(animeEp model.AnimeEpisode) bool { + animeEpPath := filepath.Join(AnimePath, GetAnimeEpFilepath(animeEp, "*")) + + files, err := filepath.Glob(animeEpPath) + if err != nil { + panic(err) + } + + return len(files) > 0 +} diff --git a/go.mod b/go.mod index 1075a4d..f48fb33 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( git.milar.in/milarin/envvars v1.0.3 git.milar.in/milarin/gmath v0.0.2 git.milar.in/milarin/slices v0.0.2 - git.milar.in/nyaanime/model v0.0.0-20220821124037-0a28c6b41556 + git.milar.in/nyaanime/model v0.0.0-20220821215714-9959e681573f git.milar.in/nyaanime/parsers v0.0.0-20220815144327-52de61265e27 github.com/PuerkitoBio/goquery v1.8.0 ) diff --git a/go.sum b/go.sum index ec3e57a..5be7d85 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,12 @@ git.milar.in/milarin/slices v0.0.2 h1:j92MuP0HWKSaHJMq/FRxDtSDIGiOTvw+KogUTwuulr git.milar.in/milarin/slices v0.0.2/go.mod h1:XRNfE99aNKeaPOY1phjOlpIQqeGCW1LOqqh8UHS+vAk= git.milar.in/nyaanime/model v0.0.0-20220821124037-0a28c6b41556 h1:RplYz4+CMK9ByI3ELusBltWFRlDs6VMOGk5EyENnLi0= git.milar.in/nyaanime/model v0.0.0-20220821124037-0a28c6b41556/go.mod h1:OzhQgj0b/Hf9fg8VXYxYt8ONQOvHm8xC44TmS9kQ150= +git.milar.in/nyaanime/model v0.0.0-20220821212334-5e9f052f86b6 h1:vmc1G5uVkYAYJYcaQKLrr+JE6u2780YR6R00EdF5A5I= +git.milar.in/nyaanime/model v0.0.0-20220821212334-5e9f052f86b6/go.mod h1:OzhQgj0b/Hf9fg8VXYxYt8ONQOvHm8xC44TmS9kQ150= +git.milar.in/nyaanime/model v0.0.0-20220821212807-44de43f4c500 h1:zwYRzcbRiS62hDu6hy4OGxMbhLnTsL13VEXNIoTodsk= +git.milar.in/nyaanime/model v0.0.0-20220821212807-44de43f4c500/go.mod h1:OzhQgj0b/Hf9fg8VXYxYt8ONQOvHm8xC44TmS9kQ150= +git.milar.in/nyaanime/model v0.0.0-20220821215714-9959e681573f h1:BKxr1hWCl2QfSiOis1NaRcoG1mZlwo5VnB+0mYhmfEw= +git.milar.in/nyaanime/model v0.0.0-20220821215714-9959e681573f/go.mod h1:OzhQgj0b/Hf9fg8VXYxYt8ONQOvHm8xC44TmS9kQ150= git.milar.in/nyaanime/parsers v0.0.0-20220815144327-52de61265e27 h1:0+5j9MMJQS8+Luss19hD6hvNFxcBDRal2XwSUTyq7WU= git.milar.in/nyaanime/parsers v0.0.0-20220815144327-52de61265e27/go.mod h1:qm6fIFBFs90uz7IJ8RKgDir0K8Fa8isixGLgrtC6kgU= github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U= diff --git a/main.go b/main.go index 992ad0c..72b3b04 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "html/template" "math" "time" @@ -21,6 +22,15 @@ var ( StorageUser = envvars.String("STORAGE_USER", "") StoragePass = envvars.String("STORAGE_PASS", "") + DownloadPath = envvars.String("DOWNLOAD_PATH", "") + AnimePath = envvars.String("ANIME_PATH", "") + + AnimeEpFilepathPattern = envvars.Object( + "EPISODE_FILEPATH_PATTERN", + template.Must(template.New("anime-episode-filepath-pattern").Parse(`{{.Title.UserPreferred}}/{{.Title.UserPreferred}} Episode {{.Episode}}.{{.Ext}}`)), + template.New("anime-episode-filepath-pattern").Parse, + ) + AnimeStatus = envvars.ObjectSlice("ANIME_STATUS", ",", ParseMediaListStatus) // essential torrent properties @@ -92,20 +102,34 @@ func checkTorrents() { return } - /*animes, err = GetAnimesToDownloadByAnimeID() + animes, err := GetAnimesToDownloadByAnimeID() if err != nil { fmt.Println(adverr.Wrap("retrieving anime list failed", err)) return - }*/ + } parsedTorrents := ParseTorrentsByAnimeEpSortedByProperties(torrents) for animeEp, torrentPriorities := range parsedTorrents { - fmt.Printf("\nanime: %s | episode: %d | torrents found: %d\n", animeEp.Anime, animeEp.Episode, len(torrentPriorities)) - for _, torrentPriority := range torrentPriorities { - PrintTorrentPriority(torrentPriority) + fmt.Printf("\nanime: %s | episode: %d | torrents found: %d\n", animeEp.Anime.Title.Romaji, animeEp.Episode, len(torrentPriorities)) - // TODO download anime episode with highest priority (first on in slice) + for _, torrentPriority := range torrentPriorities { + /*animeListEntry*/ _, ok := animes[torrentPriority.ParsedTorrent.Anime.ID] + if !ok { + fmt.Printf("%s | NOT ON LIST\n", FormatTorrentPriority(torrentPriority)) + continue + } + + if AnimeEpExistsLocally(animeEp) { + // TODO check if current torrent has higher priority than downloaded one (might not have any priority) + continue + } + + // TODO is currently downloading? (database query) + + fmt.Println(FormatTorrentPriority(torrentPriority)) + + // TODO download anime episode with highest priority (first one in slice) } } diff --git a/torrent_parse.go b/torrent_parse.go index f5c1d7e..b29533b 100644 --- a/torrent_parse.go +++ b/torrent_parse.go @@ -42,7 +42,7 @@ func ParseTorrentsByAnimeEp(torrents []model.Torrent) map[model.AnimeEpisode][]* } animeEpisode := model.AnimeEpisode{ - Anime: parsedTorrent.Anime.Title.Native, + Anime: parsedTorrent.Anime, Episode: parsedTorrent.Episode, } diff --git a/utils.go b/utils.go index eedb3d8..5388e8b 100644 --- a/utils.go +++ b/utils.go @@ -43,8 +43,8 @@ func Map2Str[K comparable, T any](m map[K]T) string { return str[:len(str)-1] } -func PrintTorrentPriority(torrentPriority *TorrentPriority) { - fmt.Printf("id: %s | resolution: %d | languages: %s | subtitles: %s | seeders: %d | leechers: %d, | downloads: %d | trusted: %t | preferred properties: %s | priority: %d\n", +func FormatTorrentPriority(torrentPriority *TorrentPriority) string { + return fmt.Sprintf("id: %s | resolution: %d | languages: %s | subtitles: %s | seeders: %d | leechers: %d, | downloads: %d | trusted: %t | preferred properties: %s | priority: %d", torrentPriority.ParsedTorrent.Torrent.ID, torrentPriority.ParsedTorrent.Resolution, strings.Join(torrentPriority.ParsedTorrent.Languages, ","),