added information being pulled from anilist
This commit is contained in:
@ -1 +1,170 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/99designs/keyring"
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var jwt AniListJWT
|
||||
|
||||
var ring, _ = keyring.Open(keyring.Config{
|
||||
ServiceName: "AniTrack",
|
||||
})
|
||||
|
||||
var ctxShutdown, cancel = context.WithCancel(context.Background())
|
||||
|
||||
func (a *App) AniListLogin() {
|
||||
if (AniListJWT{}) == jwt {
|
||||
tokenType, err := ring.Get("anilistTokenType")
|
||||
expiresIn, err := ring.Get("anilistTokenExpiresIn")
|
||||
accessToken, err := ring.Get("anilistAccessToken")
|
||||
refreshToken, err := ring.Get("anilistRefreshToken")
|
||||
if err != nil {
|
||||
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()
|
||||
} else {
|
||||
jwt.TokenType = string(tokenType.Data)
|
||||
jwt.AccessToken = string(accessToken.Data)
|
||||
jwt.RefreshToken = string(refreshToken.Data)
|
||||
expiresInString := string(expiresIn.Data)
|
||||
jwt.ExpiresIn, _ = strconv.Atoi(expiresInString)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
_ = ring.Set(keyring.Item{
|
||||
Key: "anilistTokenType",
|
||||
Data: []byte(jwt.TokenType),
|
||||
})
|
||||
_ = ring.Set(keyring.Item{
|
||||
Key: "anilistTokenExpiresIn",
|
||||
Data: []byte(string(jwt.ExpiresIn)),
|
||||
})
|
||||
_ = ring.Set(keyring.Item{
|
||||
Key: "anilistAccessToken",
|
||||
Data: []byte(jwt.AccessToken),
|
||||
})
|
||||
_ = ring.Set(keyring.Item{
|
||||
Key: "anilistRefreshToken",
|
||||
Data: []byte(jwt.RefreshToken),
|
||||
})
|
||||
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) AniListJWT {
|
||||
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 AniListJWT
|
||||
err = json.Unmarshal(returnedBody, &post)
|
||||
if err != nil {
|
||||
log.Printf("Failed at unmarshal, %s\n", err)
|
||||
}
|
||||
|
||||
return post
|
||||
}
|
||||
|
||||
func (a *App) GetAniListLoggedInUserId() AniListUser {
|
||||
a.AniListLogin()
|
||||
body := struct {
|
||||
Query string `json:"query"`
|
||||
}{
|
||||
Query: `
|
||||
query {
|
||||
Viewer {
|
||||
id
|
||||
name
|
||||
}
|
||||
}
|
||||
`,
|
||||
}
|
||||
|
||||
user, _ := AniListQuery(body, true)
|
||||
|
||||
var post AniListUser
|
||||
err := json.Unmarshal(user, &post)
|
||||
if err != nil {
|
||||
log.Printf("Failed at unmarshal, %s\n", err)
|
||||
}
|
||||
|
||||
return post
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user