From 4d9012b43ca20f18ada136b30deac2b7b8f36474 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Sun, 18 Aug 2024 17:23:09 -0400 Subject: [PATCH] Fixed Simkl Bugs --- SimklFunctions.go | 129 +++++++++++++----- SimklTypes.go | 18 ++- SimklUserFunctions.go | 20 ++- bruno/AniTrack/MAL/Get Single Anime.bru | 2 +- .../Simkl/Get Items/Search By MalID.bru | 16 --- frontend/src/App.svelte | 52 ++++--- frontend/src/ChangeDataDialogue.svelte | 54 ++++---- .../GlobalVariablesAndHelperFunctions.svelte | 56 +++++--- frontend/src/Search.svelte | 1 - frontend/src/simkl/types/simklTypes.ts | 17 ++- frontend/wailsjs/go/main/App.d.ts | 8 +- frontend/wailsjs/go/main/App.js | 4 + frontend/wailsjs/go/models.ts | 60 ++++---- 13 files changed, 280 insertions(+), 157 deletions(-) delete mode 100644 bruno/AniTrack/Simkl/Get Items/Search By MalID.bru diff --git a/SimklFunctions.go b/SimklFunctions.go index 633f38f..082d33b 100644 --- a/SimklFunctions.go +++ b/SimklFunctions.go @@ -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 +} diff --git a/SimklTypes.go b/SimklTypes.go index 25a549e..4cb794e 100644 --- a/SimklTypes.go +++ b/SimklTypes.go @@ -27,10 +27,10 @@ type SimklUser struct { } type SimklWatchList struct { - Anime []Anime `json:"anime" ts_type:"anime"` + Anime []SimklAnime `json:"anime" ts_type:"anime"` } -type Anime struct { +type SimklAnime struct { LastWatchedAt string `json:"last_watched_at" ts_type:"last_watched_at"` Status string `json:"status" ts_type:"status"` UserRating float64 `json:"user_rating" ts_type:"user_rating"` @@ -105,3 +105,17 @@ type SimklShowStatus struct { Ids `json:"ids" ts_type:"ids"` To string `json:"to" ts_type:"to"` } + +type SimklSearchType []struct { + Type string `json:"type"` + Title string `json:"title"` + Poster string `json:"poster"` + Year int `json:"year"` + Status string `json:"status"` + Ids struct { + Simkl int `json:"simkl"` + Slug string `json:"slug"` + } `json:"ids"` + TotalEpisodes int `json:"total_episodes"` + AnimeType string `json:"anime_type"` +} diff --git a/SimklUserFunctions.go b/SimklUserFunctions.go index f551687..24207dd 100644 --- a/SimklUserFunctions.go +++ b/SimklUserFunctions.go @@ -181,10 +181,26 @@ func (a *App) GetSimklLoggedInUser() SimklUser { defer response.Body.Close() - var user SimklUser - respBody, _ := io.ReadAll(response.Body) + 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 SimklUser{} + } + + var user SimklUser + err = json.Unmarshal(respBody, &user) if err != nil { log.Printf("Failed at unmarshal, %s\n", err) diff --git a/bruno/AniTrack/MAL/Get Single Anime.bru b/bruno/AniTrack/MAL/Get Single Anime.bru index 64f30c8..7ec6495 100644 --- a/bruno/AniTrack/MAL/Get Single Anime.bru +++ b/bruno/AniTrack/MAL/Get Single Anime.bru @@ -5,7 +5,7 @@ meta { } get { - url: https://api.myanimelist.net/v2/anime/52991?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 + url: https://api.myanimelist.net/v2/anime/53580?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 body: none auth: bearer } diff --git a/bruno/AniTrack/Simkl/Get Items/Search By MalID.bru b/bruno/AniTrack/Simkl/Get Items/Search By MalID.bru deleted file mode 100644 index d10d297..0000000 --- a/bruno/AniTrack/Simkl/Get Items/Search By MalID.bru +++ /dev/null @@ -1,16 +0,0 @@ -meta { - name: Search By MalID - type: http - seq: 1 -} - -get { - url: https://api.simkl.com/search/id?client_id={{SIMKL_CLIENT_ID}}&simkl=2307708 - body: none - auth: none -} - -params:query { - client_id: {{SIMKL_CLIENT_ID}} - simkl: 2307708 -} diff --git a/frontend/src/App.svelte b/frontend/src/App.svelte index 8021c35..6bb1ec9 100644 --- a/frontend/src/App.svelte +++ b/frontend/src/App.svelte @@ -4,28 +4,30 @@ anilistModal, aniListPrimary, aniListUser, - malUser, - malLoggedIn, aniListWatchlist, + animePerPage, GetAniListSingleItemAndOpenModal, + malLoggedIn, + malPrimary, + malUser, + malWatchList, simklLoggedIn, simklUser, simklWatchList, title, watchListPage, - animePerPage, - malWatchList, - malPrimary + simklPrimary, } from "./GlobalVariablesAndHelperFunctions.svelte"; import { CheckIfAniListLoggedIn, - CheckIfSimklLoggedIn, CheckIfMyAnimeListLoggedIn, + CheckIfSimklLoggedIn, GetAniListLoggedInUser, GetAniListUserWatchingList, + GetMyAnimeList, + GetMyAnimeListLoggedInUser, GetSimklLoggedInUser, SimklGetUserWatchlist, - GetMyAnimeListLoggedInUser, GetMyAnimeList, } from "../wailsjs/go/main/App"; import {MediaListSort} from "./anilist/types/AniListTypes"; import type {AniListCurrentUserWatchList} from "./anilist/types/AniListCurrentUserWatchListType" @@ -40,12 +42,14 @@ let isAniListLoggedIn: boolean let isAniListPrimary: boolean let isMalPrimary: boolean + let isSimklPrimary: boolean let aniListWatchListLoaded: AniListCurrentUserWatchList aniListLoggedIn.subscribe((value) => isAniListLoggedIn = value) aniListPrimary.subscribe((value) => isAniListPrimary = value) aniListWatchlist.subscribe((value) => aniListWatchListLoaded = value) malPrimary.subscribe((value) => isMalPrimary = value) + simklPrimary.subscribe(value => isSimklPrimary = value) let page: number @@ -75,7 +79,7 @@ if (loggedIn) { GetMyAnimeListLoggedInUser().then(user => { malUser.set(user) - if (isMalPrimary){ + if (isMalPrimary) { GetMyAnimeList(1000).then(watchList => { malWatchList.set(watchList) malLoggedIn.set(loggedIn) @@ -90,11 +94,19 @@ await CheckIfSimklLoggedIn().then(loggedIn => { if (loggedIn) { GetSimklLoggedInUser().then(user => { - simklUser.set(user) - SimklGetUserWatchlist().then(result => { - simklWatchList.set(result) - simklLoggedIn.set(loggedIn) - }) + if (Object.keys(user).length === 0) { + simklLoggedIn.set(false) + } else { + simklUser.set(user) + if(isSimklPrimary) { + SimklGetUserWatchlist().then(result => { + simklWatchList.set(result) + simklLoggedIn.set(loggedIn) + }) + } else { + simklLoggedIn.set(loggedIn) + } + } }) } }) @@ -107,15 +119,19 @@ {#if isAniListLoggedIn}

Your AniList WatchList

- +
{#each aniListWatchListLoaded.data.Page.mediaList as media} @@ -153,7 +169,7 @@ {/each}
- +
{/if} diff --git a/frontend/src/ChangeDataDialogue.svelte b/frontend/src/ChangeDataDialogue.svelte index 7c69513..01915cb 100644 --- a/frontend/src/ChangeDataDialogue.svelte +++ b/frontend/src/ChangeDataDialogue.svelte @@ -5,7 +5,8 @@ aniListLoggedIn, simklLoggedIn, malLoggedIn, - malAnime + malAnime, + simklAnime, } from "./GlobalVariablesAndHelperFunctions.svelte"; import {aniListAnime} from "./GlobalVariablesAndHelperFunctions.svelte"; import {Button} from "flowbite-svelte"; @@ -14,8 +15,7 @@ import moment from 'moment' import { Table, TableBody, TableBodyCell, TableBodyRow, TableHead, TableHeadCell } from 'flowbite-svelte'; import { writable } from 'svelte/store'; - import type {SimklAnime} from "./simkl/types/simklTypes"; - import { get } from 'svelte/store'; + import type {SimklAnime, SimklWatchList} from "./simkl/types/simklTypes"; import { AniListUpdateEntry, MyAnimeListUpdate, SimklSyncEpisodes, @@ -25,13 +25,12 @@ import type {MALAnime, MALUploadStatus, MyListStatus} from "./mal/types/MALTypes"; import type {AniListUpdateVariables} from "./anilist/types/AniListTypes"; - const simklWatch = get(simklWatchList); + let simklWatch: SimklWatchList let isAniListLoggedIn: boolean let isMalLoggedIn: boolean let isSimklLoggedIn: boolean - let simklAnimeIndex: number let currentMalAnime: MALAnime - let simklAnime: SimklAnime | undefined = undefined + let currentSimklAnime: SimklAnime let submitting = writable(false) let isSubmitting: boolean @@ -40,13 +39,8 @@ simklLoggedIn.subscribe((value) => isSimklLoggedIn = value) malAnime.subscribe((value) => currentMalAnime = value) submitting.subscribe((value) => isSubmitting = value) - - for (let i = 0; i < simklWatch.anime.length; i++) { - if (Number(simklWatch.anime[i].show.ids.mal) === aniListAnime.data.MediaList.media.idMal) { - simklAnimeIndex = i - simklAnime = simklWatch.anime[i] - } - } + simklWatchList.subscribe((value) => simklWatch = value) + simklAnime.subscribe((value) => currentSimklAnime = value) type statusOption = { id: number, @@ -108,15 +102,15 @@ }) } - if(isSimklLoggedIn && simklAnime !== undefined) { + if(isSimklLoggedIn && Object.keys(currentSimklAnime).length > 0) { items.push({ - id: simklAnime.show.ids.simkl, + id: currentSimklAnime.show.ids.simkl, service: "Simkl", - progress: simklAnime.watched_episodes_count, - status: simklAnime.status, + progress: currentSimklAnime.watched_episodes_count, + status: currentSimklAnime.status, startedAt: "", completedAt: "", - score: simklAnime.user_rating, + score: currentSimklAnime.user_rating, repeat: 0, notes: "" }) @@ -299,24 +293,24 @@ } if (simklLoggedIn) { - if (simklAnime.watched_episodes_count !== values.progress) { - await SimklSyncEpisodes(simklAnime, values.progress).then(() => { - simklAnime.watched_episodes_count = values.progress - simklWatch.anime[simklAnimeIndex].watched_episodes_count = values.progress + if (currentSimklAnime.watched_episodes_count !== values.progress) { + await SimklSyncEpisodes(currentSimklAnime, values.progress).then(() => { + currentSimklAnime.watched_episodes_count = values.progress + simklWatch.anime[simklWatch.currentIndex].watched_episodes_count = values.progress }) } - if (simklAnime.user_rating !== values.score) { - await SimklSyncRating(simklAnime, values.score).then(() => { - simklAnime.user_rating = values.score - simklWatch.anime[simklAnimeIndex].user_rating = values.score + if (currentSimklAnime.user_rating !== values.score) { + await SimklSyncRating(currentSimklAnime, values.score).then(() => { + currentSimklAnime.user_rating = values.score + simklWatch.anime[simklWatch.currentIndex].user_rating = values.score }) } - if (simklAnime.status !== values.status.simkl) { - await SimklSyncStatus(simklAnime, values.status.simkl).then(() => { - simklAnime.status = values.status.simkl - simklWatch.anime[simklAnimeIndex].status = values.status.simkl + if (currentSimklAnime.status !== values.status.simkl) { + await SimklSyncStatus(currentSimklAnime, values.status.simkl).then(() => { + currentSimklAnime.status = values.status.simkl + simklWatch.anime[simklWatch.currentIndex].status = values.status.simkl }) } } diff --git a/frontend/src/GlobalVariablesAndHelperFunctions.svelte b/frontend/src/GlobalVariablesAndHelperFunctions.svelte index 26242b4..62d7462 100644 --- a/frontend/src/GlobalVariablesAndHelperFunctions.svelte +++ b/frontend/src/GlobalVariablesAndHelperFunctions.svelte @@ -1,15 +1,23 @@