From b876e67c2b21631231a06a4f7c929cb212d01595 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sat, 5 Feb 2022 14:18:46 +0530 Subject: implemented searching to MAL --- anime/anime.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'anime/anime.go') diff --git a/anime/anime.go b/anime/anime.go index 34e996c..e05b674 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -26,6 +26,45 @@ import ( const BASE_URL string = "https://api.myanimelist.net/v2/anime" +// in MAL documentation this is named Get Anime List +// TODO: handle errors (if any) +func SearchAnime(token, searchString string, limit, offset int) (AnimeSearch, error) { + var searchResults AnimeSearch + + // if limit exceeds what MAL supports + if limit > 500 { + return searchResults, errors.New(fmt.Sprintf("SearchAnime: Limit too high(%d). Max limit is 500", limit)) + } else if offset > 499 { + return searchResults, errors.New(fmt.Sprintf("SearchAnime: Offset too high(%d). Max offset for mal2go is 499", offset)) + } + + // generate endpoint url with custom params + endpoint, _ := urlGenerator( + BASE_URL, + []string{"q", "limit", "offset"}, + [][]string{{searchString}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}}, + true, + ) + + var animeSearchData AnimeSearchRaw + data := requestHandler(token, endpoint) + json.Unmarshal([]byte(data), &animeSearchData) + + // Adding all the animes to another list to get formatted results later + var animes []Anime + for _, element := range animeSearchData.Data { + animes = append(animes, element.Anime) + } + + // finally generate AnimeList + searchResults = AnimeSearch { + Animes: animes, + Paging: animeSearchData.Paging, + } + + return searchResults, nil +} + // Each anime has its own ID on MAL func GetAnimeById(token string, animeId int, fields []string) (Anime, error) { var anime Anime -- cgit v1.2.3