From fff3dadabc321871e6819392efc605c6dffe2e69 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Tue, 15 Aug 2023 10:57:27 +0530 Subject: migrated from MAL2Go to mg package --- mal/data.go | 17 +++++++++-------- mal/delete.go | 18 +++++------------- mal/episodes.go | 20 +++++++++++--------- mal/list.go | 31 ++++++++++++++++++++++++++----- mal/mal.go | 21 ++++----------------- mal/score.go | 17 +++++++++-------- mal/search.go | 31 +++++++++++++++++++++---------- mal/seasonals.go | 19 +++++++++++++------ mal/status.go | 17 +++++++++-------- mal/user.go | 9 +++++---- 10 files changed, 112 insertions(+), 88 deletions(-) (limited to 'mal') diff --git a/mal/data.go b/mal/data.go index 59b0e9a..4422caa 100644 --- a/mal/data.go +++ b/mal/data.go @@ -19,15 +19,15 @@ along with this program. If not, see . package mal import ( + "vidhukant.com/mg" "fmt" "os" - m "github.com/MikunoNaka/MAL2Go/v4/manga" - a "github.com/MikunoNaka/MAL2Go/v4/anime" ) -// because MAL2Go/Anime.SearchAnime won't give us all the data sometimes -func GetAnimeData(animeId int, fields []string) a.Anime { - data, err := animeClient.GetAnimeById(animeId, fields) +// because mg.SearchAnime won't give us all the data sometimes +func GetAnimeData(animeId int, fields []string) mg.Anime { + var data mg.Anime + err := MALClient.GetAnimeById(&data, animeId, fields) if err != nil { fmt.Println("Error while fetching data about anime:", err) os.Exit(1) @@ -35,9 +35,10 @@ func GetAnimeData(animeId int, fields []string) a.Anime { return data } -// because MAL2Go/Manga.SearchManga won't give us all the data sometimes -func GetMangaData(mangaId int, fields []string) m.Manga { - data, err := mangaClient.GetMangaById(mangaId, fields) +// because mg.SearchManga won't give us all the data sometimes +func GetMangaData(mangaId int, fields []string) mg.Manga { + var data mg.Manga + err := MALClient.GetMangaById(&data, mangaId, fields) if err != nil { fmt.Println("Error while fetching data about manga:", err) os.Exit(1) diff --git a/mal/delete.go b/mal/delete.go index e6cfd32..61acc94 100644 --- a/mal/delete.go +++ b/mal/delete.go @@ -19,28 +19,20 @@ along with this program. If not, see . package mal import ( + "vidhukant.com/mg" "fmt" - // "os" - a "github.com/MikunoNaka/MAL2Go/v4/anime" - m "github.com/MikunoNaka/MAL2Go/v4/manga" ) -func DeleteAnime(anime a.Anime) { - res, err := userAnimeClient.DeleteAnime(anime.Id) +func DeleteAnime(anime mg.Anime) { + err := MALClient.DeleteAnime(anime.Id) if err != nil { fmt.Println("Error While Deleting " + anime.Title + ":", err) } - if res != "200" { - fmt.Println("Error: MyAnimeList Returned " + res + " while deleting " + anime.Title) - } } -func DeleteManga(manga m.Manga) { - res, err := userMangaClient.DeleteManga(manga.Id) +func DeleteManga(manga mg.Manga) { + err := MALClient.DeleteManga(manga.Id) if err != nil { fmt.Println("Error While Deleting " + manga.Title + ":", err) } - if res != "200" { - fmt.Println("Error: MyAnimeList Returned " + res + " while deleting " + manga.Title) - } } diff --git a/mal/episodes.go b/mal/episodes.go index 0940af5..caea1a8 100644 --- a/mal/episodes.go +++ b/mal/episodes.go @@ -19,15 +19,15 @@ along with this program. If not, see . package mal import ( + "vidhukant.com/macli/util" + "vidhukant.com/mg" "fmt" "os" - a "github.com/MikunoNaka/MAL2Go/v4/user/anime" - m "github.com/MikunoNaka/MAL2Go/v4/user/manga" - "github.com/MikunoNaka/macli/util" ) -func SetEpisodes(animeId, prevValue int, ep string) a.UpdateResponse { - res, err := userAnimeClient.SetWatchedEpisodes(animeId, util.ParseNumeric(ep, prevValue)) +func SetEpisodes(animeId, prevValue int, ep string) mg.AnimeUpdateResponse { + var res mg.AnimeUpdateResponse + err := MALClient.UpdateAnime(&res, animeId, map[string]interface{}{mg.EpisodesWatched: util.ParseNumeric(ep, prevValue)}) if err != nil { fmt.Println("MyAnimeList returned error while updating episodes:", err) os.Exit(1) @@ -35,8 +35,9 @@ func SetEpisodes(animeId, prevValue int, ep string) a.UpdateResponse { return res } -func SetChapters(mangaId, prevValue int, ch string) m.UpdateResponse { - res, err := userMangaClient.SetChaptersRead(mangaId, util.ParseNumeric(ch, prevValue)) +func SetChapters(mangaId, prevValue int, ch string) mg.MangaUpdateResponse { + var res mg.MangaUpdateResponse + err := MALClient.UpdateManga(&res, mangaId, map[string]interface{}{mg.ChaptersRead: util.ParseNumeric(ch, prevValue)}) if err != nil { fmt.Println("MyAnimeList returned error while updating chapters:", err) os.Exit(1) @@ -44,8 +45,9 @@ func SetChapters(mangaId, prevValue int, ch string) m.UpdateResponse { return res } -func SetVolumes(mangaId, prevValue int, vol string) m.UpdateResponse { - res, err := userMangaClient.SetVolumesRead(mangaId, util.ParseNumeric(vol, prevValue)) +func SetVolumes(mangaId, prevValue int, vol string) mg.MangaUpdateResponse { + var res mg.MangaUpdateResponse + err := MALClient.UpdateManga(&res, mangaId, map[string]interface{}{mg.VolumesRead: util.ParseNumeric(vol, prevValue)}) if err != nil { fmt.Println("MyAnimeList returned error while updating volumes:", err) os.Exit(1) diff --git a/mal/list.go b/mal/list.go index 3e9e737..f00edd9 100644 --- a/mal/list.go +++ b/mal/list.go @@ -19,28 +19,49 @@ along with this program. If not, see . package mal import ( + "vidhukant.com/mg" "fmt" "os" - a "github.com/MikunoNaka/MAL2Go/v4/anime" - m "github.com/MikunoNaka/MAL2Go/v4/manga" ) // TODO: return all the list items using loop -func AnimeList(user, status, sort string) []a.Anime { - res, _, err := userAnimeClient.GetAnimeList(user, status, sort, 1000, 0, SearchNSFW, []string{"title", "num_episodes", "media_type"}) +func AnimeList(user, status, sort string) []mg.Anime { + var res []mg.Anime + _, err := MALClient.GetAnimeList(&res, &mg.ListParams{ + Username: user, + Status: status, + Sort: sort, + Limit: 1000, + Offset: 0, + NSFW: SearchNSFW, + Fields: []string{"title", "num_episodes", "media_type"}, + }) + if err != nil { fmt.Println(err) os.Exit(1) } + return res } // TODO: return all the list items using loop func MangaList(user, status, sort string) []m.Manga { - res, _, err := userMangaClient.GetMangaList(user, status, sort, 1000, 0, SearchNSFW, []string{"title", "num_chapters", "num_volumes", "media_type"}) + var res []mg.Manga + _, err := MALClient.GetMangaList(&res, &mg.ListParams{ + Username: user, + Status: status, + Sort: sort, + Limit: 1000, + Offset: 0, + NSFW: SearchNSFW, + Fields: []string{"title", "num_chapters", "num_volumes", "media_type"}, + }) + if err != nil { fmt.Println(err) os.Exit(1) } + return res } diff --git a/mal/mal.go b/mal/mal.go index 69cb1f1..ce25d22 100644 --- a/mal/mal.go +++ b/mal/mal.go @@ -19,23 +19,15 @@ along with this program. If not, see . package mal import ( - "github.com/MikunoNaka/macli/auth" - a "github.com/MikunoNaka/MAL2Go/v4/anime" - m "github.com/MikunoNaka/MAL2Go/v4/manga" - u "github.com/MikunoNaka/MAL2Go/v4/user" - ua "github.com/MikunoNaka/MAL2Go/v4/user/anime" - um "github.com/MikunoNaka/MAL2Go/v4/user/manga" + "vidhukant.com/macli/auth" + "vidhukant.com/mg" "github.com/spf13/viper" "strings" ) var ( Secret string - animeClient a.Client - mangaClient m.Client - userClient u.Client - userAnimeClient ua.Client - userMangaClient um.Client + MALClient mg.Client SearchLength, SearchOffset int SearchNSFW bool @@ -52,10 +44,5 @@ func Init() { } tk := "Bearer " + strings.TrimSpace(Secret) - // initialise MAL2Go Client(s) - animeClient.AuthToken = tk - mangaClient.AuthToken = tk - userClient.AuthToken = tk - userAnimeClient.AuthToken = tk - userMangaClient.AuthToken = tk + MALClient.MainAuth = tk } diff --git a/mal/score.go b/mal/score.go index 2db6000..c641302 100644 --- a/mal/score.go +++ b/mal/score.go @@ -19,23 +19,24 @@ along with this program. If not, see . package mal import ( + "vidhukant.com/mg" "fmt" - a "github.com/MikunoNaka/MAL2Go/v4/user/anime" - m "github.com/MikunoNaka/MAL2Go/v4/user/manga" ) -func SetAnimeScore(animeId, score int) a.UpdateResponse { - resp, err := userAnimeClient.SetScore(animeId, score) +func SetAnimeScore(animeId, score int) mg.AnimeUpdateResponse { + var res mg.AnimeUpdateResponse + err := MALClient.UpdateAnime(&res, animeId, map[string]interface{}{mg.Score: score}) if err != nil { fmt.Println("MyAnimeList returned error while updating anime score:", err) } - return resp + return res } -func SetMangaScore(mangaId, score int) m.UpdateResponse { - resp, err := userMangaClient.SetScore(mangaId, score) +func SetMangaScore(mangaId, score int) mg.MangaUpdateResponse { + var res mg.MangaUpdateResponse + err := MALClient.UpdateManga(&res, mangaId, map[string]interface{}{mg.Score: score}) if err != nil { fmt.Println("MyAnimeList returned error while updating manga score:", err) } - return resp + return res } diff --git a/mal/search.go b/mal/search.go index e6e416a..b8dafb9 100644 --- a/mal/search.go +++ b/mal/search.go @@ -19,22 +19,27 @@ along with this program. If not, see . package mal import ( + "vidhukant.com/mg" "fmt" "os" - a "github.com/MikunoNaka/MAL2Go/v4/anime" - m "github.com/MikunoNaka/MAL2Go/v4/manga" ) -func SearchAnime(searchString string, fields []string) []a.Anime { - fields = append([]string{"title", "id"}, fields...) - +func SearchAnime(searchString string, fields []string) []mg.Anime { searchLength, searchOffset := SearchLength, SearchOffset if AutoSel > 0 { searchLength = 1 searchOffset = AutoSel - 1 } - res, err := animeClient.SearchAnime(searchString, searchLength, searchOffset, SearchNSFW, fields) + var res []mg.Anime + err := MALClient.SearchAnime(&res, &mg.SearchParams{ + Limit: searchLength, + Offset: searchOffset, + NSFW: SearchNSFW, + SearchString: searchString, + Fields: append([]string{"title", "id"}, fields...), + }) + if err != nil { fmt.Println("MyAnimeList reported error while searching:", err.Error()) os.Exit(1) @@ -43,16 +48,22 @@ func SearchAnime(searchString string, fields []string) []a.Anime { return res } -func SearchManga(searchString string, fields []string) []m.Manga { - fields = append([]string{"title", "id"}, fields...) - +func SearchManga(searchString string, fields []string) []mg.Manga { searchLength, searchOffset := SearchLength, SearchOffset if AutoSel > 0 { searchLength = 1 searchOffset = AutoSel - 1 } - res, err := mangaClient.SearchManga(searchString, searchLength, searchOffset, SearchNSFW, fields) + var res []mg.Manga + err := MALClient.SearchManga(&res, &mg.SearchParams{ + Limit: searchLength, + Offset: searchOffset, + NSFW: SearchNSFW, + SearchString: searchString, + Fields: append([]string{"title", "id"}, fields...), + }) + if err != nil { fmt.Println("MyAnimeList reported error while searching:", err.Error()) os.Exit(1) diff --git a/mal/seasonals.go b/mal/seasonals.go index fda130b..29cfea6 100644 --- a/mal/seasonals.go +++ b/mal/seasonals.go @@ -19,21 +19,28 @@ along with this program. If not, see . package mal import ( + "vidhukant.com/mg" "fmt" "os" "strconv" - a "github.com/MikunoNaka/MAL2Go/v4/anime" ) -func GetSeasonalAnime(season a.Season, sort string) []a.Anime { - year := strconv.Itoa(season.Year) - fields := []string{"title", "id"} +func GetSeasonalAnime(season a.Season, sort string) []mg.Anime { + var res []mg.Anime + err := MALClient.GetSeasonalAnime(&res, &mg.SeasonalParams{ + Year: strconv.Itoa(season.Year), + Season: season.Name, + Sort: sort, + Limit: SearchLength, + Offset: SearchOffset, + NSFW: SearchNSFW, + Fields: []string{"title", "id"}, + }) - res, err := animeClient.GetSeasonalAnime(year, season.Name, sort, SearchLength, SearchOffset, SearchNSFW, fields) if err != nil { fmt.Println("MyAnimeList reported error while getting seasonal animes:", err.Error()) os.Exit(1) } - return res.Animes + return res } diff --git a/mal/status.go b/mal/status.go index e48f021..ba8eb4d 100644 --- a/mal/status.go +++ b/mal/status.go @@ -19,26 +19,27 @@ along with this program. If not, see . package mal import ( + "vidhukant.com/mg" "fmt" "os" - a "github.com/MikunoNaka/MAL2Go/v4/user/anime" - m "github.com/MikunoNaka/MAL2Go/v4/user/manga" ) -func SetAnimeStatus(animeId int, status string) a.UpdateResponse { - resp, err := userAnimeClient.SetStatus(animeId, status) +func SetAnimeStatus(animeId int, status string) mg.AnimeUpdateResponse { + var res mg.AnimeUpdateResponse + err := MALClient.UpdateAnime(&res, animeId, map[string]interface{}{mg.Status: status}) if err != nil { fmt.Println("Error while parsing status:", err.Error()) os.Exit(1) } - return resp + return res } -func SetMangaStatus(mangaId int, status string) m.UpdateResponse { - resp, err := userMangaClient.SetStatus(mangaId, status) +func SetMangaStatus(mangaId int, status string) mg.MangaUpdateResponse { + var res mg.MangaUpdateResponse + err := MALClient.UpdateManga(&res, mangaId, map[string]interface{}{mg.Status: status}) if err != nil { fmt.Println("Error while parsing status:", err.Error()) os.Exit(1) } - return resp + return res } diff --git a/mal/user.go b/mal/user.go index c271aab..b9301e0 100644 --- a/mal/user.go +++ b/mal/user.go @@ -19,16 +19,17 @@ along with this program. If not, see . package mal import ( - u "github.com/MikunoNaka/MAL2Go/v4/user" + "vidhukant.com/mg" "fmt" "os" ) -func GetUserInfo() u.UserInfo { - userInfo, err := userClient.GetSelfUserInfo() +func GetUserInfo() mg.User { + var user mg.User + err := MALClient.GetSelfInfo(&user, true) if err != nil { fmt.Println("Error fetching User Information:", err) os.Exit(1) } - return userInfo + return user } -- cgit v1.2.3