anitrack/MALFunctions.go

117 lines
3.3 KiB
Go
Raw Permalink Normal View History

2024-08-15 21:27:31 -04:00
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strconv"
"strings"
)
func MALHelper(method string, malUrl string, body url.Values) (json.RawMessage, string) {
2024-08-15 21:27:31 -04:00
client := &http.Client{}
req, _ := http.NewRequest(method, malUrl, strings.NewReader(body.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Authorization", "Bearer "+myAnimeListJwt.AccessToken)
resp, err := client.Do(req)
if err != nil {
fmt.Println("Errored when sending request to the server")
message, _ := json.Marshal(struct {
Message string `json:"message" ts_type:"message"`
}{
Message: "Errored when sending request to the server" + err.Error(),
})
return message, resp.Status
2024-08-15 21:27:31 -04:00
}
defer resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
if resp.Status == "401 Unauthorized" {
refreshMyAnimeListAuthorizationToken()
MALHelper(method, malUrl, body)
}
return respBody, resp.Status
2024-08-15 21:27:31 -04:00
}
func (a *App) GetMyAnimeList(count int) MALWatchlist {
limit := strconv.Itoa(count)
user := a.GetMyAnimeListLoggedInUser()
malUrl := "https://api.myanimelist.net/v2/users/" + user.Name + "/animelist?fields=list_status&status=watching&limit=" + limit
var malList MALWatchlist
respBody, resStatus := MALHelper("GET", malUrl, nil)
2024-08-15 21:27:31 -04:00
if resStatus == "200 OK" {
err := json.Unmarshal(respBody, &malList)
if err != nil {
log.Printf("Failed to unmarshal json response, %s\n", err)
}
2024-08-15 21:27:31 -04:00
}
return malList
}
func (a *App) MyAnimeListUpdate(anime MALAnime, update MALUploadStatus) MalListStatus {
if update.NumTimesRewatched >= 1 {
update.IsRewatching = true
} else {
update.IsRewatching = false
}
body := url.Values{}
body.Set("status", update.Status)
body.Set("is_rewatching", strconv.FormatBool(update.IsRewatching))
body.Set("score", strconv.Itoa(update.Score))
body.Set("num_watched_episodes", strconv.Itoa(update.NumWatchedEpisodes))
body.Set("num_times_rewatched", strconv.Itoa(update.NumTimesRewatched))
body.Set("comments", update.Comments)
malUrl := "https://api.myanimelist.net/v2/anime/" + strconv.Itoa(anime.Id) + "/my_list_status"
var status MalListStatus
respBody, respStatus := MALHelper("PATCH", malUrl, body)
if respStatus == "200 OK" {
err := json.Unmarshal(respBody, &status)
if err != nil {
log.Printf("Failed to unmarshal json response, %s\n", err)
}
}
return status
}
func (a *App) GetMyAnimeListAnime(id int) MALAnime {
malUrl := "https://api.myanimelist.net/v2/anime/" + strconv.Itoa(id) + "?fields=id,title,main_picture,alternative_titles,start_date,end_date,synopsis,mean,rank,popularity,num_list_users,num_scoring_users,nsfw,genres,created_at,updated_at,media_type,status,my_list_status,num_episodes,start_season,broadcast,source,average_episode_duration,rating,pictures,background,related_anime,recommendations,studios,statistics"
respBody, respStatus := MALHelper("GET", malUrl, nil)
var malAnime MALAnime
if respStatus == "200 OK" {
err := json.Unmarshal(respBody, &malAnime)
if err != nil {
log.Printf("Failed to unmarshal json response, %s\n", err)
}
}
return malAnime
}
func (a *App) DeleteMyAnimeListEntry(id int) bool {
malUrl := "https://api.myanimelist.net/v2/anime/" + strconv.Itoa(id) + "/my_list_status"
_, respStatus := MALHelper("DELETE", malUrl, nil)
if respStatus == "200 OK" {
return true
} else {
return false
}
}