From a45849821abe64ad568ea1bbe65268d9a4954f6b Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Thu, 18 Aug 2022 14:25:37 +0200 Subject: [PATCH] ANIME_STATUS introduced --- anilist.go | 28 ++++++++++++++-------------- errors.go | 1 + go.mod | 1 + go.sum | 2 ++ main.go | 14 ++++++++++++-- utils.go | 31 ++++++++++++++++++++++++++++--- 6 files changed, 58 insertions(+), 19 deletions(-) diff --git a/anilist.go b/anilist.go index fc645e0..4b89cf2 100644 --- a/anilist.go +++ b/anilist.go @@ -5,8 +5,14 @@ import ( "git.milar.in/milarin/anilist" "git.milar.in/milarin/channel" + "git.milar.in/milarin/slices" ) +type Pair[A, B any] struct { + First A + Second B +} + func GetCurrentlyWatchingAnimesByAnimeID() (map[anilist.MediaID]*anilist.MediaList, error) { watchingAnimesChannel, err := GetCurrentlyWatchingAnimes() if err != nil { @@ -23,21 +29,15 @@ func GetCurrentlyWatchingAnimes() (<-chan *anilist.MediaList, error) { return nil, ErrAnimeListNotObtainable.Wrap(err, "access token acquisition failed") } - currentMedia := anilist.NewApi(token).GetMediaList(context.Background(), anilist.MediaListQuery{ - UserName: AnilistUsername, - Type: anilist.MediaTypeAnime, - Status: anilist.MediaListStatusCurrent, - }, nil) + media := slices.Map(AnimeStatus, func(status anilist.MediaListStatus) <-chan *anilist.MediaList { + return anilist.NewApi(token).GetMediaList(context.Background(), anilist.MediaListQuery{ + UserName: AnilistUsername, + Type: anilist.MediaTypeAnime, + Status: status, + }, nil).Chan + }) - plannedMedia := anilist.NewApi(token).GetMediaList(context.Background(), anilist.MediaListQuery{ - UserName: AnilistUsername, - Type: anilist.MediaTypeAnime, - Status: anilist.MediaListStatusPlanning, - }, nil) - - // TODO add completedMedia for re-downloading last episode? - - return channel.Merge(currentMedia.Chan, plannedMedia.Chan), nil + return channel.Merge(media...), nil } var ( diff --git a/errors.go b/errors.go index 0637e8c..f8a8693 100644 --- a/errors.go +++ b/errors.go @@ -11,4 +11,5 @@ var ( ErrStorageRequestFailed = adverr.NewErrTmpl("ErrStorageRequestFailed", "request to file storage could not be made") ErrAnimeListNotObtainable = adverr.NewErrTmpl("ErrAnimeListNotObtainable", "anime list from anilist.co not obtainable (reason: %s)") ErrTorrentNotObtainable = adverr.NewErrTmpl("ErrTorrentNotObtainable", "torrents from nyaa.si not obtainable (reason: %s)") + ErrInvalidAnimeStatus = adverr.NewErrTmpl("ErrInvalidAnimeStatus", "invalid status '%s' in ANIME_STATUS (allowed: %s)") ) diff --git a/go.mod b/go.mod index 94c7d16..0b9901c 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( git.milar.in/milarin/anilist v1.5.0 git.milar.in/milarin/channel v0.0.7 git.milar.in/milarin/envvars v1.0.3 + git.milar.in/milarin/slices v0.0.0-20220818114116-f7ab541d0a5b git.milar.in/nyaanime/model v0.0.0-20220815143950-c3d8a5af20c3 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 f9e6e59..1c8881d 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ git.milar.in/milarin/channel v0.0.7 h1:cVKtwgH/EE7U+XTHcoFCClJ4LR349KanzjX9xKwRc git.milar.in/milarin/channel v0.0.7/go.mod h1:We83LTI8S7u7II3pD+A2ChCDWJfCkcBUCUqii9HjTtM= git.milar.in/milarin/envvars v1.0.3 h1:go6pYExUzPx+aLvJ5BKNkaANkBNmcrlRJ8TtWsk4uWY= git.milar.in/milarin/envvars v1.0.3/go.mod h1:rLh/HN6S254h6m2lklnImcpsy4kHFxaOjM6+Nv9GHKI= +git.milar.in/milarin/slices v0.0.0-20220818114116-f7ab541d0a5b h1:886jJlrbGpjKijCvtK9Nzwi4JZhWFFCTRWBc4yiM+TY= +git.milar.in/milarin/slices v0.0.0-20220818114116-f7ab541d0a5b/go.mod h1:XRNfE99aNKeaPOY1phjOlpIQqeGCW1LOqqh8UHS+vAk= git.milar.in/nyaanime/model v0.0.0-20220815143950-c3d8a5af20c3 h1:GyAhYs3Nl8CuzfOeLKmWwRe/Z+pEEtnWty39HfGz6iE= git.milar.in/nyaanime/model v0.0.0-20220815143950-c3d8a5af20c3/go.mod h1:OzhQgj0b/Hf9fg8VXYxYt8ONQOvHm8xC44TmS9kQ150= git.milar.in/nyaanime/parsers v0.0.0-20220815144327-52de61265e27 h1:0+5j9MMJQS8+Luss19hD6hvNFxcBDRal2XwSUTyq7WU= diff --git a/main.go b/main.go index 40dc5e3..2b4a508 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "time" "git.milar.in/milarin/adverr" + "git.milar.in/milarin/anilist" "git.milar.in/milarin/envvars" "git.milar.in/nyaanime/model" ) @@ -20,6 +21,8 @@ var ( StorageUser = envvars.String("STORAGE_USER", "") StoragePass = envvars.String("STORAGE_PASS", "") + AnimeStatus = envvars.ObjectSlice("ANIME_STATUS", ",", ParseMediaListStatus) + // essential torrent properties MaxResolution = envvars.Object("MAX_RESOLUTION", model.Resolution4K, model.ParseResolution) @@ -53,6 +56,13 @@ var ( ) func main() { + if len(AnimeStatus) == 0 { + AnimeStatus = []anilist.MediaListStatus{ + anilist.MediaListStatusCurrent, + anilist.MediaListStatusPlanning, + } + } + // get access token once at startup to be sure that an access token is obtainable at all if _, err := GetAnilistAccessToken(); err != nil { panic(err) @@ -77,11 +87,11 @@ func checkTorrents() { return } - /*watchingAnimes, err := GetCurrentlyWatchingAnimesByAnimeID() + _, err = GetCurrentlyWatchingAnimesByAnimeID() if err != nil { fmt.Println(adverr.Wrap("retrieving anime list failed", err)) return - }*/ + } parsedTorrentsByAnimeEp := FilterEssentialTorrents(ParseTorrentsByAnimeEp(torrents)) diff --git a/utils.go b/utils.go index 82392ab..ec852dc 100644 --- a/utils.go +++ b/utils.go @@ -1,6 +1,31 @@ package main -type Pair[A, B any] struct { - First A - Second B +import ( + "strings" + + "git.milar.in/milarin/anilist" + "git.milar.in/milarin/slices" +) + +var AllMediaListStatuses = []anilist.MediaListStatus{ + anilist.MediaListStatusCurrent, + anilist.MediaListStatusPlanning, + anilist.MediaListStatusCompleted, + anilist.MediaListStatusDropped, + anilist.MediaListStatusPaused, + anilist.MediaListStatusRepeating, +} + +func ParseMediaListStatus(str string) (anilist.MediaListStatus, error) { + s := anilist.MediaListStatus(str) + + allStatusesStr := slices.Map(AllMediaListStatuses, func(status anilist.MediaListStatus) string { + return string(status) + }) + + if !slices.Contains(AllMediaListStatuses, s) { + return s, ErrInvalidAnimeStatus.New(s, strings.Join(allStatusesStr, ",")) + } + + return s, nil }