From b876d3bea001718492fa289ebeba69d2989ddaf3 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 30 Jan 2022 16:13:36 +0530 Subject: Added error handling for getting ranking --- anime/anime.go | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'anime/anime.go') diff --git a/anime/anime.go b/anime/anime.go index 5d054ab..e9ef70f 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -8,9 +8,8 @@ import ( "net/http" ) -func GetAnimeById(token string, animeId int) Anime { +func requestHandler(token string, endpoint string) string { client := &http.Client{} - endpoint := fmt.Sprintf("https://api.myanimelist.net/v2/anime/%d?fields=id,title,main_picture,alternative_titles,start_date,end_date,synopsis,mean,rank,popularity,num_list_users,num_scoring_users,nsfw,created_at,updated_at,media_type,status,genres,my_list_status,num_episodes,start_season,broadcast,source,average_episode_duration,rating,pictures,background,related_anime,related_manga,recommendations,studios,statistics", animeId) // generate request req, err := http.NewRequest("GET", endpoint, nil) @@ -18,7 +17,6 @@ func GetAnimeById(token string, animeId int) Anime { log.Fatal(err) } req.Header.Add("Authorization", token) - // req.Header.Add("Content-Type", "application/json") // do request res, err := client.Do(req) @@ -27,14 +25,45 @@ func GetAnimeById(token string, animeId int) Anime { } defer res.Body.Close() + // read body body, err := ioutil.ReadAll(res.Body) if err != nil { log.Fatal(err) } - data := string(body) + return string(body) +} + +func GetAnimeById(token string, animeId int) Anime { + endpoint := fmt.Sprintf("https://api.myanimelist.net/v2/anime/%d?fields=id,title,main_picture,alternative_titles,start_date,end_date,synopsis,mean,rank,popularity,num_list_users,num_scoring_users,nsfw,created_at,updated_at,media_type,status,genres,my_list_status,num_episodes,start_season,broadcast,source,average_episode_duration,rating,pictures,background,related_anime,related_manga,recommendations,studios,statistics", animeId) + + data := requestHandler(token, endpoint) var anime Anime json.Unmarshal([]byte(data), &anime) return anime } + +// Checks if given rankingType is valid +func isValidRankingType(rankingType string) bool { + switch rankingType { + case + "all", + "airing", + "upcoming", + "tv", + "ova", + "movie", + "special", + "bypopularity", + "favorite": + return true + } + return false +} + +func GetAnimeRanking(token string, rankingType string) { + if !isValidRankingType(rankingType) { + log.Fatal(fmt.Sprintf("GetAnimeRanking: Invalid Ranking Type Given (\"%s\")", rankingType)) + } +} -- cgit v1.2.3 From 02752551dec484dd0e2b6f50158f516fd5d5c39d Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 31 Jan 2022 00:06:46 +0530 Subject: Completed Get Anime Ranking API route --- anime/anime.go | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'anime/anime.go') diff --git a/anime/anime.go b/anime/anime.go index e9ef70f..c397d93 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "log" "net/http" + "errors" ) func requestHandler(token string, endpoint string) string { @@ -34,11 +35,12 @@ func requestHandler(token string, endpoint string) string { return string(body) } +// Each anime has its own ID on MAL func GetAnimeById(token string, animeId int) Anime { endpoint := fmt.Sprintf("https://api.myanimelist.net/v2/anime/%d?fields=id,title,main_picture,alternative_titles,start_date,end_date,synopsis,mean,rank,popularity,num_list_users,num_scoring_users,nsfw,created_at,updated_at,media_type,status,genres,my_list_status,num_episodes,start_season,broadcast,source,average_episode_duration,rating,pictures,background,related_anime,related_manga,recommendations,studios,statistics", animeId) - data := requestHandler(token, endpoint) var anime Anime + data := requestHandler(token, endpoint) json.Unmarshal([]byte(data), &anime) return anime @@ -62,8 +64,40 @@ func isValidRankingType(rankingType string) bool { return false } -func GetAnimeRanking(token string, rankingType string) { +// Ranking is a list of anime sorted by their rank +func GetAnimeRanking(token string, rankingType string) (AnimeRanking, error) { + var animeRanking AnimeRanking if !isValidRankingType(rankingType) { - log.Fatal(fmt.Sprintf("GetAnimeRanking: Invalid Ranking Type Given (\"%s\")", rankingType)) + return animeRanking, errors.New(fmt.Sprintf("GetAnimeRanking: Invalid Ranking Type Given (\"%s\")", rankingType)) + } + + endpoint := "https://api.myanimelist.net/v2/anime/ranking?ranking_type=all&limit=4" + + // gets data from API and stores it in a struct + var rankingData RawRanking + data := requestHandler(token, endpoint) + json.Unmarshal([]byte(data), &rankingData) + + // Adding all the animes in ranking list to a slice + var animeRankingTitles []AnimeRankingTitle + for _, element := range rankingData.Data { + animeRankingTitles = append( + animeRankingTitles, + AnimeRankingTitle { + Anime: element.Anime, + RankNum: element.Ranking.Rank, + }, + ) } + + // Finally, create the AnimeRanking object + animeRanking = AnimeRanking { + Titles: animeRankingTitles, + Paging: ListPaging { + NextPage: rankingData.Paging.NextPage, + PrevPage: rankingData.Paging.PrevPage, + }, + } + + return animeRanking, nil } -- cgit v1.2.3 From a835f9b0b8b714a76d8b2f9c49b84f7042ddbd6a Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 31 Jan 2022 10:35:43 +0530 Subject: distributed code among multiple files for simplicity --- anime/anime.go | 47 ----------------------------------------------- 1 file changed, 47 deletions(-) (limited to 'anime/anime.go') diff --git a/anime/anime.go b/anime/anime.go index c397d93..7e5cf70 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -3,38 +3,9 @@ package anime import ( "encoding/json" "fmt" - "io/ioutil" - "log" - "net/http" "errors" ) -func requestHandler(token string, endpoint string) string { - client := &http.Client{} - - // generate request - req, err := http.NewRequest("GET", endpoint, nil) - if err != nil { - log.Fatal(err) - } - req.Header.Add("Authorization", token) - - // do request - res, err := client.Do(req) - if err != nil { - log.Fatal(err) - } - defer res.Body.Close() - - // read body - body, err := ioutil.ReadAll(res.Body) - if err != nil { - log.Fatal(err) - } - - return string(body) -} - // Each anime has its own ID on MAL func GetAnimeById(token string, animeId int) Anime { endpoint := fmt.Sprintf("https://api.myanimelist.net/v2/anime/%d?fields=id,title,main_picture,alternative_titles,start_date,end_date,synopsis,mean,rank,popularity,num_list_users,num_scoring_users,nsfw,created_at,updated_at,media_type,status,genres,my_list_status,num_episodes,start_season,broadcast,source,average_episode_duration,rating,pictures,background,related_anime,related_manga,recommendations,studios,statistics", animeId) @@ -46,24 +17,6 @@ func GetAnimeById(token string, animeId int) Anime { return anime } -// Checks if given rankingType is valid -func isValidRankingType(rankingType string) bool { - switch rankingType { - case - "all", - "airing", - "upcoming", - "tv", - "ova", - "movie", - "special", - "bypopularity", - "favorite": - return true - } - return false -} - // Ranking is a list of anime sorted by their rank func GetAnimeRanking(token string, rankingType string) (AnimeRanking, error) { var animeRanking AnimeRanking -- cgit v1.2.3 From 0dd65dd062362f913b3028e07e2f0c3afaec8894 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 31 Jan 2022 11:25:04 +0530 Subject: Added a URL Generator --- anime/anime.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'anime/anime.go') diff --git a/anime/anime.go b/anime/anime.go index 7e5cf70..2c23209 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -2,8 +2,9 @@ package anime import ( "encoding/json" + "errors" "fmt" - "errors" + "strconv" ) // Each anime has its own ID on MAL @@ -18,13 +19,18 @@ func GetAnimeById(token string, animeId int) Anime { } // Ranking is a list of anime sorted by their rank -func GetAnimeRanking(token string, rankingType string) (AnimeRanking, error) { +func GetAnimeRanking(token string, rankingType string, limit int) (AnimeRanking, error) { var animeRanking AnimeRanking if !isValidRankingType(rankingType) { return animeRanking, errors.New(fmt.Sprintf("GetAnimeRanking: Invalid Ranking Type Given (\"%s\")", rankingType)) } - endpoint := "https://api.myanimelist.net/v2/anime/ranking?ranking_type=all&limit=4" + endpoint, _ := urlGenerator( + "https://api.myanimelist.net/v2/anime/ranking", + []string{"ranking_type", "limit"}, + [][]string{{rankingType}, {strconv.Itoa(limit)}}, + true, + ) // gets data from API and stores it in a struct var rankingData RawRanking -- cgit v1.2.3 From c4c273888446b4beb215cafc165ecd9365bee357 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 31 Jan 2022 12:00:19 +0530 Subject: added error handling for GetAnimeById --- anime/anime.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) (limited to 'anime/anime.go') diff --git a/anime/anime.go b/anime/anime.go index 2c23209..bad3cf3 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -4,29 +4,69 @@ import ( "encoding/json" "errors" "fmt" + "log" "strconv" ) -// Each anime has its own ID on MAL -func GetAnimeById(token string, animeId int) Anime { - endpoint := fmt.Sprintf("https://api.myanimelist.net/v2/anime/%d?fields=id,title,main_picture,alternative_titles,start_date,end_date,synopsis,mean,rank,popularity,num_list_users,num_scoring_users,nsfw,created_at,updated_at,media_type,status,genres,my_list_status,num_episodes,start_season,broadcast,source,average_episode_duration,rating,pictures,background,related_anime,related_manga,recommendations,studios,statistics", animeId) +const BASE_URL string = "https://api.myanimelist.net/v2/anime" +// Each anime has its own ID on MAL +func GetAnimeById(token string, animeId int, fields []string) (Anime, error) { var anime Anime + + // Check if given fields are valid + for _, j := range(fields) { + if !isValidField(j) { + return anime, errors.New(fmt.Sprintf("GetAnimeById: Invalid field specified: \"%s\"", j)) + } + } + + // default fields to use when none are specified + defaultFields := []string{ + "id", "title", "main_picture", + "alternative_titles", "start_date", + "end_date", "synopsis", "mean", "rank", + "popularity", "num_list_users", + "num_scoring_users", "nsfw", "created_at", + "updated_at", "media_type", "status", + "genres", "my_list_status", "num_episodes", + "start_season", "broadcast", "source", + "average_episode_duration", "rating", + "pictures", "background", "related_anime", + "related_manga", "recommendations", + "studios", "statistics", + } + + if cap(fields) == 0 { + fields = defaultFields + log.Println("GetAnimeById: WARN: No fields specified, using all default fields to get data") + } + + endpoint, _ := urlGenerator( + BASE_URL + "/" + strconv.Itoa(animeId), + []string{"fields"}, + /* it seems to still return all fields from the API. + * this might be an issue with MAL itself + * TODO: look into this */ + [][]string{fields}, + true, + ) + data := requestHandler(token, endpoint) json.Unmarshal([]byte(data), &anime) - return anime + return anime, nil } // Ranking is a list of anime sorted by their rank func GetAnimeRanking(token string, rankingType string, limit int) (AnimeRanking, error) { var animeRanking AnimeRanking if !isValidRankingType(rankingType) { - return animeRanking, errors.New(fmt.Sprintf("GetAnimeRanking: Invalid Ranking Type Given (\"%s\")", rankingType)) + return animeRanking, errors.New(fmt.Sprintf("GetAnimeRanking: Invalid ranking type specified: \"%s\"", rankingType)) } endpoint, _ := urlGenerator( - "https://api.myanimelist.net/v2/anime/ranking", + BASE_URL + "/ranking", []string{"ranking_type", "limit"}, [][]string{{rankingType}, {strconv.Itoa(limit)}}, true, -- cgit v1.2.3 From caa17299f9f70addca805eb94a8174efcdda6985 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 31 Jan 2022 12:06:52 +0530 Subject: Adding copyright declarations to every file --- anime/anime.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'anime/anime.go') diff --git a/anime/anime.go b/anime/anime.go index bad3cf3..1439221 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -1,3 +1,19 @@ +/* mal2go - MyAnimeList V2 API wrapper for Go + * Copyright (C) 2022 Vidhu Kant Sharma + + * 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 . */ + package anime import ( -- cgit v1.2.3 From 97a02c2d9b309ae7e6ed2c0b807ed02913ded98c Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 31 Jan 2022 12:21:19 +0530 Subject: Added error handling for if limit is exceeded --- anime/anime.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'anime/anime.go') diff --git a/anime/anime.go b/anime/anime.go index 1439221..5d207ea 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "log" + "math" "strconv" ) @@ -75,16 +76,25 @@ func GetAnimeById(token string, animeId int, fields []string) (Anime, error) { } // Ranking is a list of anime sorted by their rank -func GetAnimeRanking(token string, rankingType string, limit int) (AnimeRanking, error) { +func GetAnimeRanking(token string, rankingType string, limit int, offset int) (AnimeRanking, error) { var animeRanking AnimeRanking + + // if limit exceeds what MAL supports + if limit > 500 { + return animeRanking, errors.New(fmt.Sprintf("GetAnimeRanking: Limit too high(%d). Max limit is 500", limit)) + } else if offset > 499 { + return animeRanking, errors.New(fmt.Sprintf("GetAnimeRanking: Offset too high(%d). Max offset for mal2go is 499", offset)) + } + + // if ranking type is invalid if !isValidRankingType(rankingType) { return animeRanking, errors.New(fmt.Sprintf("GetAnimeRanking: Invalid ranking type specified: \"%s\"", rankingType)) } endpoint, _ := urlGenerator( BASE_URL + "/ranking", - []string{"ranking_type", "limit"}, - [][]string{{rankingType}, {strconv.Itoa(limit)}}, + []string{"ranking_type", "limit", "offset"}, + [][]string{{rankingType}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}}, true, ) -- cgit v1.2.3 From f2d4bf9de9808f08f523ea2c741f385830960214 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 31 Jan 2022 22:07:21 +0530 Subject: Fixed Alternate Titles not loading --- anime/anime.go | 1 - 1 file changed, 1 deletion(-) (limited to 'anime/anime.go') diff --git a/anime/anime.go b/anime/anime.go index 5d207ea..34e996c 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -21,7 +21,6 @@ import ( "errors" "fmt" "log" - "math" "strconv" ) -- cgit v1.2.3 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 From f3ec24145da97fa7e4a5687503a6f46d59ff8c2a Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sat, 5 Feb 2022 21:52:30 +0530 Subject: added function to get seasonal anime and added fields support to all the endpoints that have it --- anime/anime.go | 128 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 85 insertions(+), 43 deletions(-) (limited to 'anime/anime.go') diff --git a/anime/anime.go b/anime/anime.go index e05b674..6c65138 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -27,25 +27,30 @@ 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) { +func SearchAnime(token, searchString string, limit, offset int, fields []string) (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)) + // error handling for limit and offset + limitsErr := limitsErrHandler(limit, offset) + if limitsErr != nil { + log.Println(limitsErr) + } + + // handle all the errors for the fields + fields, err := fieldsErrHandler(fields) + if err != nil { + log.Println(err) } // generate endpoint url with custom params endpoint, _ := urlGenerator( BASE_URL, - []string{"q", "limit", "offset"}, - [][]string{{searchString}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}}, + []string{"q", "limit", "offset", "fields"}, + [][]string{{searchString}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, true, ) + // gets data from API and stores it in a struct var animeSearchData AnimeSearchRaw data := requestHandler(token, endpoint) json.Unmarshal([]byte(data), &animeSearchData) @@ -69,32 +74,10 @@ func SearchAnime(token, searchString string, limit, offset int) (AnimeSearch, er func GetAnimeById(token string, animeId int, fields []string) (Anime, error) { var anime Anime - // Check if given fields are valid - for _, j := range(fields) { - if !isValidField(j) { - return anime, errors.New(fmt.Sprintf("GetAnimeById: Invalid field specified: \"%s\"", j)) - } - } - - // default fields to use when none are specified - defaultFields := []string{ - "id", "title", "main_picture", - "alternative_titles", "start_date", - "end_date", "synopsis", "mean", "rank", - "popularity", "num_list_users", - "num_scoring_users", "nsfw", "created_at", - "updated_at", "media_type", "status", - "genres", "my_list_status", "num_episodes", - "start_season", "broadcast", "source", - "average_episode_duration", "rating", - "pictures", "background", "related_anime", - "related_manga", "recommendations", - "studios", "statistics", - } - - if cap(fields) == 0 { - fields = defaultFields - log.Println("GetAnimeById: WARN: No fields specified, using all default fields to get data") + // handle all the errors for the fields + fields, err := fieldsErrHandler(fields) + if err != nil { + log.Println(err) } endpoint, _ := urlGenerator( @@ -114,14 +97,19 @@ func GetAnimeById(token string, animeId int, fields []string) (Anime, error) { } // Ranking is a list of anime sorted by their rank -func GetAnimeRanking(token string, rankingType string, limit int, offset int) (AnimeRanking, error) { +func GetAnimeRanking(token string, rankingType string, limit, offset int, fields []string) (AnimeRanking, error) { var animeRanking AnimeRanking - // if limit exceeds what MAL supports - if limit > 500 { - return animeRanking, errors.New(fmt.Sprintf("GetAnimeRanking: Limit too high(%d). Max limit is 500", limit)) - } else if offset > 499 { - return animeRanking, errors.New(fmt.Sprintf("GetAnimeRanking: Offset too high(%d). Max offset for mal2go is 499", offset)) + // error handling for limit and offset + limitsErr := limitsErrHandler(limit, offset) + if limitsErr != nil { + log.Println(limitsErr) + } + + // handle all the errors for the fields + fields, err := fieldsErrHandler(fields) + if err != nil { + log.Println(err) } // if ranking type is invalid @@ -131,8 +119,8 @@ func GetAnimeRanking(token string, rankingType string, limit int, offset int) (A endpoint, _ := urlGenerator( BASE_URL + "/ranking", - []string{"ranking_type", "limit", "offset"}, - [][]string{{rankingType}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}}, + []string{"ranking_type", "limit", "offset", "fields"}, + [][]string{{rankingType}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, true, ) @@ -164,3 +152,57 @@ func GetAnimeRanking(token string, rankingType string, limit int, offset int) (A return animeRanking, nil } + +// get list of animes from specified season +func GetSeasonalAnime(token, year, season, sort string, limit, offset int, fields []string) (SeasonalAnime, error) { + var seasonalAnime SeasonalAnime + + // error handling for limit and offset + limitsErr := limitsErrHandler(limit, offset) + if limitsErr != nil { + log.Println(limitsErr) + } + + // handle all the errors for the fields + fields, err := fieldsErrHandler(fields) + if err != nil { + log.Println(err) + } + + // checks if valid season is specified + if !isValidSeason(season) { + return seasonalAnime, errors.New(fmt.Sprintf("GetSeasonalAnime: Invalid season specified: \"%s\"", season)) + } + + // checks if valid sort is specified + if !isValidSort(sort) { + return seasonalAnime, errors.New(fmt.Sprintf("GetSeasonalAnime: Invalid sort specified: \"%s\"", sort)) + } + + endpoint, _ := urlGenerator( + BASE_URL + fmt.Sprintf("/season/%s/%s", year, season), + []string{"sort", "limit", "offset", "fields"}, + [][]string{{sort}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, + true, + ) + + // gets data from API and stores it in a struct + var seasonalAnimeData SeasonalAnimeRaw + data := requestHandler(token, endpoint) + json.Unmarshal([]byte(data), &seasonalAnimeData) + + // Adding all the animes to another list to get formatted results later + var animes []Anime + for _, element := range seasonalAnimeData.Data { + animes = append(animes, element.Anime) + } + + // finally generate SeasonalAnime + seasonalAnime = SeasonalAnime { + Animes: animes, + Paging: seasonalAnimeData.Paging, + Season: seasonalAnimeData.Season, + } + + return seasonalAnime, nil +} -- cgit v1.2.3 From e550d3d71360c4da68c7f9e978ef45f05e68b216 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sat, 5 Feb 2022 22:15:48 +0530 Subject: implemented function for get suggested anime endpoint --- anime/anime.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 8 deletions(-) (limited to 'anime/anime.go') diff --git a/anime/anime.go b/anime/anime.go index 6c65138..3344016 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -20,7 +20,6 @@ import ( "encoding/json" "errors" "fmt" - "log" "strconv" ) @@ -33,13 +32,13 @@ func SearchAnime(token, searchString string, limit, offset int, fields []string) // error handling for limit and offset limitsErr := limitsErrHandler(limit, offset) if limitsErr != nil { - log.Println(limitsErr) + return searchResults, limitsErr } // handle all the errors for the fields fields, err := fieldsErrHandler(fields) if err != nil { - log.Println(err) + return searchResults, err } // generate endpoint url with custom params @@ -77,7 +76,7 @@ func GetAnimeById(token string, animeId int, fields []string) (Anime, error) { // handle all the errors for the fields fields, err := fieldsErrHandler(fields) if err != nil { - log.Println(err) + return anime, err } endpoint, _ := urlGenerator( @@ -103,13 +102,13 @@ func GetAnimeRanking(token string, rankingType string, limit, offset int, fields // error handling for limit and offset limitsErr := limitsErrHandler(limit, offset) if limitsErr != nil { - log.Println(limitsErr) + return animeRanking, limitsErr } // handle all the errors for the fields fields, err := fieldsErrHandler(fields) if err != nil { - log.Println(err) + return animeRanking, err } // if ranking type is invalid @@ -160,13 +159,13 @@ func GetSeasonalAnime(token, year, season, sort string, limit, offset int, field // error handling for limit and offset limitsErr := limitsErrHandler(limit, offset) if limitsErr != nil { - log.Println(limitsErr) + return seasonalAnime, limitsErr } // handle all the errors for the fields fields, err := fieldsErrHandler(fields) if err != nil { - log.Println(err) + return seasonalAnime, err } // checks if valid season is specified @@ -206,3 +205,46 @@ func GetSeasonalAnime(token, year, season, sort string, limit, offset int, field return seasonalAnime, nil } + +// get anime suggestions for the user +func GetSuggestedAnime(token string, limit, offset int, fields []string) (SuggestedAnime, error){ + var suggestedAnime SuggestedAnime + + // error handling for limit and offset + limitsErr := limitsErrHandler(limit, offset) + if limitsErr != nil { + return suggestedAnime, limitsErr + } + + // handle all the errors for the fields + fields, err := fieldsErrHandler(fields) + if err != nil { + return suggestedAnime, err + } + + endpoint, _ := urlGenerator( + BASE_URL + "/suggestions", + []string{"limit", "offset", "fields"}, + [][]string{{strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, + true, + ) + + // gets data from API and stores it in a struct + var suggestedAnimeData SuggestedAnimeRaw + data := requestHandler(token, endpoint) + json.Unmarshal([]byte(data), &suggestedAnimeData) + + // Adding all the animes to another list to get formatted results later + var animes []Anime + for _, element := range suggestedAnimeData.Data { + animes = append(animes, element.Anime) + } + + // finally generate RecommendedAnime struct + suggestedAnime = SuggestedAnime { + Animes: animes, + Paging: suggestedAnimeData.Paging, + } + + return suggestedAnime, nil +} -- cgit v1.2.3