57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
|
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 {
|
||
|
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
|
||
|
}
|
||
|
|
||
|
defer resp.Body.Close()
|
||
|
respBody, _ := io.ReadAll(resp.Body)
|
||
|
|
||
|
return respBody
|
||
|
}
|
||
|
|
||
|
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 := MALHelper("GET", malUrl, nil)
|
||
|
|
||
|
err := json.Unmarshal(respBody, &malList)
|
||
|
if err != nil {
|
||
|
log.Printf("Failed to unmarshal json response, %s\n", err)
|
||
|
}
|
||
|
|
||
|
return malList
|
||
|
}
|