From dcf7322b0c3336782afa6793ea77b4a09a02a7fb Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Wed, 24 Jul 2024 09:15:52 -0400 Subject: [PATCH] updated bruno files --- AniListFunctions.go | 152 +++++++++++++++--- AniListTypes.go | 1 + AniListUserFunctions.go | 1 + bruno/AniTrack/AniList Item.bru | 26 +-- .../AniTrack/AniList MediaList User Query.bru | 103 ++++++++++++ bruno/AniTrack/AniList MediaList User.bru | 28 ++++ frontend/src/AniListTypes.ts | 55 ------- .../types/AniListCurrentUserWatchListType.ts | 0 frontend/src/anilist/types/AniListTypes.ts | 147 +++++++++++++++++ 9 files changed, 428 insertions(+), 85 deletions(-) create mode 100644 AniListTypes.go create mode 100644 AniListUserFunctions.go create mode 100644 bruno/AniTrack/AniList MediaList User Query.bru create mode 100644 bruno/AniTrack/AniList MediaList User.bru delete mode 100644 frontend/src/AniListTypes.ts create mode 100644 frontend/src/anilist/types/AniListCurrentUserWatchListType.ts create mode 100644 frontend/src/anilist/types/AniListTypes.ts diff --git a/AniListFunctions.go b/AniListFunctions.go index fbf374c..5b47fe4 100644 --- a/AniListFunctions.go +++ b/AniListFunctions.go @@ -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() { + +} diff --git a/AniListTypes.go b/AniListTypes.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/AniListTypes.go @@ -0,0 +1 @@ +package main diff --git a/AniListUserFunctions.go b/AniListUserFunctions.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/AniListUserFunctions.go @@ -0,0 +1 @@ +package main diff --git a/bruno/AniTrack/AniList Item.bru b/bruno/AniTrack/AniList Item.bru index 8f39336..b199c71 100644 --- a/bruno/AniTrack/AniList Item.bru +++ b/bruno/AniTrack/AniList Item.bru @@ -29,16 +29,6 @@ body:graphql { coverImage { large } - tags { - id - name - description - category - rank - isGeneralSpoiler - isMediaSpoiler - isAdult - } } } @@ -50,3 +40,19 @@ body:graphql:vars { "listType": "ANIME" } } + +docs { + Title + + Image + + Description + + Episodes + + Status + + Season + + External & Streaming Links +} diff --git a/bruno/AniTrack/AniList MediaList User Query.bru b/bruno/AniTrack/AniList MediaList User Query.bru new file mode 100644 index 0000000..783cd53 --- /dev/null +++ b/bruno/AniTrack/AniList MediaList User Query.bru @@ -0,0 +1,103 @@ +meta { + name: AniList MediaList User Query + type: graphql + seq: 6 +} + +post { + url: https://graphql.anilist.co + body: graphql + auth: none +} + +headers { + Content-Type: "application/json" + Accept: "application/json" +} + +body:graphql { + # Write your query or mutation here + query( + $page: Int + $perPage: Int + $userId: Int + $listType: MediaType + $status: MediaListStatus + ) { + Page(page: $page, perPage: $perPage) { + pageInfo { + total + perPage + currentPage + lastPage + hasNextPage + } + mediaList(userId: $userId, type: $listType, status: $status) { + id + mediaId + userId + media { + id + idMal + title { + romaji + english + native + } + description + coverImage { + large + } + season + seasonYear + episodes + } + status + notes + progress + score + repeat + user { + id + statistics { + anime { + count + statuses { + status + count + } + } + } + } + } + } + } + +} + +body:graphql:vars { + { + "page": 1, + "perPage": 20, + "userId": 413504, + "listType": "ANIME", + "status": "CURRENT" + } +} + +docs { + Title + Image + Description + Episodes + Status + Season + External & Streaming Links + + User Stuff Per Item + Status + Score + Episode Progress + Total Rewatches + Notes +} diff --git a/bruno/AniTrack/AniList MediaList User.bru b/bruno/AniTrack/AniList MediaList User.bru new file mode 100644 index 0000000..29c028d --- /dev/null +++ b/bruno/AniTrack/AniList MediaList User.bru @@ -0,0 +1,28 @@ +meta { + name: AniList MediaList User + type: http + seq: 5 +} + +post { + url: https://graphql.anilist.co + body: none + auth: none +} + +docs { + Title + Image + Description + Episodes + Status + Season + External & Streaming Links + + User Stuff Per Item + Status + Score + Episode Progress + Total Rewatches + Notes +} diff --git a/frontend/src/AniListTypes.ts b/frontend/src/AniListTypes.ts deleted file mode 100644 index 3ca3d6b..0000000 --- a/frontend/src/AniListTypes.ts +++ /dev/null @@ -1,55 +0,0 @@ -export interface AniListItem { - data: { - Media: { - id: number, - idMal: number, - title: { - romaji: string, - english: string, - }, - description: string, - coverImage: { - medium: string, - large: string, - extraLarge: string, - color: string, - }, - tags: [{ - id: number, - name: string, - description: string, - category: string, - rank: number, - isGeneralSpoiler: boolean, - isMediaSpoiler: boolean, - isAdult: boolean - }] - } - } -} - -export interface AniSearchList { - data: { - Page: { - pageInfo: { - total: number, - currentPage: number, - lastPage: number, - hasNextPage: boolean, - perPage: number - }, - media: [{ - id: number, - idMal: number, - title: { - romaji: string, - english: string, - }, - coverImage: { - extraLarge: string, - color: string, - }, - }], - }, - } -} \ No newline at end of file diff --git a/frontend/src/anilist/types/AniListCurrentUserWatchListType.ts b/frontend/src/anilist/types/AniListCurrentUserWatchListType.ts new file mode 100644 index 0000000..e69de29 diff --git a/frontend/src/anilist/types/AniListTypes.ts b/frontend/src/anilist/types/AniListTypes.ts new file mode 100644 index 0000000..d441ddc --- /dev/null +++ b/frontend/src/anilist/types/AniListTypes.ts @@ -0,0 +1,147 @@ +export interface AniListItem { + data: { + Media: { + id: number, + idMal: number, + title: { + romaji: string, + english: string, + }, + description: string, + coverImage: { + medium: string, + large: string, + extraLarge: string, + color: string, + }, + tags: [{ + id: number, + name: string, + description: string, + category: string, + rank: number, + isGeneralSpoiler: boolean, + isMediaSpoiler: boolean, + isAdult: boolean + }] + } + } +} + +export interface AniSearchList { + data: { + Page: { + pageInfo: { + total: number, + currentPage: number, + lastPage: number, + hasNextPage: boolean, + perPage: number + }, + media: [{ + id: number, + idMal: number, + title: { + romaji: string, + english: string, + native: string, + }, + coverImage: { + extraLarge: string, + color: string, + }, + }], + }, + } +} + +export interface AniListUser { + "data": { + "Viewer": { + id: number, + name: string, + } + } +} + +export interface AniListCurrentUserWatchList { + data: Data; +} + +export interface Data { + Page: Page; +} + +export interface Page { + pageInfo: PageInfo; + mediaList: MediaList[]; +} + +export interface MediaList { + id: number; + mediaId: number; + userId: number; + media: Media; + status: StatusEnum; + notes: string | null; + progress: number; + score: number; + repeat: number; + user: User; +} + +export interface Media { + id: number; + idMal: number; + title: Title; + description: string; + coverImage: CoverImage; + season: string; + seasonYear: number; + episodes: number; +} + +export interface CoverImage { + large: string; +} + +export interface Title { + romaji: string | null; + english: string | null; + native: string; +} + +export enum StatusEnum { + Completed = "COMPLETED", + Current = "CURRENT", + Dropped = "DROPPED", + Planning = "PLANNING", + Repeating = "REPEATING", +} + +export interface User { + id: number; + statistics: Statistics; +} + +export interface Statistics { + anime: Anime; +} + +export interface Anime { + count: number; + statuses: StatusElement[]; +} + +export interface StatusElement { + status: StatusEnum; + count: number; +} + +export interface PageInfo { + total: number; + perPage: number; + currentPage: number; + lastPage: number; + hasNextPage: boolean; +}