initial commit
This commit is contained in:
71
anilist.go
Normal file
71
anilist.go
Normal file
@ -0,0 +1,71 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.tordarus.net/tordarus/anilist"
|
||||
"git.tordarus.net/tordarus/channel"
|
||||
)
|
||||
|
||||
func GetAnimeListByAnimeID(statuses []anilist.MediaListStatus) (map[anilist.MediaID]*anilist.MediaList, error) {
|
||||
animeListChannel, err := GetCurrentlyWatchingAnimes(statuses...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
toMapFunc := func(entry *anilist.MediaList) (anilist.MediaID, *anilist.MediaList) { return entry.MediaID, entry }
|
||||
return channel.ToMap(animeListChannel, toMapFunc), nil
|
||||
}
|
||||
|
||||
func GetCurrentlyWatchingAnimes(statuses ...anilist.MediaListStatus) (<-chan *anilist.MediaList, error) {
|
||||
return GetCurrentlyWatchingAnimesContext(context.Background(), statuses...)
|
||||
}
|
||||
|
||||
func GetCurrentlyWatchingAnimesContext(ctx context.Context, statuses ...anilist.MediaListStatus) (<-chan *anilist.MediaList, error) {
|
||||
token, err := GetAnilistAccessToken()
|
||||
if err != nil {
|
||||
return nil, ErrAnimeListNotObtainable.Wrap(err, "access token acquisition failed")
|
||||
}
|
||||
|
||||
media := channel.Map(channel.Of(statuses...), func(status anilist.MediaListStatus) <-chan *anilist.MediaList {
|
||||
return anilist.NewApi(token).GetMediaList(ctx, anilist.MediaListQuery{
|
||||
UserName: AnilistUsername,
|
||||
Type: anilist.MediaTypeAnime,
|
||||
Status: status,
|
||||
}, nil).Chan
|
||||
})
|
||||
|
||||
return channel.FlatChan(media), nil
|
||||
}
|
||||
|
||||
var (
|
||||
animeByTitleCache = map[string]*anilist.Media{}
|
||||
)
|
||||
|
||||
func SearchAnimeByTitle(title string) (anime *anilist.Media, err error) {
|
||||
// caching
|
||||
if cacheEntry, ok := animeByTitleCache[title]; ok {
|
||||
return cacheEntry, nil
|
||||
}
|
||||
defer func() {
|
||||
if err != nil && anime != nil {
|
||||
animeByTitleCache[title] = anime
|
||||
}
|
||||
}()
|
||||
|
||||
token, err := GetAnilistAccessToken()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
anime = anilist.NewApi(token).GetMedia(context.Background(), anilist.MediaQuery{
|
||||
Search: title,
|
||||
Type: anilist.MediaTypeAnime,
|
||||
}, nil).First()
|
||||
|
||||
if anime == nil {
|
||||
return nil, ErrAnimeNotFound.New(title)
|
||||
}
|
||||
|
||||
return anime, nil
|
||||
}
|
Reference in New Issue
Block a user