2024-07-24 09:18:45 -04:00
|
|
|
package main
|
2024-07-21 10:25:21 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2024-07-24 09:15:52 -04:00
|
|
|
func AniListQuery(body interface{}, login bool) (json.RawMessage, string) {
|
2024-07-21 10:25:21 -04:00
|
|
|
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)
|
|
|
|
}
|
2024-07-24 09:15:52 -04:00
|
|
|
if login && (AniListJWT{}) != jwt {
|
2024-07-21 10:25:21 -04:00
|
|
|
response.Header.Add("Authorization", "Bearer "+jwt.AccessToken)
|
|
|
|
} else if login {
|
2024-07-24 09:18:45 -04:00
|
|
|
return nil, "Please login to anilist to make this request"
|
2024-07-21 10:25:21 -04:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
|
2024-07-24 09:15:52 -04:00
|
|
|
return returnedBody, ""
|
2024-07-21 10:25:21 -04:00
|
|
|
}
|
|
|
|
|
2024-07-24 09:18:45 -04:00
|
|
|
func (a *App) GetAniListItem(aniId int) any {
|
2024-07-21 10:25:21 -04:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-07-24 09:15:52 -04:00
|
|
|
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
|
2024-07-21 10:25:21 -04:00
|
|
|
}
|
|
|
|
|
2024-07-24 09:18:45 -04:00
|
|
|
func (a *App) AniListSearch(query string) any {
|
2024-07-21 10:25:21 -04:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
2024-07-24 09:15:52 -04:00
|
|
|
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
|
2024-07-21 10:25:21 -04:00
|
|
|
}
|
|
|
|
|
2024-07-24 09:18:45 -04:00
|
|
|
func (a *App) GetAniListUserWatchingList(page int, perPage int, sort string) AniListCurrentUserWatchList {
|
|
|
|
var user = a.GetAniListLoggedInUserId()
|
|
|
|
type Variables struct {
|
|
|
|
Page int `json:"page"`
|
|
|
|
PerPage int `json:"perPage"`
|
|
|
|
UserId int `json:"userId"`
|
|
|
|
ListType string `json:"listType"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Sort string `json:"sort"`
|
2024-07-24 09:15:52 -04:00
|
|
|
}
|
|
|
|
body := struct {
|
2024-07-24 09:18:45 -04:00
|
|
|
Query string `json:"query"`
|
|
|
|
Variables Variables `json:"variables"`
|
2024-07-24 09:15:52 -04:00
|
|
|
}{
|
|
|
|
Query: `
|
2024-07-24 09:18:45 -04:00
|
|
|
query(
|
|
|
|
$page: Int
|
|
|
|
$perPage: Int
|
|
|
|
$userId: Int
|
|
|
|
$listType: MediaType
|
|
|
|
$status: MediaListStatus
|
|
|
|
$sort:[MediaListSort]
|
|
|
|
) {
|
|
|
|
Page(page: $page, perPage: $perPage) {
|
|
|
|
pageInfo {
|
|
|
|
total
|
|
|
|
perPage
|
|
|
|
currentPage
|
|
|
|
lastPage
|
|
|
|
hasNextPage
|
|
|
|
}
|
|
|
|
mediaList(userId: $userId, type: $listType, status: $status, sort: $sort) {
|
|
|
|
id
|
|
|
|
mediaId
|
|
|
|
userId
|
|
|
|
media {
|
|
|
|
id
|
|
|
|
idMal
|
|
|
|
title {
|
|
|
|
romaji
|
|
|
|
english
|
|
|
|
native
|
|
|
|
}
|
|
|
|
description
|
|
|
|
coverImage {
|
|
|
|
large
|
|
|
|
}
|
|
|
|
season
|
|
|
|
seasonYear
|
|
|
|
status
|
|
|
|
episodes
|
|
|
|
nextAiringEpisode{
|
|
|
|
airingAt
|
|
|
|
timeUntilAiring
|
|
|
|
episode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status
|
|
|
|
notes
|
|
|
|
progress
|
|
|
|
score
|
|
|
|
repeat
|
|
|
|
user {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
avatar{
|
|
|
|
large
|
|
|
|
medium
|
|
|
|
}
|
|
|
|
statistics {
|
|
|
|
anime {
|
|
|
|
count
|
|
|
|
statuses {
|
|
|
|
status
|
|
|
|
count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-24 09:15:52 -04:00
|
|
|
`,
|
2024-07-24 09:18:45 -04:00
|
|
|
Variables: Variables{
|
|
|
|
Page: page,
|
|
|
|
PerPage: perPage,
|
|
|
|
UserId: user.Data.Viewer.ID,
|
|
|
|
ListType: "ANIME",
|
|
|
|
Status: "CURRENT",
|
|
|
|
Sort: sort,
|
|
|
|
},
|
2024-07-24 09:15:52 -04:00
|
|
|
}
|
|
|
|
|
2024-07-24 09:18:45 -04:00
|
|
|
returnedBody, _ := AniListQuery(body, true)
|
2024-07-24 09:15:52 -04:00
|
|
|
|
2024-07-24 09:18:45 -04:00
|
|
|
var post AniListCurrentUserWatchList
|
|
|
|
err := json.Unmarshal(returnedBody, &post)
|
2024-07-24 09:15:52 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed at unmarshal, %s\n", err)
|
|
|
|
}
|
|
|
|
|
2024-07-24 09:18:45 -04:00
|
|
|
// Getting the real total, finding the real last page and storing that in the Page info
|
|
|
|
statuses := post.Data.Page.MediaList[0].User.Statistics.Anime.Statuses
|
|
|
|
var total int
|
|
|
|
for _, status := range statuses {
|
|
|
|
if status.Status == "CURRENT" {
|
|
|
|
total = status.Count
|
|
|
|
}
|
|
|
|
}
|
2024-07-24 09:15:52 -04:00
|
|
|
|
2024-07-24 09:18:45 -04:00
|
|
|
lastPage := total / perPage
|
2024-07-24 09:15:52 -04:00
|
|
|
|
2024-07-24 09:18:45 -04:00
|
|
|
post.Data.Page.PageInfo.Total = total
|
|
|
|
post.Data.Page.PageInfo.LastPage = lastPage
|
2024-07-24 09:15:52 -04:00
|
|
|
|
2024-07-24 09:18:45 -04:00
|
|
|
return post
|
2024-07-24 09:15:52 -04:00
|
|
|
}
|