Fixed Simkl Bugs
This commit is contained in:
@ -7,43 +7,23 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (a *App) SimklGetUserWatchlist() SimklWatchList {
|
||||
client := &http.Client{}
|
||||
var WatchList SimklWatchList
|
||||
|
||||
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", Environment.SIMKL_CLIENT_ID)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Errored when sending request to the server")
|
||||
return SimklWatchList{}
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
respBody, _ := io.ReadAll(resp.Body)
|
||||
|
||||
var watchlist SimklWatchList
|
||||
|
||||
err = json.Unmarshal(respBody, &watchlist)
|
||||
if err != nil {
|
||||
log.Printf("Failed at unmarshal, %s\n", err)
|
||||
}
|
||||
|
||||
return watchlist
|
||||
}
|
||||
|
||||
func SimklPostHelper(url string, body interface{}) json.RawMessage {
|
||||
func SimklHelper(method string, url string, body interface{}) json.RawMessage {
|
||||
reader, _ := json.Marshal(body)
|
||||
var req *http.Request
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(reader))
|
||||
if body != nil {
|
||||
req, _ = http.NewRequest(method, url, bytes.NewBuffer(reader))
|
||||
} else {
|
||||
req, _ = http.NewRequest(method, url, nil)
|
||||
}
|
||||
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", "Bearer "+simklJwt.AccessToken)
|
||||
@ -54,7 +34,7 @@ func SimklPostHelper(url string, body interface{}) json.RawMessage {
|
||||
if err != nil {
|
||||
fmt.Println("Errored when sending request to the server")
|
||||
message, _ := json.Marshal(struct {
|
||||
Message string `json:"message" ts_type:"message"`
|
||||
Message string `json:"message"`
|
||||
}{
|
||||
Message: "Errored when sending request to the server" + err.Error(),
|
||||
})
|
||||
@ -69,7 +49,41 @@ func SimklPostHelper(url string, body interface{}) json.RawMessage {
|
||||
|
||||
}
|
||||
|
||||
func (a *App) SimklSyncEpisodes(anime Anime, progress int) interface{} {
|
||||
func (a *App) SimklGetUserWatchlist() SimklWatchList {
|
||||
method := "GET"
|
||||
url := "https://api.simkl.com/sync/all-items/anime/watching"
|
||||
|
||||
respBody := SimklHelper(method, url, nil)
|
||||
|
||||
var errCheck struct {
|
||||
Error string `json:"error"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
err := json.Unmarshal(respBody, &errCheck)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Failed at unmarshal, %s\n", err)
|
||||
}
|
||||
|
||||
if errCheck.Error != "" {
|
||||
a.LogoutSimkl()
|
||||
return SimklWatchList{}
|
||||
}
|
||||
|
||||
var watchlist SimklWatchList
|
||||
|
||||
err = json.Unmarshal(respBody, &watchlist)
|
||||
if err != nil {
|
||||
log.Printf("Failed at unmarshal, %s\n", err)
|
||||
}
|
||||
|
||||
WatchList = watchlist
|
||||
|
||||
return watchlist
|
||||
}
|
||||
|
||||
func (a *App) SimklSyncEpisodes(anime SimklAnime, progress int) interface{} {
|
||||
|
||||
var episodes []Episode
|
||||
var url string
|
||||
@ -101,7 +115,7 @@ func (a *App) SimklSyncEpisodes(anime Anime, progress int) interface{} {
|
||||
|
||||
simklSync := SimklSyncHistoryType{shows}
|
||||
|
||||
respBody := SimklPostHelper(url, simklSync)
|
||||
respBody := SimklHelper("POST", url, simklSync)
|
||||
|
||||
var success interface{}
|
||||
|
||||
@ -113,7 +127,7 @@ func (a *App) SimklSyncEpisodes(anime Anime, progress int) interface{} {
|
||||
return success
|
||||
}
|
||||
|
||||
func (a *App) SimklSyncRating(anime Anime, rating int) interface{} {
|
||||
func (a *App) SimklSyncRating(anime SimklAnime, rating int) interface{} {
|
||||
var url string
|
||||
var showWithRating = ShowWithRating{
|
||||
Title: anime.Show.Title,
|
||||
@ -148,7 +162,7 @@ func (a *App) SimklSyncRating(anime Anime, rating int) interface{} {
|
||||
Shows []interface{} `json:"shows" ts_type:"shows"`
|
||||
}{shows}
|
||||
|
||||
respBody := SimklPostHelper(url, simklSync)
|
||||
respBody := SimklHelper("POST", url, simklSync)
|
||||
|
||||
var success interface{}
|
||||
|
||||
@ -160,7 +174,7 @@ func (a *App) SimklSyncRating(anime Anime, rating int) interface{} {
|
||||
return success
|
||||
}
|
||||
|
||||
func (a *App) SimklSyncStatus(anime Anime, status string) interface{} {
|
||||
func (a *App) SimklSyncStatus(anime SimklAnime, status string) interface{} {
|
||||
url := "https://api.simkl.com/sync/add-to-list"
|
||||
var show = SimklShowStatus{
|
||||
Title: anime.Show.Title,
|
||||
@ -180,7 +194,7 @@ func (a *App) SimklSyncStatus(anime Anime, status string) interface{} {
|
||||
Shows []SimklShowStatus `json:"shows" ts_type:"shows"`
|
||||
}{shows}
|
||||
|
||||
respBody := SimklPostHelper(url, simklSync)
|
||||
respBody := SimklHelper("POST", url, simklSync)
|
||||
|
||||
var success interface{}
|
||||
|
||||
@ -191,3 +205,44 @@ func (a *App) SimklSyncStatus(anime Anime, status string) interface{} {
|
||||
|
||||
return success
|
||||
}
|
||||
|
||||
func (a *App) SimklSearch(aniId int) SimklAnime {
|
||||
fmt.Println(aniId)
|
||||
var result SimklAnime
|
||||
|
||||
if reflect.DeepEqual(WatchList, SimklWatchList{}) {
|
||||
fmt.Println("Watchlist empty. Calling...")
|
||||
WatchList = a.SimklGetUserWatchlist()
|
||||
}
|
||||
|
||||
for _, anime := range WatchList.Anime {
|
||||
id, err := strconv.Atoi(anime.Show.Ids.AniList)
|
||||
if err != nil {
|
||||
fmt.Println("AniList ID does not exist on " + anime.Show.Title)
|
||||
}
|
||||
if id == aniId {
|
||||
result = anime
|
||||
}
|
||||
}
|
||||
|
||||
if reflect.DeepEqual(result, SimklAnime{}) {
|
||||
var anime SimklSearchType
|
||||
url := "https://api.simkl.com/search/id?anilist=" + strconv.Itoa(aniId)
|
||||
|
||||
respBody := SimklHelper("GET", url, nil)
|
||||
|
||||
err := json.Unmarshal(respBody, &anime)
|
||||
if err != nil {
|
||||
log.Printf("Failed at unmarshal, %s\n", err)
|
||||
}
|
||||
|
||||
if len(anime) > 0 {
|
||||
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
|
||||
}
|
||||
|
Reference in New Issue
Block a user