From 937f3b8ada85274dfe3842f3dde8aef45c4f3ae7 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 13 Feb 2022 14:12:45 +0530 Subject: completed (?) the GetAnimeList function --- anime/anime.go | 13 ++++++++----- errhandlers/errhandlers.go | 8 ++++---- errhandlers/validators.go | 30 +++++++++++++++++++++++++++++- user/anime/animelist.go | 35 +++++++++++++++++++++++++++-------- 4 files changed, 68 insertions(+), 18 deletions(-) diff --git a/anime/anime.go b/anime/anime.go index 323105b..969b04c 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -27,12 +27,15 @@ import ( const BASE_URL string = "https://api.myanimelist.net/v2/anime" +// MAL Might change this +const maxAnimeLimit int = 500 + // in MAL documentation this is named Get Anime List func (c AnimeClient) SearchAnime(searchString string, limit, offset int, fields []string) (AnimeSearch, error) { var searchResults AnimeSearch // error handling for limit and offset - limitsErr := e.LimitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset, maxAnimeLimit) if limitsErr != nil { return searchResults, limitsErr } @@ -102,7 +105,7 @@ func (c AnimeClient) GetAnimeRanking(rankingType string, limit, offset int, fiel var animeRanking AnimeRanking // error handling for limit and offset - limitsErr := e.LimitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset, maxAnimeLimit) if limitsErr != nil { return animeRanking, limitsErr } @@ -159,7 +162,7 @@ func (c AnimeClient) GetSeasonalAnime(year, season, sort string, limit, offset i var seasonalAnime SeasonalAnime // error handling for limit and offset - limitsErr := e.LimitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset, maxAnimeLimit) if limitsErr != nil { return seasonalAnime, limitsErr } @@ -176,7 +179,7 @@ func (c AnimeClient) GetSeasonalAnime(year, season, sort string, limit, offset i } // checks if valid sort is specified - if !e.IsValidSort(sort) { + if !e.IsValidSeasonalSort(sort) { return seasonalAnime, errors.New(fmt.Sprintf("GetSeasonalAnime: Invalid sort specified: \"%s\"", sort)) } @@ -213,7 +216,7 @@ func (c AnimeClient) GetSuggestedAnime(limit, offset int, fields []string) (Sugg var suggestedAnime SuggestedAnime // error handling for limit and offset - limitsErr := e.LimitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset, maxAnimeLimit) if limitsErr != nil { return suggestedAnime, limitsErr } diff --git a/errhandlers/errhandlers.go b/errhandlers/errhandlers.go index 50f4aac..a9f8054 100644 --- a/errhandlers/errhandlers.go +++ b/errhandlers/errhandlers.go @@ -41,10 +41,10 @@ func FieldsErrHandler(fields []string) ([]string, error) { } // if limit or error specified are above the limit -func LimitsErrHandler(limit, offset int) error { - maxOffset := 500 - limit - if limit > 500 { - return errors.New(fmt.Sprintf("InvalidLimitError: Limit specified too high (%d > 500).", limit)) +func LimitsErrHandler(limit, offset, maxLimit int) error { + maxOffset := maxLimit - limit + if limit > maxLimit { + return errors.New(fmt.Sprintf("InvalidLimitError: Limit specified too high (%d > %d).", limit, maxLimit)) } else if offset > maxOffset { return errors.New(fmt.Sprintf("InvalidOffsetError: Offset specified too high (%d > %d).", offset, maxOffset)) } diff --git a/errhandlers/validators.go b/errhandlers/validators.go index f9baea1..6bf7a3f 100644 --- a/errhandlers/validators.go +++ b/errhandlers/validators.go @@ -86,7 +86,8 @@ func IsValidSeason(season string) bool { } // Checks if given sort is valid -func IsValidSort(sort string) bool { +// For seasonal anime lists +func IsValidSeasonalSort(sort string) bool { switch sort { case "anime_score", @@ -94,3 +95,30 @@ func IsValidSort(sort string) bool { } return false } + +// Checks if given sort is valid +// for user anime lists +func IsValidListSort(sort string) bool { + switch sort { + case + "list_score", + "list_updated_at", + "anime_title", + "anime_start_date", + "anime_id": return true + } + return false +} + +// Checks if given anime list status is valid +func IsValidListStatus(status string) bool { + switch status { + case + "watching", + "completed", + "on_hold", + "dropped", + "plan_to_watch": return true + } + return false +} diff --git a/user/anime/animelist.go b/user/anime/animelist.go index e4cddc3..f25a2a2 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -18,18 +18,22 @@ package anime import ( "encoding/json" - "fmt" - "github.com/MikunoNaka/mal2go/anime" + "strconv" + "fmt" + "errors" + a "github.com/MikunoNaka/mal2go/anime" e "github.com/MikunoNaka/mal2go/errhandlers" + u "github.com/MikunoNaka/mal2go/util" ) const BASE_URL string = "https://api.myanimelist.net/v2" +const maxListLimit int = 1000 // Get authenticated user's anime list -func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset int, fields []string) (anime.AnimeList, error){ - var userAnimeList anime.AnimeList +func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset int, fields []string) (a.AnimeList, error){ + var userAnimeList a.AnimeList // error handling for limit and offset - limitsErr := e.LimitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset, maxListLimit) if limitsErr != nil { return userAnimeList, limitsErr } @@ -40,12 +44,27 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset i return userAnimeList, err } + // checks if valid sort is specified + if !e.IsValidListSort(sort) { + return userAnimeList, errors.New(fmt.Sprintf("GetAnimeList: Invalid sort specified: \"%s\"", sort)) + } + + // checks if valid status is specified + if !e.IsValidListStatus(status) { + return userAnimeList, errors.New(fmt.Sprintf("GetAnimeList: Invalid status specified: \"%s\"", status)) + } + // get own list if user not specified if user == "" { user = "@me" } - endpoint := BASE_URL + "/users/0ZeroTsu/animelist?fields=list_status&limit=4" + endpoint, _ := u.UrlGenerator( + BASE_URL + "/users/" + user + "/animelist", + []string{"status", "sort", "limit", "offset", "fields"}, + [][]string{{status}, {sort}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, + true, + ) // get data from API var animeListData AnimeListRaw @@ -53,7 +72,7 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset i json.Unmarshal([]byte(data), &animeListData) // set MyListStatus for each element and add it to array - var animes []anime.Anime + var animes []a.Anime for _, element := range animeListData.Data { a := element.Anime a.MyListStatus = element.ListStatus @@ -62,7 +81,7 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset i } // finally create AnimeList - userAnimeList = anime.AnimeList { + userAnimeList = a.AnimeList { Animes: animes, Paging: animeListData.Paging, } -- cgit v1.2.3