From 7816a8487dd5d79111ca84b0da227faf5f8d7bad Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 6 Feb 2022 19:27:02 +0530 Subject: implemented basic functionality to get user's animelist in string form --- user/anime/animelist.go | 32 +++++++++++++++++++ user/anime/client.go | 27 ++++++++++++++++ user/anime/util.go | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 user/anime/animelist.go create mode 100644 user/anime/client.go create mode 100644 user/anime/util.go diff --git a/user/anime/animelist.go b/user/anime/animelist.go new file mode 100644 index 0000000..c48980d --- /dev/null +++ b/user/anime/animelist.go @@ -0,0 +1,32 @@ +/* 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 ( + "fmt" +) + +const BASE_URL string = "https://api.myanimelist.net/v2" + +// Get authenticated user's anime list +func (c AnimeListClient) GetAnimeList() { + endpoint := BASE_URL + "/users/@me/animelist?fields=list_status&limit=4" + + data := c.requestHandler(endpoint, "GET") + fmt.Println(data) +} + diff --git a/user/anime/client.go b/user/anime/client.go new file mode 100644 index 0000000..e9a99d3 --- /dev/null +++ b/user/anime/client.go @@ -0,0 +1,27 @@ +/* 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 ( + "net/http" +) + +// MyAnimeList Client for mal2go/anime package +type AnimeListClient struct { + AuthToken, RefreshToken string + HttpClient http.Client +} diff --git a/user/anime/util.go b/user/anime/util.go new file mode 100644 index 0000000..d9a4a4c --- /dev/null +++ b/user/anime/util.go @@ -0,0 +1,85 @@ +/* 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 ( + "io/ioutil" + "log" + "net/http" +) + +// Handles HTTP request with your OAuth token as a Header +func (c AnimeListClient) requestHandler(endpoint, method string) string { + // generate request + req, err := http.NewRequest(method, endpoint, nil) + if err != nil { + log.Fatal(err) + } + req.Header.Add("Authorization", c.AuthToken) + + // do request + res, err := c.HttpClient.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) +} + +// func urlGenerator(baseUrl string, names []string, values [][]string, isPrimary bool) (string, error) { +// // length of names and values should be same +// if cap(names) != cap(values) { +// return "", errors.New("urlGenerator: Error: Length of names and values don't match.") +// } +// +// var fields string +// +// for index, name := range(names) { +// var data string +// /* if the data is the first field in URL, +// * it goes like ?key=value +// * else it is &nextkey=value */ +// if isPrimary { +// data = "?" + name + "=" +// } else { +// data = "&" + name + "=" +// } +// +// // add values to data variable +// for i, j := range values[index] { +// if i > 0 { +// data = data + "," + j +// } else { +// data = data + j +// } +// } +// +// fields = fields + data +// +// // from now on all other fields will be secondary +// isPrimary = false +// } +// +// return baseUrl + fields, nil +// } -- cgit v1.2.3 From 4bd702d111c6e4d5455865a7e1fbe5de11899b15 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Tue, 8 Feb 2022 21:47:58 +0530 Subject: NOT WORKING: pushing current progress, adding support for different options with GetAnimeList --- anime/errhandlers.go | 21 +-------------------- anime/general.structs.go | 24 ++++++++++++++++++++++++ user/anime/animelist.go | 35 +++++++++++++++++++++++++++++++---- user/anime/animelist.structs.go | 29 +++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 24 deletions(-) create mode 100644 user/anime/animelist.structs.go diff --git a/anime/errhandlers.go b/anime/errhandlers.go index d7f70f9..3a60f7c 100644 --- a/anime/errhandlers.go +++ b/anime/errhandlers.go @@ -21,30 +21,11 @@ import ( "fmt" ) -/* NOTE: MAL still seems to send some fields - * even if they aren't requested. - * those include Title, Picture, Id, etc */ -// default fields to use when none are specified -var defaultFields []string = []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 fields aren't specified func fieldsErrHandler(fields []string) ([]string, error) { if cap(fields) == 0 { // uses all the default fields if none specified - return defaultFields, nil + return DefaultFields, nil } // checks if each given field is valid diff --git a/anime/general.structs.go b/anime/general.structs.go index c49b762..77bd22a 100644 --- a/anime/general.structs.go +++ b/anime/general.structs.go @@ -16,6 +16,25 @@ package anime +/* NOTE: MAL still seems to send some fields + * even if they aren't requested. + * those include Title, Picture, Id, etc */ +// default fields to use when none are specified +var DefaultFields []string = []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", +} + // contains previous/next page for anime list type ListPaging struct { NextPage string `json:"next"` @@ -26,3 +45,8 @@ type Season struct { Year int `json:"year"` Name string `json:"season"` } + +type AnimeList struct { + Animes []Anime + Paging ListPaging +} diff --git a/user/anime/animelist.go b/user/anime/animelist.go index c48980d..b36b50d 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -17,16 +17,43 @@ package anime import ( - "fmt" + "encoding/json" + "fmt" + "github.com/MikunoNaka/mal2go/anime" ) const BASE_URL string = "https://api.myanimelist.net/v2" // Get authenticated user's anime list -func (c AnimeListClient) GetAnimeList() { - endpoint := BASE_URL + "/users/@me/animelist?fields=list_status&limit=4" +func (c AnimeListClient) GetAnimeList(user, status, sort string/*, limit, offset int*/) { + // get own list if user not specified + if user == "" { + user = "@me" + } + var userAnimeList anime.AnimeList + endpoint := BASE_URL + "/users/0ZeroTsu/animelist?fields=list_status&limit=4" + + // get data from API + var animeListData AnimeListRaw data := c.requestHandler(endpoint, "GET") - fmt.Println(data) + json.Unmarshal([]byte(data), &animeListData) + + // set MyListStatus for each element and add it to array + var animes []anime.Anime + for _, element := range animeListData.Data { + a := element.Anime + a.MyListStatus = element.ListStatus + + animes = append(animes, a) + } + + // finally create AnimeList + userAnimeList = anime.AnimeList { + Animes: animes, + Paging: animeListData.Paging, + } + + fmt.Println(userAnimeList) } diff --git a/user/anime/animelist.structs.go b/user/anime/animelist.structs.go new file mode 100644 index 0000000..d43b47b --- /dev/null +++ b/user/anime/animelist.structs.go @@ -0,0 +1,29 @@ +/* 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 ( + "github.com/MikunoNaka/mal2go/anime" +) + +type AnimeListRaw struct { + Data []struct { + Anime anime.Anime `json:"node"` + ListStatus anime.ListStatus `json:"list_status"` + } `json:"data"` + Paging anime.ListPaging `json:"paging"` +} -- cgit v1.2.3 From 1d3f72c1b48998b86fd1740e893559b6dcaf7663 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 13 Feb 2022 13:42:06 +0530 Subject: modularised the code for easy access of various functions in packages --- anime/anime.go | 36 ++++++++-------- anime/errhandlers.go | 52 ----------------------- anime/general.structs.go | 19 --------- anime/request_handler.go | 49 ++++++++++++++++++++++ anime/util.go | 87 --------------------------------------- anime/validators.go | 96 ------------------------------------------- errhandlers/errhandlers.go | 53 ++++++++++++++++++++++++ errhandlers/validators.go | 96 +++++++++++++++++++++++++++++++++++++++++++ user/anime/animelist.go | 19 +++++++-- user/anime/request_handler.go | 48 ++++++++++++++++++++++ user/anime/util.go | 85 -------------------------------------- util/structs.go | 36 ++++++++++++++++ util/url_generator.go | 58 ++++++++++++++++++++++++++ 13 files changed, 375 insertions(+), 359 deletions(-) delete mode 100644 anime/errhandlers.go create mode 100644 anime/request_handler.go delete mode 100644 anime/util.go delete mode 100644 anime/validators.go create mode 100644 errhandlers/errhandlers.go create mode 100644 errhandlers/validators.go create mode 100644 user/anime/request_handler.go delete mode 100644 user/anime/util.go create mode 100644 util/structs.go create mode 100644 util/url_generator.go diff --git a/anime/anime.go b/anime/anime.go index 90231e3..323105b 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -21,6 +21,8 @@ import ( "errors" "fmt" "strconv" + e "github.com/MikunoNaka/mal2go/errhandlers" + u "github.com/MikunoNaka/mal2go/util" ) const BASE_URL string = "https://api.myanimelist.net/v2/anime" @@ -30,19 +32,19 @@ func (c AnimeClient) SearchAnime(searchString string, limit, offset int, fields var searchResults AnimeSearch // error handling for limit and offset - limitsErr := limitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset) if limitsErr != nil { return searchResults, limitsErr } // handle all the errors for the fields - fields, err := fieldsErrHandler(fields) + fields, err := e.FieldsErrHandler(fields) if err != nil { return searchResults, err } // generate endpoint url with custom params - endpoint, _ := urlGenerator( + endpoint, _ := u.UrlGenerator( BASE_URL, []string{"q", "limit", "offset", "fields"}, [][]string{{searchString}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, @@ -74,12 +76,12 @@ func (c AnimeClient) GetAnimeById(animeId int, fields []string) (Anime, error) { var anime Anime // handle all the errors for the fields - fields, err := fieldsErrHandler(fields) + fields, err := e.FieldsErrHandler(fields) if err != nil { return anime, err } - endpoint, _ := urlGenerator( + endpoint, _ := u.UrlGenerator( BASE_URL + "/" + strconv.Itoa(animeId), []string{"fields"}, /* it seems to still return all fields from the API. @@ -100,23 +102,23 @@ func (c AnimeClient) GetAnimeRanking(rankingType string, limit, offset int, fiel var animeRanking AnimeRanking // error handling for limit and offset - limitsErr := limitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset) if limitsErr != nil { return animeRanking, limitsErr } // handle all the errors for the fields - fields, err := fieldsErrHandler(fields) + fields, err := e.FieldsErrHandler(fields) if err != nil { return animeRanking, err } // if ranking type is invalid - if !isValidRankingType(rankingType) { + if !e.IsValidRankingType(rankingType) { return animeRanking, errors.New(fmt.Sprintf("GetAnimeRanking: Invalid ranking type specified: \"%s\"", rankingType)) } - endpoint, _ := urlGenerator( + endpoint, _ := u.UrlGenerator( BASE_URL + "/ranking", []string{"ranking_type", "limit", "offset", "fields"}, [][]string{{rankingType}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, @@ -157,28 +159,28 @@ func (c AnimeClient) GetSeasonalAnime(year, season, sort string, limit, offset i var seasonalAnime SeasonalAnime // error handling for limit and offset - limitsErr := limitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset) if limitsErr != nil { return seasonalAnime, limitsErr } // handle all the errors for the fields - fields, err := fieldsErrHandler(fields) + fields, err := e.FieldsErrHandler(fields) if err != nil { return seasonalAnime, err } // checks if valid season is specified - if !isValidSeason(season) { + if !e.IsValidSeason(season) { return seasonalAnime, errors.New(fmt.Sprintf("GetSeasonalAnime: Invalid season specified: \"%s\"", season)) } // checks if valid sort is specified - if !isValidSort(sort) { + if !e.IsValidSort(sort) { return seasonalAnime, errors.New(fmt.Sprintf("GetSeasonalAnime: Invalid sort specified: \"%s\"", sort)) } - endpoint, _ := urlGenerator( + endpoint, _ := u.UrlGenerator( BASE_URL + fmt.Sprintf("/season/%s/%s", year, season), []string{"sort", "limit", "offset", "fields"}, [][]string{{sort}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, @@ -211,18 +213,18 @@ func (c AnimeClient) GetSuggestedAnime(limit, offset int, fields []string) (Sugg var suggestedAnime SuggestedAnime // error handling for limit and offset - limitsErr := limitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset) if limitsErr != nil { return suggestedAnime, limitsErr } // handle all the errors for the fields - fields, err := fieldsErrHandler(fields) + fields, err := e.FieldsErrHandler(fields) if err != nil { return suggestedAnime, err } - endpoint, _ := urlGenerator( + endpoint, _ := u.UrlGenerator( BASE_URL + "/suggestions", []string{"limit", "offset", "fields"}, [][]string{{strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, diff --git a/anime/errhandlers.go b/anime/errhandlers.go deleted file mode 100644 index 3a60f7c..0000000 --- a/anime/errhandlers.go +++ /dev/null @@ -1,52 +0,0 @@ -/* 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 ( - "errors" - "fmt" -) - -// if fields aren't specified -func fieldsErrHandler(fields []string) ([]string, error) { - if cap(fields) == 0 { - // uses all the default fields if none specified - return DefaultFields, nil - } - - // checks if each given field is valid - for _, j := range(fields) { - if !isValidField(j) { - return []string{}, errors.New(fmt.Sprintf("InvalidFieldError: Invalid field specified: \"%s\"", j)) - } - } - - // everything's fine! - return fields, nil -} - -// 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)) - } else if offset > maxOffset { - return errors.New(fmt.Sprintf("InvalidOffsetError: Offset specified too high (%d > %d).", offset, maxOffset)) - } - // return nil if no error - return nil -} diff --git a/anime/general.structs.go b/anime/general.structs.go index 77bd22a..5df1357 100644 --- a/anime/general.structs.go +++ b/anime/general.structs.go @@ -16,25 +16,6 @@ package anime -/* NOTE: MAL still seems to send some fields - * even if they aren't requested. - * those include Title, Picture, Id, etc */ -// default fields to use when none are specified -var DefaultFields []string = []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", -} - // contains previous/next page for anime list type ListPaging struct { NextPage string `json:"next"` diff --git a/anime/request_handler.go b/anime/request_handler.go new file mode 100644 index 0000000..809ca07 --- /dev/null +++ b/anime/request_handler.go @@ -0,0 +1,49 @@ +/* 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 ( + "io/ioutil" + "log" + "net/http" +) + +// Handles HTTP request with your OAuth token as a Header +// TODO: Verify that this function is safe to use +func (c AnimeClient) requestHandler(endpoint string) string { + // generate request + req, err := http.NewRequest("GET", endpoint, nil) + if err != nil { + log.Fatal(err) + } + req.Header.Add("Authorization", c.AuthToken) + + // do request + res, err := c.HttpClient.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) +} diff --git a/anime/util.go b/anime/util.go deleted file mode 100644 index 862b54d..0000000 --- a/anime/util.go +++ /dev/null @@ -1,87 +0,0 @@ -/* 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 ( - "io/ioutil" - "log" - "net/http" - "errors" -) - -// Handles HTTP request with your OAuth token as a Header -// TODO: Verify that this function is safe to use -func (c AnimeClient) requestHandler(endpoint string) string { - // generate request - req, err := http.NewRequest("GET", endpoint, nil) - if err != nil { - log.Fatal(err) - } - req.Header.Add("Authorization", c.AuthToken) - - // do request - res, err := c.HttpClient.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) -} - -func urlGenerator(baseUrl string, names []string, values [][]string, isPrimary bool) (string, error) { - // length of names and values should be same - if cap(names) != cap(values) { - return "", errors.New("urlGenerator: Error: Length of names and values don't match.") - } - - var fields string - - for index, name := range(names) { - var data string - /* if the data is the first field in URL, - * it goes like ?key=value - * else it is &nextkey=value */ - if isPrimary { - data = "?" + name + "=" - } else { - data = "&" + name + "=" - } - - // add values to data variable - for i, j := range values[index] { - if i > 0 { - data = data + "," + j - } else { - data = data + j - } - } - - fields = fields + data - - // from now on all other fields will be secondary - isPrimary = false - } - - return baseUrl + fields, nil -} diff --git a/anime/validators.go b/anime/validators.go deleted file mode 100644 index 7f6a7cc..0000000 --- a/anime/validators.go +++ /dev/null @@ -1,96 +0,0 @@ -/* 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 - -// 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 -} - -// Checks if given rankingType is valid -func isValidField(field string) bool { - switch field { - case - "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": return true - } - return false -} - -// Checks if given season is valid -func isValidSeason(season string) bool { - switch season { - case - "winter", - "spring", - "summer", - "fall": return true - } - return false -} - -// Checks if given sort is valid -func isValidSort(sort string) bool { - switch sort { - case - "anime_score", - "anime_num_list_users": return true - } - return false -} diff --git a/errhandlers/errhandlers.go b/errhandlers/errhandlers.go new file mode 100644 index 0000000..50f4aac --- /dev/null +++ b/errhandlers/errhandlers.go @@ -0,0 +1,53 @@ +/* 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 errhandlers + +import ( + "errors" + "fmt" + "github.com/MikunoNaka/mal2go/util" +) + +// if fields aren't specified +func FieldsErrHandler(fields []string) ([]string, error) { + if cap(fields) == 0 { + // uses all the default fields if none specified + return util.DefaultFields, nil + } + + // checks if each given field is valid + for _, j := range(fields) { + if !IsValidField(j) { + return []string{}, errors.New(fmt.Sprintf("InvalidFieldError: Invalid field specified: \"%s\"", j)) + } + } + + // everything's fine! + return fields, nil +} + +// 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)) + } else if offset > maxOffset { + return errors.New(fmt.Sprintf("InvalidOffsetError: Offset specified too high (%d > %d).", offset, maxOffset)) + } + // return nil if no error + return nil +} diff --git a/errhandlers/validators.go b/errhandlers/validators.go new file mode 100644 index 0000000..f9baea1 --- /dev/null +++ b/errhandlers/validators.go @@ -0,0 +1,96 @@ +/* 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 errhandlers + +// 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 +} + +// Checks if given rankingType is valid +func IsValidField(field string) bool { + switch field { + case + "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": return true + } + return false +} + +// Checks if given season is valid +func IsValidSeason(season string) bool { + switch season { + case + "winter", + "spring", + "summer", + "fall": return true + } + return false +} + +// Checks if given sort is valid +func IsValidSort(sort string) bool { + switch sort { + case + "anime_score", + "anime_num_list_users": return true + } + return false +} diff --git a/user/anime/animelist.go b/user/anime/animelist.go index b36b50d..e4cddc3 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -20,18 +20,31 @@ import ( "encoding/json" "fmt" "github.com/MikunoNaka/mal2go/anime" + e "github.com/MikunoNaka/mal2go/errhandlers" ) const BASE_URL string = "https://api.myanimelist.net/v2" // Get authenticated user's anime list -func (c AnimeListClient) GetAnimeList(user, status, sort string/*, limit, offset int*/) { +func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset int, fields []string) (anime.AnimeList, error){ + var userAnimeList anime.AnimeList + // error handling for limit and offset + limitsErr := e.LimitsErrHandler(limit, offset) + if limitsErr != nil { + return userAnimeList, limitsErr + } + + // handle all the errors for the fields + fields, err := e.FieldsErrHandler(fields) + if err != nil { + return userAnimeList, err + } + // get own list if user not specified if user == "" { user = "@me" } - var userAnimeList anime.AnimeList endpoint := BASE_URL + "/users/0ZeroTsu/animelist?fields=list_status&limit=4" // get data from API @@ -54,6 +67,6 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string/*, limit, offset Paging: animeListData.Paging, } - fmt.Println(userAnimeList) + return userAnimeList, nil } diff --git a/user/anime/request_handler.go b/user/anime/request_handler.go new file mode 100644 index 0000000..f424c29 --- /dev/null +++ b/user/anime/request_handler.go @@ -0,0 +1,48 @@ +/* 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 ( + "io/ioutil" + "log" + "net/http" +) + +// Handles HTTP request with your OAuth token as a Header +func (c AnimeListClient) requestHandler(endpoint, method string) string { + // generate request + req, err := http.NewRequest(method, endpoint, nil) + if err != nil { + log.Fatal(err) + } + req.Header.Add("Authorization", c.AuthToken) + + // do request + res, err := c.HttpClient.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) +} diff --git a/user/anime/util.go b/user/anime/util.go deleted file mode 100644 index d9a4a4c..0000000 --- a/user/anime/util.go +++ /dev/null @@ -1,85 +0,0 @@ -/* 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 ( - "io/ioutil" - "log" - "net/http" -) - -// Handles HTTP request with your OAuth token as a Header -func (c AnimeListClient) requestHandler(endpoint, method string) string { - // generate request - req, err := http.NewRequest(method, endpoint, nil) - if err != nil { - log.Fatal(err) - } - req.Header.Add("Authorization", c.AuthToken) - - // do request - res, err := c.HttpClient.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) -} - -// func urlGenerator(baseUrl string, names []string, values [][]string, isPrimary bool) (string, error) { -// // length of names and values should be same -// if cap(names) != cap(values) { -// return "", errors.New("urlGenerator: Error: Length of names and values don't match.") -// } -// -// var fields string -// -// for index, name := range(names) { -// var data string -// /* if the data is the first field in URL, -// * it goes like ?key=value -// * else it is &nextkey=value */ -// if isPrimary { -// data = "?" + name + "=" -// } else { -// data = "&" + name + "=" -// } -// -// // add values to data variable -// for i, j := range values[index] { -// if i > 0 { -// data = data + "," + j -// } else { -// data = data + j -// } -// } -// -// fields = fields + data -// -// // from now on all other fields will be secondary -// isPrimary = false -// } -// -// return baseUrl + fields, nil -// } diff --git a/util/structs.go b/util/structs.go new file mode 100644 index 0000000..d242e7f --- /dev/null +++ b/util/structs.go @@ -0,0 +1,36 @@ +/* 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 util + +/* NOTE: MAL still seems to send some fields + * even if they aren't requested. + * those include Title, Picture, Id, etc */ +// default fields to use when none are specified +var DefaultFields []string = []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", +} diff --git a/util/url_generator.go b/util/url_generator.go new file mode 100644 index 0000000..3d21a98 --- /dev/null +++ b/util/url_generator.go @@ -0,0 +1,58 @@ +/* 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 util + +import ( + "errors" +) + +func UrlGenerator(baseUrl string, names []string, values [][]string, isPrimary bool) (string, error) { + // length of names and values should be same + if cap(names) != cap(values) { + return "", errors.New("util.UrlGenerator: Error: Length of names and values don't match.") + } + + var fields string + + for index, name := range(names) { + var data string + /* if the data is the first field in URL, + * it goes like ?key=value + * else it is &nextkey=value */ + if isPrimary { + data = "?" + name + "=" + } else { + data = "&" + name + "=" + } + + // add values to data variable + for i, j := range values[index] { + if i > 0 { + data = data + "," + j + } else { + data = data + j + } + } + + fields = fields + data + + // from now on all other fields will be secondary + isPrimary = false + } + + return baseUrl + fields, nil +} -- cgit v1.2.3 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 From 7dcda8a7344ec5be2b93672ace638515708554de Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 13 Feb 2022 15:30:32 +0530 Subject: removed fields from GetAnimeList because endpoint does not support that --- user/anime/animelist.go | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/user/anime/animelist.go b/user/anime/animelist.go index f25a2a2..2099204 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -23,14 +23,13 @@ import ( "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) (a.AnimeList, error){ +func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset int) (a.AnimeList, error){ var userAnimeList a.AnimeList // error handling for limit and offset limitsErr := e.LimitsErrHandler(limit, offset, maxListLimit) @@ -38,12 +37,6 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset i return userAnimeList, limitsErr } - // handle all the errors for the fields - fields, err := e.FieldsErrHandler(fields) - if err != nil { - 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)) @@ -59,12 +52,7 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset i user = "@me" } - 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, - ) + endpoint := BASE_URL + "/users/" + user + "/animelist?status=" + status + "&sort=" + sort + "&limit=" + strconv.Itoa(limit) + "&offset=" + strconv.Itoa(offset) // get data from API var animeListData AnimeListRaw -- cgit v1.2.3 From 032469fc1c43fb76fdf8882371391672dcfb87f0 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 13 Feb 2022 16:05:15 +0530 Subject: added function to delete anime from MAL List --- user/anime/animelist.go | 8 ++++++++ user/anime/request_handler.go | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/user/anime/animelist.go b/user/anime/animelist.go index 2099204..7fe81b8 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -28,6 +28,14 @@ import ( const BASE_URL string = "https://api.myanimelist.net/v2" const maxListLimit int = 1000 +// Delete an anime from user's anime list +func (c AnimeListClient)DeleteAnime(id int) string { + endpoint := fmt.Sprintf("%s/anime/%d/my_list_status", BASE_URL, id) + /* Returns 200 if anime successfully deleted + * Alternatively returns 404 if anime not in user's anime list */ + return c.requestHandler(endpoint, "DELETE") +} + // Get authenticated user's anime list func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset int) (a.AnimeList, error){ var userAnimeList a.AnimeList diff --git a/user/anime/request_handler.go b/user/anime/request_handler.go index f424c29..2007e56 100644 --- a/user/anime/request_handler.go +++ b/user/anime/request_handler.go @@ -20,6 +20,7 @@ import ( "io/ioutil" "log" "net/http" + "strconv" ) // Handles HTTP request with your OAuth token as a Header @@ -44,5 +45,10 @@ func (c AnimeListClient) requestHandler(endpoint, method string) string { log.Fatal(err) } + // for DeleteAnime, its endpoint returns null data + if method == "DELETE" { + return strconv.Itoa(res.StatusCode) + } + return string(body) } -- cgit v1.2.3 From 208cb52b47278315b5227251d5b65c07908cde23 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 13 Feb 2022 16:15:47 +0530 Subject: added support to get all anime from user's anime list --- user/anime/animelist.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/user/anime/animelist.go b/user/anime/animelist.go index 7fe81b8..120bdea 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -36,6 +36,11 @@ func (c AnimeListClient)DeleteAnime(id int) string { return c.requestHandler(endpoint, "DELETE") } +// Update/Add an anime to user's anime list +// func UpdateAnime(id int) { + +// } + // Get authenticated user's anime list func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset int) (a.AnimeList, error){ var userAnimeList a.AnimeList @@ -51,7 +56,7 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset i } // checks if valid status is specified - if !e.IsValidListStatus(status) { + if status != "" && !e.IsValidListStatus(status) { return userAnimeList, errors.New(fmt.Sprintf("GetAnimeList: Invalid status specified: \"%s\"", status)) } @@ -60,7 +65,13 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset i user = "@me" } - endpoint := BASE_URL + "/users/" + user + "/animelist?status=" + status + "&sort=" + sort + "&limit=" + strconv.Itoa(limit) + "&offset=" + strconv.Itoa(offset) + // if status is "" it returns all anime + var endpoint string + if status == "" { + endpoint = BASE_URL + "/users/" + user + "/animelist?sort=" + sort + "&limit=" + strconv.Itoa(limit) + "&offset=" + strconv.Itoa(offset) + } else { + endpoint = BASE_URL + "/users/" + user + "/animelist?status=" + status + "&sort=" + sort + "&limit=" + strconv.Itoa(limit) + "&offset=" + strconv.Itoa(offset) + } // get data from API var animeListData AnimeListRaw -- cgit v1.2.3 From 419e08bc3a369a0c1138871184e1a30320032afd Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 13 Feb 2022 16:56:09 +0530 Subject: added functionality to update anime list of authenticated user --- user/anime/animelist.go | 14 ++++++++++++-- user/anime/animelist.structs.go | 13 +++++++++++++ user/anime/request_handler.go | 26 ++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/user/anime/animelist.go b/user/anime/animelist.go index 120bdea..4f5eac5 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -37,9 +37,19 @@ func (c AnimeListClient)DeleteAnime(id int) string { } // Update/Add an anime to user's anime list -// func UpdateAnime(id int) { +func (c AnimeListClient)UpdateAnime(id int, data UpdateAnimeData) string { + endpoint := fmt.Sprintf("%s/anime/%d/my_list_status", BASE_URL, id) + + // turn data struct into json + pepe, err := json.Marshal(data) + if err != nil { + fmt.Println(err) + } -// } + // finally make API request + res := c.putRequestHandler(endpoint, pepe) + return res +} // Get authenticated user's anime list func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset int) (a.AnimeList, error){ diff --git a/user/anime/animelist.structs.go b/user/anime/animelist.structs.go index d43b47b..d874f7b 100644 --- a/user/anime/animelist.structs.go +++ b/user/anime/animelist.structs.go @@ -27,3 +27,16 @@ type AnimeListRaw struct { } `json:"data"` Paging anime.ListPaging `json:"paging"` } + +type UpdateAnimeData struct { + Status string `json:"status"` + IsRewatching bool `json:"is_rewatching"` + Score int `json:"score"` + EpWatched int `json:"num_watched_episodes"` + Priority int `json:"priority"` + TimesRewatched int `json:"num_times_rewatched"` + // NOTE: idk what RewatchValue is + RewatchValue int `json:"rewatch_value"` + Tags string `json:"tags"` + Comments string `json:"comments"` +} diff --git a/user/anime/request_handler.go b/user/anime/request_handler.go index 2007e56..22c6e0b 100644 --- a/user/anime/request_handler.go +++ b/user/anime/request_handler.go @@ -21,6 +21,7 @@ import ( "log" "net/http" "strconv" + "bytes" ) // Handles HTTP request with your OAuth token as a Header @@ -52,3 +53,28 @@ func (c AnimeListClient) requestHandler(endpoint, method string) string { return string(body) } + +// for PUT requests (used by UpdateAnime) +func (c AnimeListClient) putRequestHandler(endpoint string, data []uint8) string { + // generate request + req, err := http.NewRequest(http.MethodPut, endpoint, bytes.NewBuffer(data)) + if err != nil { + log.Fatal(err) + } + req.Header.Add("Authorization", c.AuthToken) + + // do request + res, err := c.HttpClient.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) +} -- cgit v1.2.3