updated bruno files

This commit is contained in:
2024-07-24 09:15:52 -04:00
parent d7233a52ba
commit dcf7322b0c
9 changed files with 428 additions and 85 deletions

View File

@ -1,6 +1,7 @@
package main
package AniList
import (
"AniTrack"
"bytes"
"context"
"encoding/json"
@ -15,25 +16,84 @@ import (
"sync"
)
type JWT struct {
type AniListJWT struct {
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
}
var jwt JWT
type AniListUser struct {
Data struct {
Viewer struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"Viewer"`
} `json:"data"`
}
func AniListQuery(body interface{}, login bool) interface{} {
type AniListCurrentUserWatchList struct {
Data struct {
Page struct {
PageInfo struct {
Total int `json:"total"`
PerPage int `json:"perPage"`
CurrentPage int `json:"currentPage"`
LastPage int `json:"lastPage"`
HasNextPage bool `json:"hasNextPage"`
} `json:"pageInfo"`
MediaList []struct {
ID int `json:"id"`
MediaID int `json:"mediaId"`
Media struct {
ID int `json:"id"`
IDMal int `json:"idMal"`
Title struct {
Romaji string `json:"romaji"`
English string `json:"english"`
Native string `json:"native"`
} `json:"title"`
Description string `json:"description"`
CoverImage struct {
Large string `json:"large"`
} `json:"coverImage"`
Season string `json:"season"`
SeasonYear int `json:"seasonYear"`
Episodes int `json:"episodes"`
} `json:"media"`
Status string `json:"status"`
Notes string `json:"notes"`
Progress int `json:"progress"`
Score int `json:"score"`
Repeat int `json:"repeat"`
User struct {
Statistics struct {
Anime struct {
Count int `json:"count"`
Statuses []struct {
Status string `json:"status"`
Count int `json:"count"`
} `json:"statuses"`
} `json:"anime"`
} `json:"statistics"`
} `json:"user"`
} `json:"mediaList"`
} `json:"Page"`
} `json:"data"`
}
var jwt AniListJWT
func AniListQuery(body interface{}, login bool) (json.RawMessage, string) {
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 {
if login && (AniListJWT{}) != jwt {
response.Header.Add("Authorization", "Bearer "+jwt.AccessToken)
} else if login {
return "Please login to AniList to make this request"
return nil, "Please login to AniList to make this request"
}
response.Header.Add("Content-Type", "application/json")
response.Header.Add("Accept", "application/json")
@ -48,16 +108,17 @@ func AniListQuery(body interface{}, login bool) interface{} {
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
//var post interface{}
//err = json.Unmarshal(returnedBody, &post)
//if err != nil {
// log.Printf("Failed at unmarshal, %s\n", err)
//}
//
//return post
return returnedBody, ""
}
func (a *App) GetAniListItem(aniId int) any {
func (a *main.App) GetAniListItem(aniId int) any {
type Variables struct {
ID int `json:"id"`
ListType string `json:"listType"`
@ -101,10 +162,18 @@ func (a *App) GetAniListItem(aniId int) any {
},
}
return AniListQuery(body, false)
returnedBody, _ := AniListQuery(body, false)
var post interface{}
err := json.Unmarshal(returnedBody, &post)
if err != nil {
log.Printf("Failed at unmarshal, %s\n", err)
}
return post
}
func (a *App) AniListSearch(query string) any {
func (a *main.App) AniListSearch(query string) any {
type Variables struct {
Search string `json:"search"`
ListType string `json:"listType"`
@ -143,12 +212,20 @@ func (a *App) AniListSearch(query string) any {
ListType: "ANIME",
},
}
return AniListQuery(body, false)
returnedBody, _ := AniListQuery(body, false)
var post interface{}
err := json.Unmarshal(returnedBody, &post)
if err != nil {
log.Printf("Failed at unmarshal, %s\n", err)
}
return post
}
var ctxShutdown, cancel = context.WithCancel(context.Background())
func (a *App) AniListLogin() {
func (a *main.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)
@ -195,7 +272,7 @@ func handleAniListCallback(wg *sync.WaitGroup) {
}()
}
func getAniListAuthorizationToken(content string) JWT {
func getAniListAuthorizationToken(content string) AniListJWT {
apiUrl := "https://anilist.co/api/v2/oauth/token"
resource := "/api/v2/oauth/token"
data := url.Values{}
@ -227,7 +304,7 @@ func getAniListAuthorizationToken(content string) JWT {
returnedBody, err := io.ReadAll(res.Body)
var post JWT
var post AniListJWT
err = json.Unmarshal(returnedBody, &post)
if err != nil {
log.Printf("Failed at unmarshal, %s\n", err)
@ -235,3 +312,38 @@ func getAniListAuthorizationToken(content string) JWT {
return post
}
func (a *main.App) GetAniListLoggedInUserId() AniListUser {
if (AniListJWT{}) == jwt {
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)
}
fmt.Println("UserInfo: ", post)
return post
}
func (a *main.App) GetAniListUserWatchingList() {
}