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 +++++++++++++++++++-- anime/anime.structs.go | 92 ++++++++++++++++++++++++++++++++++++++++++++++++ anime/ranking.structs.go | 31 ++++++++++++++++ anime/structs.go | 92 ------------------------------------------------ 4 files changed, 160 insertions(+), 95 deletions(-) create mode 100644 anime/anime.structs.go create mode 100644 anime/ranking.structs.go delete mode 100644 anime/structs.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 } diff --git a/anime/anime.structs.go b/anime/anime.structs.go new file mode 100644 index 0000000..bb35ded --- /dev/null +++ b/anime/anime.structs.go @@ -0,0 +1,92 @@ +package anime + +type AnimePicture struct { + Medium string `json:"large"` + Large string `json:"medium"` +} + +type StatusStatistics struct { + Watching int `json:"watching"` + Completed int `json:"completed"` + OnHold int `json:"on_hold"` + Dropped int `json:"dropped"` + PlanToWatch int `json:"plan_to_watch"` +} + +type AnimeStatistics struct { + Status StatusStatistics `json:"status"` + NumListUsers int `json:"num_list_users"` +} + +type Genre struct { + Id int `json:"id"` + Name string `json:"name"` +} + +type ListStatus struct { + Status string `json:"status"` + Score int `json:"score"` + EpWatched int `json:"num_episodes_watched"` + IsRewatching bool `json:"is_rewatching"` + UpdatedAt string `json:"updated_at"` +} + +type Season struct { + Year int `json:"year"` + Name string `json:"season"` +} + +type Broadcast struct { + Day string `json:"day_of_the_week"` + Time string `json:"start_time"` +} + +type Related struct { + Anime Anime `json:"node"` + RelationType string `json:"relation_type"` + RelationTypeFormatted string `json:"relation_type_formatted"` +} + +type Studio struct { + Id int `json:"id"` + Name string `json:"name"` +} + +type Recommendation struct { + Anime Anime `json:"node"` + Num int `json:"num_recommendations"` +} + +type Anime struct { + Id int `json:"id"` + Title string `json:"title"` + MainPicture AnimePicture `json:"main_picture"` + AltTitles []string `json:"alternative_titles"` + StartDate string `json:"start_date"` + EndDate string `json:"end_date"` + Synopsis string `json:"synopsis"` + MeanScore float32 `json:"mean"` + Rank int `json:"rank"` + 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 + CreatedAt string `json:"created_at"` + UpdatedAt string `json:"updated_at"` + MediaType string `json:"media_type"` + Status string `json:"status"` + Genres []Genre `json:"genres"` + MyListStatus ListStatus `json:"my_list_status"` + NumEpisodes int `json:"num_episodes"` + StartSeason Season `json:"start_season"` + Broadcast Broadcast `json:"broadcast"` + Source string `json:"source"` + DurationSeconds int `json:"average_episode_duration"` + Rating string `json:"rating"` + Pictures []AnimePicture `json:"pictures"` + Background string `json:"background"` + RelatedAnime []Related `json:"related_anime"` + Recommendations []Recommendation `json:"recommendations"` + Studios []Studio `json:"studios"` + Statistics AnimeStatistics `json:"statistics"` +} diff --git a/anime/ranking.structs.go b/anime/ranking.structs.go new file mode 100644 index 0000000..8b9b303 --- /dev/null +++ b/anime/ranking.structs.go @@ -0,0 +1,31 @@ +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/structs.go b/anime/structs.go deleted file mode 100644 index bb35ded..0000000 --- a/anime/structs.go +++ /dev/null @@ -1,92 +0,0 @@ -package anime - -type AnimePicture struct { - Medium string `json:"large"` - Large string `json:"medium"` -} - -type StatusStatistics struct { - Watching int `json:"watching"` - Completed int `json:"completed"` - OnHold int `json:"on_hold"` - Dropped int `json:"dropped"` - PlanToWatch int `json:"plan_to_watch"` -} - -type AnimeStatistics struct { - Status StatusStatistics `json:"status"` - NumListUsers int `json:"num_list_users"` -} - -type Genre struct { - Id int `json:"id"` - Name string `json:"name"` -} - -type ListStatus struct { - Status string `json:"status"` - Score int `json:"score"` - EpWatched int `json:"num_episodes_watched"` - IsRewatching bool `json:"is_rewatching"` - UpdatedAt string `json:"updated_at"` -} - -type Season struct { - Year int `json:"year"` - Name string `json:"season"` -} - -type Broadcast struct { - Day string `json:"day_of_the_week"` - Time string `json:"start_time"` -} - -type Related struct { - Anime Anime `json:"node"` - RelationType string `json:"relation_type"` - RelationTypeFormatted string `json:"relation_type_formatted"` -} - -type Studio struct { - Id int `json:"id"` - Name string `json:"name"` -} - -type Recommendation struct { - Anime Anime `json:"node"` - Num int `json:"num_recommendations"` -} - -type Anime struct { - Id int `json:"id"` - Title string `json:"title"` - MainPicture AnimePicture `json:"main_picture"` - AltTitles []string `json:"alternative_titles"` - StartDate string `json:"start_date"` - EndDate string `json:"end_date"` - Synopsis string `json:"synopsis"` - MeanScore float32 `json:"mean"` - Rank int `json:"rank"` - 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 - CreatedAt string `json:"created_at"` - UpdatedAt string `json:"updated_at"` - MediaType string `json:"media_type"` - Status string `json:"status"` - Genres []Genre `json:"genres"` - MyListStatus ListStatus `json:"my_list_status"` - NumEpisodes int `json:"num_episodes"` - StartSeason Season `json:"start_season"` - Broadcast Broadcast `json:"broadcast"` - Source string `json:"source"` - DurationSeconds int `json:"average_episode_duration"` - Rating string `json:"rating"` - Pictures []AnimePicture `json:"pictures"` - Background string `json:"background"` - RelatedAnime []Related `json:"related_anime"` - Recommendations []Recommendation `json:"recommendations"` - Studios []Studio `json:"studios"` - Statistics AnimeStatistics `json:"statistics"` -} -- cgit v1.2.3