diff options
author | Vidhu Kant Sharma <vidhukant@protonmail.ch> | 2022-02-05 14:18:46 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@protonmail.ch> | 2022-02-05 14:18:46 +0530 |
commit | b876e67c2b21631231a06a4f7c929cb212d01595 (patch) | |
tree | 093aedcc62275ae2e929ae2f4353c72f4d50d92a /anime | |
parent | a01c567bf41778a5ca4c7d5b77eb375d4f058d63 (diff) |
implemented searching to MAL
Diffstat (limited to 'anime')
-rw-r--r-- | anime/anime.go | 39 | ||||
-rw-r--r-- | anime/anime.structs.go | 6 | ||||
-rw-r--r-- | anime/general.structs.go | 49 | ||||
-rw-r--r-- | anime/ranking.structs.go | 6 | ||||
-rw-r--r-- | anime/search.structs.go | 32 |
5 files changed, 125 insertions, 7 deletions
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 diff --git a/anime/anime.structs.go b/anime/anime.structs.go index 2db1b19..d9d4221 100644 --- a/anime/anime.structs.go +++ b/anime/anime.structs.go @@ -92,7 +92,11 @@ type Anime struct { Popularity int `json:"popularity"` NumListUsers int `json:"num_list_users"` NumScoringUsers int `json:"num_scoring_users"` - NsfwStatus string `json:"nsfw"` // find out what values are there + /* NsfwStatus potential values: + * white = sfw + * gray = probably nsfw + * black = nsfw */ + NsfwStatus string `json:"nsfw"` CreatedAt string `json:"created_at"` UpdatedAt string `json:"updated_at"` MediaType string `json:"media_type"` diff --git a/anime/general.structs.go b/anime/general.structs.go new file mode 100644 index 0000000..b77fbe5 --- /dev/null +++ b/anime/general.structs.go @@ -0,0 +1,49 @@ +/* mal2go - MyAnimeList V2 API wrapper for Go + * Copyright (C) 2022 Vidhu Kant Sharma <vidhukant@protonmail.ch> + + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. */ + +package anime + +// contains previous/next page for anime list +type ListPaging struct { + NextPage string `json:"next"` + PrevPage string `json:"previous"` // might need checking +} + +/* +// this is how the API returns data (looks horrible) +type RawRanking struct { + Data []struct { + Anime Anime `json:"node"` + Ranking struct { + Rank int `json:"rank"` + } `json:"ranking"` + } `json:"data"` + + Paging ListPaging `json:"paging"` +} + +// each anime has a ranking number +type AnimeRankingTitle struct { + Anime Anime + RankNum int +} + +// this is how mal2go returns data +type AnimeRanking struct { + Titles []AnimeRankingTitle + Paging ListPaging +} +*/ diff --git a/anime/ranking.structs.go b/anime/ranking.structs.go index faa9722..28ae860 100644 --- a/anime/ranking.structs.go +++ b/anime/ranking.structs.go @@ -16,12 +16,6 @@ package anime -// contains previous/next page for anime list -type ListPaging struct { - NextPage string `json:"next"` - PrevPage string `json:"previous"` // might need checking -} - // this is how the API returns data (looks horrible) type RawRanking struct { Data []struct { diff --git a/anime/search.structs.go b/anime/search.structs.go new file mode 100644 index 0000000..e59bb44 --- /dev/null +++ b/anime/search.structs.go @@ -0,0 +1,32 @@ +/* mal2go - MyAnimeList V2 API wrapper for Go + * Copyright (C) 2022 Vidhu Kant Sharma <vidhukant@protonmail.ch> + + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. */ + +package anime + +// this is how the API returns data (looks horrible) +type AnimeSearchRaw struct { + Data []struct { + Anime Anime `json:"node"` + } `json:"data"` + + Paging ListPaging `json:"paging"` +} + +// this is how mal2go returns data +type AnimeSearch struct { + Animes []Anime + Paging ListPaging +} |