238 lines
5.1 KiB
Go
238 lines
5.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
type JWT struct {
|
|
TokenType string `json:"token_type"`
|
|
ExpiresIn int `json:"expires_in"`
|
|
AccessToken string `json:"access_token"`
|
|
RefreshToken string `json:"refresh_token"`
|
|
}
|
|
|
|
var jwt JWT
|
|
|
|
func AniListQuery(body interface{}, login bool) interface{} {
|
|
reader, _ := json.Marshal(body)
|
|
response, err := http.NewRequest("POST", "https://graphql.anilist.co", bytes.NewBuffer(reader))
|
|
if err != nil {
|
|
log.Printf("Failed at response, %s\n", err)
|
|
}
|
|
if login && (JWT{}) != jwt {
|
|
response.Header.Add("Authorization", "Bearer "+jwt.AccessToken)
|
|
} else if login {
|
|
return "Please login to AniList to make this request"
|
|
}
|
|
response.Header.Add("Content-Type", "application/json")
|
|
response.Header.Add("Accept", "application/json")
|
|
|
|
client := &http.Client{}
|
|
res, reserr := client.Do(response)
|
|
if reserr != nil {
|
|
log.Printf("Failed at res, %s\n", err)
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
returnedBody, err := io.ReadAll(res.Body)
|
|
|
|
var post interface{}
|
|
err = json.Unmarshal(returnedBody, &post)
|
|
if err != nil {
|
|
log.Printf("Failed at unmarshal, %s\n", err)
|
|
}
|
|
|
|
return post
|
|
}
|
|
|
|
func (a *App) GetAniListItem(aniId int) any {
|
|
type Variables struct {
|
|
ID int `json:"id"`
|
|
ListType string `json:"listType"`
|
|
}
|
|
body := struct {
|
|
Query string `json:"query"`
|
|
Variables Variables `json:"variables"`
|
|
}{
|
|
Query: `
|
|
query ($id: Int!, $listType: MediaType) {
|
|
Media (id: $id, type: $listType) {
|
|
id
|
|
idMal
|
|
title {
|
|
romaji
|
|
english
|
|
}
|
|
description
|
|
coverImage {
|
|
medium
|
|
large
|
|
extraLarge
|
|
color
|
|
}
|
|
tags {
|
|
id
|
|
name
|
|
description
|
|
category
|
|
rank
|
|
isGeneralSpoiler
|
|
isMediaSpoiler
|
|
isAdult
|
|
}
|
|
}
|
|
}
|
|
`,
|
|
Variables: Variables{
|
|
ID: aniId,
|
|
ListType: "ANIME",
|
|
},
|
|
}
|
|
|
|
return AniListQuery(body, false)
|
|
}
|
|
|
|
func (a *App) AniListSearch(query string) any {
|
|
type Variables struct {
|
|
Search string `json:"search"`
|
|
ListType string `json:"listType"`
|
|
}
|
|
body := struct {
|
|
Query string `json:"query"`
|
|
Variables Variables `json:"variables"`
|
|
}{
|
|
Query: `
|
|
query ($search: String!, $listType: MediaType) {
|
|
Page (page: 1, perPage: 100) {
|
|
pageInfo {
|
|
total
|
|
currentPage
|
|
lastPage
|
|
hasNextPage
|
|
perPage
|
|
}
|
|
media (search: $search, type: $listType) {
|
|
id
|
|
idMal
|
|
title {
|
|
romaji
|
|
english
|
|
}
|
|
coverImage {
|
|
extraLarge
|
|
color
|
|
}
|
|
}
|
|
}
|
|
}
|
|
`,
|
|
Variables: Variables{
|
|
Search: query,
|
|
ListType: "ANIME",
|
|
},
|
|
}
|
|
return AniListQuery(body, false)
|
|
}
|
|
|
|
var ctxShutdown, cancel = context.WithCancel(context.Background())
|
|
|
|
func (a *App) AniListLogin() {
|
|
getAniListCodeUrl := "https://anilist.co/api/v2/oauth/authorize?client_id=" + os.Getenv("ANILIST_APP_ID") + "&redirect_uri=" + os.Getenv("ANILIST_CALLBACK_URI") + "&response_type=code"
|
|
runtime.BrowserOpenURL(a.ctx, getAniListCodeUrl)
|
|
|
|
serverDone := &sync.WaitGroup{}
|
|
serverDone.Add(1)
|
|
handleAniListCallback(serverDone)
|
|
serverDone.Wait()
|
|
}
|
|
|
|
func handleAniListCallback(wg *sync.WaitGroup) {
|
|
srv := &http.Server{Addr: ":6734"}
|
|
http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
|
|
select {
|
|
case <-ctxShutdown.Done():
|
|
fmt.Println("Shutting down...")
|
|
return
|
|
default:
|
|
}
|
|
content := r.FormValue("code")
|
|
|
|
if content != "" {
|
|
jwt = getAniListAuthorizationToken(content)
|
|
fmt.Println("Shutting down...")
|
|
cancel()
|
|
err := srv.Shutdown(context.Background())
|
|
if err != nil {
|
|
log.Println("server.Shutdown:", err)
|
|
}
|
|
} else {
|
|
_, err := fmt.Fprintf(w, "Getting code failed.")
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
|
|
})
|
|
|
|
go func() {
|
|
defer wg.Done()
|
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Fatalf("listen: %s\n", err)
|
|
}
|
|
fmt.Println("Shutting down...")
|
|
}()
|
|
}
|
|
|
|
func getAniListAuthorizationToken(content string) JWT {
|
|
apiUrl := "https://anilist.co/api/v2/oauth/token"
|
|
resource := "/api/v2/oauth/token"
|
|
data := url.Values{}
|
|
data.Set("grant_type", "authorization_code")
|
|
data.Set("client_id", os.Getenv("ANILIST_APP_ID"))
|
|
data.Set("client_secret", os.Getenv("ANILIST_SECRET_TOKEN"))
|
|
data.Set("redirect_uri", os.Getenv("ANILIST_CALLBACK_URI"))
|
|
data.Set("code", content)
|
|
|
|
u, _ := url.ParseRequestURI(apiUrl)
|
|
u.Path = resource
|
|
urlStr := u.String()
|
|
|
|
response, err := http.NewRequest("POST", urlStr, strings.NewReader(data.Encode()))
|
|
if err != nil {
|
|
log.Printf("Failed at response, %s\n", err)
|
|
}
|
|
response.Header.Add("content-type", "application/x-www-form-urlencoded")
|
|
response.Header.Add("Content-Type", "application/json")
|
|
response.Header.Add("Accept", "application/json")
|
|
|
|
client := &http.Client{}
|
|
res, reserr := client.Do(response)
|
|
if reserr != nil {
|
|
log.Printf("Failed at res, %s\n", err)
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
returnedBody, err := io.ReadAll(res.Body)
|
|
|
|
var post JWT
|
|
err = json.Unmarshal(returnedBody, &post)
|
|
if err != nil {
|
|
log.Printf("Failed at unmarshal, %s\n", err)
|
|
}
|
|
|
|
return post
|
|
}
|