anitrack/SimklFunctions.go

195 lines
4.1 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-07-30 20:40:16 -04:00
"os"
2024-07-30 12:59:06 -04:00
)
2024-07-30 20:40:16 -04:00
func (a *App) SimklGetUserWatchlist() SimklWatchList {
client := &http.Client{}
req, _ := http.NewRequest("GET", "https://api.simkl.com/sync/all-items/anime/watching", nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+simklJwt.AccessToken)
req.Header.Add("simkl-api-key", os.Getenv("SIMKL_CLIENT_ID"))
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")
return SimklWatchList{}
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-07-30 20:40:16 -04:00
var watchlist SimklWatchList
2024-07-30 12:59:06 -04:00
2024-07-30 20:40:16 -04:00
err = json.Unmarshal(respBody, &watchlist)
if err != nil {
log.Printf("Failed at unmarshal, %s\n", err)
}
2024-07-30 12:59:06 -04:00
2024-07-30 20:40:16 -04:00
return watchlist
2024-07-30 12:59:06 -04:00
}
func SimklPostHelper(url string, body interface{}) json.RawMessage {
reader, _ := json.Marshal(body)
client := &http.Client{}
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(reader))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+simklJwt.AccessToken)
req.Header.Add("simkl-api-key", os.Getenv("SIMKL_CLIENT_ID"))
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) SimklSyncEpisodes(anime Anime, progress int) interface{} {
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}
respBody := SimklPostHelper(url, simklSync)
var success interface{}
err := json.Unmarshal(respBody, &success)
if err != nil {
log.Printf("Failed at unmarshal, %s\n", err)
}
return success
}
2024-08-11 18:32:44 -04:00
func (a *App) SimklSyncRating(anime Anime, rating int) interface{} {
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}
respBody := SimklPostHelper(url, simklSync)
var success interface{}
err := json.Unmarshal(respBody, &success)
if err != nil {
log.Printf("Failed at unmarshal, %s\n", err)
}
return success
}
2024-08-11 19:42:52 -04:00
func (a *App) SimklSyncStatus(anime Anime, status string) interface{} {
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}
respBody := SimklPostHelper(url, simklSync)
var success interface{}
err := json.Unmarshal(respBody, &success)
if err != nil {
log.Printf("Failed at unmarshal, %s\n", err)
}
return success
}