2023-01-13 11:10:07 +01:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"git.milar.in/milarin/anilist"
|
|
|
|
"git.milar.in/milarin/channel"
|
|
|
|
)
|
|
|
|
|
2023-01-18 10:25:07 +01:00
|
|
|
func GetAnimeListByAnimeID(statuses []anilist.MediaListStatus) (map[anilist.MediaID]*anilist.MediaList, error) {
|
|
|
|
animeListChannel, err := GetCurrentlyWatchingAnimes(statuses...)
|
2023-01-13 11:10:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
toMapFunc := func(entry *anilist.MediaList) (anilist.MediaID, *anilist.MediaList) { return entry.MediaID, entry }
|
2023-01-18 10:23:25 +01:00
|
|
|
return channel.ToMap(animeListChannel, toMapFunc), nil
|
2023-01-13 11:10:07 +01:00
|
|
|
}
|
|
|
|
|
2023-01-18 10:06:16 +01:00
|
|
|
func GetCurrentlyWatchingAnimes(statuses ...anilist.MediaListStatus) (<-chan *anilist.MediaList, error) {
|
|
|
|
return GetCurrentlyWatchingAnimesContext(context.Background(), statuses...)
|
2023-01-16 15:04:56 +01:00
|
|
|
}
|
|
|
|
|
2023-01-18 10:06:16 +01:00
|
|
|
func GetCurrentlyWatchingAnimesContext(ctx context.Context, statuses ...anilist.MediaListStatus) (<-chan *anilist.MediaList, error) {
|
2023-01-13 11:10:07 +01:00
|
|
|
token, err := GetAnilistAccessToken()
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrAnimeListNotObtainable.Wrap(err, "access token acquisition failed")
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:06:16 +01:00
|
|
|
media := channel.Map(channel.Of(statuses...), func(status anilist.MediaListStatus) <-chan *anilist.MediaList {
|
2023-01-16 15:04:56 +01:00
|
|
|
return anilist.NewApi(token).GetMediaList(ctx, anilist.MediaListQuery{
|
2023-01-13 11:10:07 +01:00
|
|
|
UserName: AnilistUsername,
|
|
|
|
Type: anilist.MediaTypeAnime,
|
|
|
|
Status: status,
|
|
|
|
}, nil).Chan
|
|
|
|
})
|
|
|
|
|
|
|
|
return channel.FlatChan(media), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2023-03-03 09:52:42 +01:00
|
|
|
animeByTitleCache = map[string]*anilist.Media{}
|
2023-01-13 11:10:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func SearchAnimeByTitle(title string) (anime *anilist.Media, err error) {
|
|
|
|
// caching
|
|
|
|
if cacheEntry, ok := animeByTitleCache[title]; ok {
|
2023-03-03 09:52:42 +01:00
|
|
|
return cacheEntry, nil
|
2023-01-13 11:10:07 +01:00
|
|
|
}
|
2023-03-03 09:52:42 +01:00
|
|
|
defer func() {
|
|
|
|
if err != nil && anime != nil {
|
|
|
|
animeByTitleCache[title] = anime
|
|
|
|
}
|
|
|
|
}()
|
2023-01-13 11:10:07 +01:00
|
|
|
|
|
|
|
token, err := GetAnilistAccessToken()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
anime = anilist.NewApi(token).GetMedia(context.Background(), anilist.MediaQuery{
|
|
|
|
Search: title,
|
|
|
|
Type: anilist.MediaTypeAnime,
|
2023-01-17 17:02:34 +01:00
|
|
|
}, nil).First()
|
2023-01-13 11:10:07 +01:00
|
|
|
|
|
|
|
if anime == nil {
|
|
|
|
return nil, ErrAnimeNotFound.New(title)
|
|
|
|
}
|
|
|
|
|
|
|
|
return anime, nil
|
|
|
|
}
|