1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
import type { AniListAuthorisation, UserIdentity } from './identity';
export interface WrappedMediaFormat {
startYears: {
startYear: number;
minutesWatched: number;
count: number;
};
genres: {
meanScore: number;
minutesWatched: number;
chaptersRead: number;
genre: string;
mediaIds: number[];
}[];
tags: {
meanScore: number;
minutesWatched: number;
chaptersRead: number;
tag: {
name: string;
};
mediaIds: number[];
}[];
}
export interface Wrapped {
statistics: {
anime: WrappedMediaFormat;
manga: WrappedMediaFormat;
};
activities: {
statusCount: number;
messageCount: number;
};
avatar: {
large: string;
};
}
const profileActivities = async (user: AniListAuthorisation, identity: UserIdentity) => {
const get = async (page: number) =>
await (
await fetch('https://graphql.anilist.co', {
method: 'POST',
headers: {
Authorization: `${user.tokenType} ${user.accessToken}`,
'Content-Type': 'application/json',
Accept: 'application/json'
},
body: JSON.stringify({
query: `{
Page(page: ${page}) {
activities(userId: ${identity.id}, type_in: [ TEXT, MESSAGE ]) {
... on TextActivity {
type
createdAt
}
... on MessageActivity {
type
createdAt
}
}
pageInfo {
hasNextPage
}
}
}`
})
})
).json();
const pages = [];
let page = 1;
let response = await get(page);
pages.push(response['data']['Page']['activities']);
while (response['data']['Page']['pageInfo']['hasNextPage']) {
page += 1;
response = await get(page);
pages.push(response['data']['Page']['activities']);
}
return {
statusCount: pages
.flat()
.filter(
(activity) =>
activity.type == 'TEXT' && activity.createdAt > Math.floor(Date.now() / 1000) - 31556952
).length,
messageCount: pages
.flat()
.filter(
(activity) =>
activity.type == 'MESSAGE' &&
activity.createdAt > Math.floor(Date.now() / 1000) - 31556952
).length
};
};
export const wrapped = async (
anilistAuthorisation: AniListAuthorisation,
identity: UserIdentity
): Promise<Wrapped> => {
const wrappedResponse = await (
await fetch('https://graphql.anilist.co', {
method: 'POST',
headers: {
Authorization: `${anilistAuthorisation.tokenType} ${anilistAuthorisation.accessToken}`,
'Content-Type': 'application/json',
Accept: 'application/json'
},
body: JSON.stringify({
query: `{
User(name: "${identity.name}") {
avatar { large }
statistics {
anime {
startYears { startYear minutesWatched count }
genres(sort: [ MEAN_SCORE_DESC ]) { meanScore minutesWatched chaptersRead genre mediaIds }
tags(sort: [ MEAN_SCORE_DESC ]) { meanScore minutesWatched chaptersRead tag { name } mediaIds }
}
manga {
startYears { startYear chaptersRead count }
genres(sort: [ MEAN_SCORE_DESC ]) { meanScore minutesWatched chaptersRead genre mediaIds }
tags(sort: [ MEAN_SCORE_DESC ]) { meanScore minutesWatched chaptersRead tag { name } mediaIds }
}
}
}
}`
})
})
).json();
const { statusCount, messageCount } = await profileActivities(anilistAuthorisation, identity);
return {
statistics: wrappedResponse['data']['User']['statistics'],
activities: {
statusCount,
messageCount
},
avatar: wrappedResponse['data']['User']['avatar']
};
};
|