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 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 user/anime/animelist.go (limited to 'user/anime/animelist.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) +} + -- 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 --- user/anime/animelist.go | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) (limited to 'user/anime/animelist.go') 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) } -- 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 --- user/anime/animelist.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'user/anime/animelist.go') 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 } -- 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 --- user/anime/animelist.go | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'user/anime/animelist.go') 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(-) (limited to 'user/anime/animelist.go') 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 ++++++++ 1 file changed, 8 insertions(+) (limited to 'user/anime/animelist.go') 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 -- 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(-) (limited to 'user/anime/animelist.go') 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 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'user/anime/animelist.go') 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){ -- cgit v1.2.3