added ability to update simkl episode count
This commit is contained in:
		@@ -3,7 +3,6 @@ package main
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
@@ -455,8 +454,6 @@ func (a *App) AniListUpdateEntry(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	returnedBody, _ := AniListQuery(body, true)
 | 
						returnedBody, _ := AniListQuery(body, true)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fmt.Println(string(returnedBody))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var post interface{}
 | 
						var post interface{}
 | 
				
			||||||
	err := json.Unmarshal(returnedBody, &post)
 | 
						err := json.Unmarshal(returnedBody, &post)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"bytes"
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
@@ -37,3 +38,78 @@ func (a *App) SimklGetUserWatchlist() SimklWatchList {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	return watchlist
 | 
						return watchlist
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,7 +27,10 @@ type SimklUser struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type SimklWatchList struct {
 | 
					type SimklWatchList struct {
 | 
				
			||||||
	Anime []struct {
 | 
						Anime []Anime `json:"anime" ts_type:"anime"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Anime struct {
 | 
				
			||||||
	LastWatchedAt         string  `json:"last_watched_at" ts_type:"last_watched_at"`
 | 
						LastWatchedAt         string  `json:"last_watched_at" ts_type:"last_watched_at"`
 | 
				
			||||||
	Status                string  `json:"status" ts_type:"status"`
 | 
						Status                string  `json:"status" ts_type:"status"`
 | 
				
			||||||
	UserRating            float64 `json:"user_rating" ts_type:"user_rating"`
 | 
						UserRating            float64 `json:"user_rating" ts_type:"user_rating"`
 | 
				
			||||||
@@ -64,5 +67,24 @@ type SimklWatchList struct {
 | 
				
			|||||||
		} `json:"ids" ts_type:"ids"`
 | 
							} `json:"ids" ts_type:"ids"`
 | 
				
			||||||
	} `json:"show" ts_type:"show"`
 | 
						} `json:"show" ts_type:"show"`
 | 
				
			||||||
	AnimeType string `json:"anime_type" ts_type:"anime_type"`
 | 
						AnimeType string `json:"anime_type" ts_type:"anime_type"`
 | 
				
			||||||
	} `json:"anime" ts_type:"anime"`
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type SimklSyncHistoryType struct {
 | 
				
			||||||
 | 
						Shows []SimklPostShow `json:"shows" ts_type:"shows"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type SimklPostShow struct {
 | 
				
			||||||
 | 
						Title    string `json:"title" ts_type:"title"`
 | 
				
			||||||
 | 
						Ids      `json:"ids" ts_type:"ids"`
 | 
				
			||||||
 | 
						Episodes []Episode `json:"episodes" ts_type:"episodes"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Episode struct {
 | 
				
			||||||
 | 
						Number int `json:"number" ts_type:"number"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Ids struct {
 | 
				
			||||||
 | 
						Simkl   int    `json:"simkl" ts_type:"simkl"`
 | 
				
			||||||
 | 
						Mal     string `json:"mal" ts_type:"mal"`
 | 
				
			||||||
 | 
						Anilist string `json:"anilist" ts_type:"anilist"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,12 +5,12 @@ meta {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
get {
 | 
					get {
 | 
				
			||||||
  url: https://api.simkl.com/search/id?client_id={{SIMKL_CLIENT_ID}}&mal=52991
 | 
					  url: https://api.simkl.com/search/id?client_id={{SIMKL_CLIENT_ID}}&simkl=2307708
 | 
				
			||||||
  body: none
 | 
					  body: none
 | 
				
			||||||
  auth: none
 | 
					  auth: none
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
params:query {
 | 
					params:query {
 | 
				
			||||||
  client_id: {{SIMKL_CLIENT_ID}}
 | 
					  client_id: {{SIMKL_CLIENT_ID}}
 | 
				
			||||||
  mal: 52991
 | 
					  simkl: 2307708
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,16 +14,23 @@
 | 
				
			|||||||
    import { writable } from 'svelte/store';
 | 
					    import { writable } from 'svelte/store';
 | 
				
			||||||
    import type {SimklAnime} from "./simkl/types/simklTypes";
 | 
					    import type {SimklAnime} from "./simkl/types/simklTypes";
 | 
				
			||||||
    import { get } from 'svelte/store';
 | 
					    import { get } from 'svelte/store';
 | 
				
			||||||
    import {AniListUpdateEntry} from "../wailsjs/go/main/App";
 | 
					    import {AniListUpdateEntry, SimklSyncEpisodes} from "../wailsjs/go/main/App";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const simklWatch = get(simklWatchList);
 | 
					    const simklWatch = get(simklWatchList);
 | 
				
			||||||
    let isAniListLoggedIn: boolean
 | 
					    let isAniListLoggedIn: boolean
 | 
				
			||||||
    let isSimklLoggedIn: boolean
 | 
					    let isSimklLoggedIn: boolean
 | 
				
			||||||
 | 
					    let simklAnimeIndex: number
 | 
				
			||||||
 | 
					    let simklAnime: SimklAnime | undefined = undefined
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    aniListLoggedIn.subscribe((value) => isAniListLoggedIn = value)
 | 
					    aniListLoggedIn.subscribe((value) => isAniListLoggedIn = value)
 | 
				
			||||||
    simklLoggedIn.subscribe((value) => isSimklLoggedIn = value)
 | 
					    simklLoggedIn.subscribe((value) => isSimklLoggedIn = value)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const simklAnime: SimklAnime | undefined = simklWatch.anime.filter((x) => Number(x.show.ids.mal) === aniListAnime.data.MediaList.media.idMal)[0]
 | 
					    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]
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let items = [];
 | 
					    let items = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -204,6 +211,13 @@
 | 
				
			|||||||
           console.log(value)
 | 
					           console.log(value)
 | 
				
			||||||
       })
 | 
					       })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (simklLoggedIn && simklAnime.watched_episodes_count !== values.progress) {
 | 
				
			||||||
 | 
					            await SimklSyncEpisodes(simklAnime, values.progress).then(value => {
 | 
				
			||||||
 | 
					                console.log(value)
 | 
				
			||||||
 | 
					                simklAnime.watched_episodes_count = values.progress
 | 
				
			||||||
 | 
					                simklWatch.anime[simklAnimeIndex].watched_episodes_count = values.progress
 | 
				
			||||||
 | 
					            })
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										2
									
								
								frontend/wailsjs/go/main/App.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								frontend/wailsjs/go/main/App.d.ts
									
									
									
									
										vendored
									
									
								
							@@ -23,3 +23,5 @@ export function GetSimklLoggedInUser():Promise<main.SimklUser>;
 | 
				
			|||||||
export function SimklGetUserWatchlist():Promise<main.SimklWatchList>;
 | 
					export function SimklGetUserWatchlist():Promise<main.SimklWatchList>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function SimklLogin():Promise<void>;
 | 
					export function SimklLogin():Promise<void>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function SimklSyncEpisodes(arg1:main.Anime,arg2:number):Promise<any>;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -45,3 +45,7 @@ export function SimklGetUserWatchlist() {
 | 
				
			|||||||
export function SimklLogin() {
 | 
					export function SimklLogin() {
 | 
				
			||||||
  return window['go']['main']['App']['SimklLogin']();
 | 
					  return window['go']['main']['App']['SimklLogin']();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function SimklSyncEpisodes(arg1, arg2) {
 | 
				
			||||||
 | 
					  return window['go']['main']['App']['SimklSyncEpisodes'](arg1, arg2);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -91,6 +91,36 @@ export namespace main {
 | 
				
			|||||||
		    return a;
 | 
							    return a;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						export class Anime {
 | 
				
			||||||
 | 
						    last_watched_at: last_watched_at;
 | 
				
			||||||
 | 
						    status: status;
 | 
				
			||||||
 | 
						    user_rating: user_rating;
 | 
				
			||||||
 | 
						    last_watched: last_watched;
 | 
				
			||||||
 | 
						    next_to_watch: next_to_watch;
 | 
				
			||||||
 | 
						    watched_episodes_count: watched_episodes_count;
 | 
				
			||||||
 | 
						    total_episodes_count: total_episodes_count;
 | 
				
			||||||
 | 
						    not_aired_episodes_count: not_aired_episodes_count;
 | 
				
			||||||
 | 
						    show: show;
 | 
				
			||||||
 | 
						    anime_type: anime_type;
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						    static createFrom(source: any = {}) {
 | 
				
			||||||
 | 
						        return new Anime(source);
 | 
				
			||||||
 | 
						    }
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						    constructor(source: any = {}) {
 | 
				
			||||||
 | 
						        if ('string' === typeof source) source = JSON.parse(source);
 | 
				
			||||||
 | 
						        this.last_watched_at = source["last_watched_at"];
 | 
				
			||||||
 | 
						        this.status = source["status"];
 | 
				
			||||||
 | 
						        this.user_rating = source["user_rating"];
 | 
				
			||||||
 | 
						        this.last_watched = source["last_watched"];
 | 
				
			||||||
 | 
						        this.next_to_watch = source["next_to_watch"];
 | 
				
			||||||
 | 
						        this.watched_episodes_count = source["watched_episodes_count"];
 | 
				
			||||||
 | 
						        this.total_episodes_count = source["total_episodes_count"];
 | 
				
			||||||
 | 
						        this.not_aired_episodes_count = source["not_aired_episodes_count"];
 | 
				
			||||||
 | 
						        this.show = source["show"];
 | 
				
			||||||
 | 
						        this.anime_type = source["anime_type"];
 | 
				
			||||||
 | 
						    }
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	export class MediaList {
 | 
						export class MediaList {
 | 
				
			||||||
	    id: number;
 | 
						    id: number;
 | 
				
			||||||
	    mediaId: number;
 | 
						    mediaId: number;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user