anitrack/SimklFunctions.go

288 lines
6.3 KiB
Go
Raw Normal View History

2024-07-30 12:57:08 -04:00
package main
2024-07-30 12:59:06 -04:00
import (
"bytes"
2024-07-30 12:59:06 -04:00
"encoding/json"
2024-07-30 20:40:16 -04:00
"fmt"
2024-07-30 12:59:06 -04:00
"io"
"log"
"net/http"
2024-08-18 17:23:09 -04:00
"reflect"
"strconv"
2024-07-30 12:59:06 -04:00
)
var SimklWatchList SimklWatchListType
2024-08-18 17:23:09 -04:00
func SimklHelper(method string, url string, body interface{}) json.RawMessage {
reader, _ := json.Marshal(body)
var req *http.Request
2024-07-30 20:40:16 -04:00
client := &http.Client{}
2024-08-18 17:23:09 -04:00
if body != nil {
req, _ = http.NewRequest(method, url, bytes.NewBuffer(reader))
} else {
req, _ = http.NewRequest(method, url, nil)
}
2024-07-30 20:40:16 -04:00
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+simklJwt.AccessToken)
req.Header.Add("simkl-api-key", Environment.SIMKL_CLIENT_ID)
2024-07-30 20:40:16 -04:00
resp, err := client.Do(req)
2024-07-30 12:59:06 -04:00
if err != nil {
2024-07-30 20:40:16 -04:00
fmt.Println("Errored when sending request to the server")
2024-08-18 17:23:09 -04:00
message, _ := json.Marshal(struct {
Message string `json:"message"`
}{
Message: "Errored when sending request to the server" + err.Error(),
})
return message
2024-07-30 12:59:06 -04:00
}
2024-07-30 20:40:16 -04:00
defer resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
2024-07-30 12:59:06 -04:00
2024-08-18 17:23:09 -04:00
return respBody
2024-07-30 12:59:06 -04:00
}
func (a *App) SimklGetUserWatchlist() SimklWatchListType {
2024-08-18 17:23:09 -04:00
method := "GET"
url := "https://api.simkl.com/sync/all-items/anime/watching"
2024-08-18 17:23:09 -04:00
respBody := SimklHelper(method, url, nil)
2024-08-18 17:23:09 -04:00
var errCheck struct {
Error string `json:"error"`
Message string `json:"message"`
}
2024-08-18 17:23:09 -04:00
err := json.Unmarshal(respBody, &errCheck)
if err != nil {
2024-08-18 17:23:09 -04:00
log.Printf("Failed at unmarshal, %s\n", err)
}
2024-08-18 17:23:09 -04:00
if errCheck.Error != "" {
a.LogoutSimkl()
return SimklWatchListType{}
}
var watchlist SimklWatchListType
2024-08-18 17:23:09 -04:00
err = json.Unmarshal(respBody, &watchlist)
if err != nil {
log.Printf("Failed at unmarshal, %s\n", err)
}
SimklWatchList = watchlist
2024-08-18 17:23:09 -04:00
return watchlist
}
2024-08-19 08:09:03 -04:00
func (a *App) SimklSyncEpisodes(anime SimklAnime, progress int) SimklAnime {
var episodes []Episode
var url string
var shows []SimklPostShow
if progress >= anime.WatchedEpisodesCount {
for i := 1; i <= progress; i++ {
episodes = append(episodes, Episode{Number: i})
}
url = "https://api.simkl.com/sync/history"
} else {
for i := anime.WatchedEpisodesCount; i > progress; i-- {
episodes = append(episodes, Episode{Number: i})
}
url = "https://api.simkl.com/sync/history/remove"
}
formattedShow := SimklPostShow{
Title: anime.Show.Title,
Ids: Ids{
Simkl: anime.Show.Ids.Simkl,
Mal: anime.Show.Ids.Mal,
Anilist: anime.Show.Ids.AniList,
},
Episodes: episodes,
}
shows = append(shows, formattedShow)
simklSync := SimklSyncHistoryType{shows}
2024-08-18 17:23:09 -04:00
respBody := SimklHelper("POST", url, simklSync)
var success interface{}
err := json.Unmarshal(respBody, &success)
if err != nil {
log.Printf("Failed at unmarshal, %s\n", err)
}
for i, simklAnime := range SimklWatchList.Anime {
2024-08-19 08:09:03 -04:00
if anime.Show.Ids.Simkl == simklAnime.Show.Ids.Simkl {
SimklWatchList.Anime[i].WatchedEpisodesCount = progress
2024-08-19 08:09:03 -04:00
}
}
anime.WatchedEpisodesCount = progress
return anime
}
2024-08-11 18:32:44 -04:00
2024-08-19 08:09:03 -04:00
func (a *App) SimklSyncRating(anime SimklAnime, rating int) SimklAnime {
2024-08-11 18:32:44 -04:00
var url string
var showWithRating = ShowWithRating{
Title: anime.Show.Title,
Ids: Ids{
Simkl: anime.Show.Ids.Simkl,
Mal: anime.Show.Ids.Mal,
Anilist: anime.Show.Ids.AniList,
},
Rating: rating,
}
var showWithoutRating = ShowWithoutRating{
Title: anime.Show.Title,
Ids: Ids{
Simkl: anime.Show.Ids.Simkl,
Mal: anime.Show.Ids.Mal,
Anilist: anime.Show.Ids.AniList,
},
}
var shows []interface{}
if rating > 0 {
shows = append(shows, showWithRating)
url = "https://api.simkl.com/sync/ratings"
} else {
shows = append(shows, showWithoutRating)
url = "https://api.simkl.com/sync/ratings/remove"
}
simklSync := struct {
Shows []interface{} `json:"shows" ts_type:"shows"`
}{shows}
2024-08-18 17:23:09 -04:00
respBody := SimklHelper("POST", url, simklSync)
2024-08-11 18:32:44 -04:00
var success interface{}
err := json.Unmarshal(respBody, &success)
if err != nil {
log.Printf("Failed at unmarshal, %s\n", err)
}
for i, simklAnime := range SimklWatchList.Anime {
2024-08-19 08:09:03 -04:00
if anime.Show.Ids.Simkl == simklAnime.Show.Ids.Simkl {
SimklWatchList.Anime[i].UserRating = rating
2024-08-19 08:09:03 -04:00
}
}
anime.UserRating = rating
return anime
2024-08-11 18:32:44 -04:00
}
2024-08-11 19:42:52 -04:00
2024-08-19 08:09:03 -04:00
func (a *App) SimklSyncStatus(anime SimklAnime, status string) SimklAnime {
2024-08-11 19:42:52 -04:00
url := "https://api.simkl.com/sync/add-to-list"
var show = SimklShowStatus{
Title: anime.Show.Title,
Ids: Ids{
Simkl: anime.Show.Ids.Simkl,
Mal: anime.Show.Ids.Mal,
Anilist: anime.Show.Ids.AniList,
},
To: status,
}
var shows []SimklShowStatus
shows = append(shows, show)
simklSync := struct {
Shows []SimklShowStatus `json:"shows" ts_type:"shows"`
}{shows}
2024-08-18 17:23:09 -04:00
respBody := SimklHelper("POST", url, simklSync)
2024-08-11 19:42:52 -04:00
var success interface{}
err := json.Unmarshal(respBody, &success)
if err != nil {
log.Printf("Failed at unmarshal, %s\n", err)
}
for i, simklAnime := range SimklWatchList.Anime {
2024-08-19 08:09:03 -04:00
if anime.Show.Ids.Simkl == simklAnime.Show.Ids.Simkl {
SimklWatchList.Anime[i].Status = status
2024-08-19 08:09:03 -04:00
}
}
anime.Status = status
return anime
2024-08-11 19:42:52 -04:00
}
2024-08-18 17:23:09 -04:00
func (a *App) SimklSearch(aniListAnime MediaList) SimklAnime {
2024-08-18 17:23:09 -04:00
var result SimklAnime
if reflect.DeepEqual(SimklWatchList, SimklWatchListType{}) {
2024-08-18 17:23:09 -04:00
fmt.Println("Watchlist empty. Calling...")
SimklWatchList = a.SimklGetUserWatchlist()
2024-08-18 17:23:09 -04:00
}
for _, anime := range SimklWatchList.Anime {
2024-08-18 17:23:09 -04:00
id, err := strconv.Atoi(anime.Show.Ids.AniList)
if err != nil {
fmt.Println("AniList ID does not exist on " + anime.Show.Title)
}
if id == aniListAnime.Media.ID {
2024-08-18 17:23:09 -04:00
result = anime
}
}
if reflect.DeepEqual(result, SimklAnime{}) {
var anime SimklSearchType
url := "https://api.simkl.com/search/id?anilist=" + strconv.Itoa(aniListAnime.Media.ID)
2024-08-18 17:23:09 -04:00
respBody := SimklHelper("GET", url, nil)
err := json.Unmarshal(respBody, &anime)
if len(anime) == 0 {
fmt.Println("reached search by mal")
url = "https://api.simkl.com/search/id?mal=" + strconv.Itoa(aniListAnime.Media.IDMal)
respBody = SimklHelper("GET", url, nil)
fmt.Println(string(respBody))
err = json.Unmarshal(respBody, &anime)
}
2024-08-18 17:23:09 -04:00
if err != nil {
log.Printf("Failed at unmarshal, %s\n", err)
}
for _, watchListAnime := range SimklWatchList.Anime {
id := watchListAnime.Show.Ids.Simkl
if id == anime[0].Ids.Simkl {
result = watchListAnime
}
}
if reflect.DeepEqual(result, SimklSearchType{}) && len(anime) > 0 {
2024-08-18 17:23:09 -04:00
result.Show.Title = anime[0].Title
result.Show.Poster = anime[0].Poster
result.Show.Ids.Simkl = anime[0].Ids.Simkl
result.Show.Ids.Slug = anime[0].Ids.Slug
}
}
return result
}