-
Notifications
You must be signed in to change notification settings - Fork 0
/
themes.moe-updater.user.js
285 lines (266 loc) · 11.7 KB
/
themes.moe-updater.user.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// ==UserScript==
// @name themes.moe updater
// @namespace https://github.com/pepeloni-away
// @author pploni
// @run-at document-start
// @insert-into page
// @version 1.0
// @description 5/3/2024, 7:21:59 PM
// @grant GM_xmlhttpRequest
// @match https://themes.moe/*
// ==/UserScript==
let cachedEmulatedResponse = {}
XMLHttpRequest.prototype.open = new Proxy(XMLHttpRequest.prototype.open, {
apply(target, thisArg, args) {
if (location.href !== 'https://themes.moe/' && args[0] === 'GET' && (args[1].startsWith('/api/mal/') || args[1].startsWith('/api/anilist/'))) {
const name = args[1].replace(/.*\//, '')
thisArg._ogsend = thisArg.send.bind(thisArg)
thisArg.send = function() {
// console.log('send', thisArg.ogsend, name)
args[1].startsWith('/api/mal/') ? fetchMalList(name, thisArg) : fetchAnilistList(name, thisArg)
}
thisArg.addEventListener('readystatechange', function(e) {
if (this.readyState === 4 && this._emulatedResponse) {
// console.log(this, this.response, this.responseText)
this._ogresponse = JSON.parse(this.response)
Object.defineProperty(this, 'response', {
writable: true
})
Object.defineProperty(this, 'responseText', {
writable: true
})
this.response = JSON.stringify(this._emulatedResponse)
this.responseText = JSON.stringify(this._emulatedResponse)
}
})
}
if (args[0] === 'GET' && args[1].startsWith('/api/themes/') && (location.href.includes('mal') || location.href.includes('anilist'))) {
thisArg.addEventListener('readystatechange', function(e) {
if (this.readyState === 4) {
const theme = args[1].match(/(?<=\/)[^\/]+(?=\/mirrors)/)[0]
const malID = args[1].match(/(?<=api\/themes\/)\d+/)[0]
const infoObj = cachedEmulatedResponse.find(m => m.malID === parseInt(malID))
const themeInfo = infoObj.themes.find(m => m.themeType === theme)
const ogresponse = JSON.parse(this.response)
ogresponse.themeType = themeInfo.themeType
ogresponse.themeName = themeInfo.themeName
ogresponse.mirrors.push(themeInfo.mirror)
Object.defineProperty(this, 'response', {
writable: true
})
Object.defineProperty(this, 'responseText', {
writable: true
})
this.responseText = JSON.stringify(ogresponse)
this.response = JSON.stringify(ogresponse)
}
})
}
return Reflect.apply(...arguments)
}
})
function fetchMalList(name, xhr, fullResponse = [], nextUrl = '') {
// https://myanimelist.net/apiconfig/references/api/v2#operation/users_user_id_animelist_get
// https://github.com/SuperMarcus/myanimelist-api-specification
const url = nextUrl || `https://api.myanimelist.net/v2/users/${name}/animelist?fields=list_status&nsfw=true&sort=anime_title&limit=300`
const headers = {
'X-MAL-Client-ID': '6114d00ca681b7701d1e15fe11a4987e'
}
GM_xmlhttpRequest({
url: url,
headers: headers,
onload: function(r) {
if (r.status !== 200) {
console.log('fail', r)
return
}
// console.log(r)
const response = JSON.parse(r.responseText)
fullResponse = [...fullResponse, ...response.data]
if (response.paging.next) {
fetchMalList(name, xhr, fullResponse, response.paging.next)
} else {
xhr._malData = fullResponse
getAnimethemes({database: 'MyAnimeList', ids: fullResponse.map(i => i.node.id)}, xhr)
// console.log(fullResponse)
}
}
})
}
function fetchAnilistList(name, xhr, infoElement = null, userId = null) {
if (!userId) return getUserId()
infoElement && (infoElement.textContent = `Getting ids from anilist`)
const status = 'Completed'
fetch("https://graphql.anilist.co/", {
"headers": {
"Accept": "application/json",
"Content-Type": "application/json",
},
"body": `{\"query\":\"{MediaListCollection(userId: ${userId}, type: ANIME, forceSingleCompletedList: true) {lists {name status entries {mediaId status score(format: POINT_10)}}}}\",\"variables\":null,\"operationName\":null}`,
"method": "POST",
})
.then(response => response.json())
.then(data => {
const fullList = data.data.MediaListCollection.lists.map(i => i.entries).flat()
xhr._anilistData = fullList
requestAnimationFrame(_ => getAnimethemes({
database: 'AniList',
ids: fullList.map(i => i.mediaId)
}, xhr))
// console.log(fullList)
})
.catch(error => {
infoElement && (infoElement.textContent = `Failed to get anime ids from anilist`)
console.log("Failed to get anime ids from anilist", error)
})
function getUserId() {
fetch("https://graphql.anilist.co/", {
"headers": {
"Accept": "application/json",
"Content-Type": "application/json",
},
"body": `{\"query\":\"{User(name: \\\"${name}\\\") {id name}}\",\"variables\":null,\"operationName\":null}`,
"method": "POST",
})
.then(response => response.json())
.then(data => {
if (data.data.User) {
userId = data.data.User.id
fetchAnilistList(name, xhr, infoElement, userId)
} else {
infoElement && (infoElement.textContent = `Anilist user is private or doesn't exist`)
console.log(`Anilist user "${name}" is private or doesn't exist`, data)
}
})
.catch(error => {
infoElement && (infoElement.textContent = `Failed to get anilist user`)
console.log("Failed to get anilist user", error)
})
}
}
function getAnimethemes(o, xhr) {
const url1 = `https://api.animethemes.moe/anime?filter[has]=resources&filter[site]=${o.database}&filter[external_id]=`
const url2 = `&include=animethemes.animethemeentries.videos,animethemes.song,resources&page[size]=100`
const ratelimit = 80 // the rate limit is 90 requests per minute
const minTimeBewteenRequestsInMs = 60 / ratelimit * 1000
let fullResponse = []
let lastDate
function getResponse(url, moreToCome = false) {
fetch(url)
.then(response => response.json())
.then(data => {
fullResponse = [...fullResponse, ...data.anime]
if (data.links.next) {
getResponse(data.links.next)
} else {
if (moreToCome) return getResponseInChunks()
xhr._animethemesData = fullResponse
// simulateOriginalResponse(xhr)
requestAnimationFrame(_ => simulateOriginalResponse(xhr)) // don't catch future errors here
// console.log(fullResponse)
}
})
.catch(error => {
console.log("getanimethemes fetch error", error)
})
}
o.ids.length > 600 ? getResponseInChunks() : getResponse(url1 + o.ids.join() + url2)
function getResponseInChunks() {
const ids = o.ids.splice(0, 100)
const date = new Date()
const send = _ => o.ids.length > 0 ? getResponse(url1 + ids + url2, true) : getResponse(url1 + ids + url2)
if (lastDate && date.getTime() - lastDate.getTime() < minTimeBewteenRequestsInMs) {
// from my tests this is not needed since 90 requests of this kind per miniute aren't possible with the average response time
// but maybe i'm rate limited since i spammed the api while working on this project
// console.log(date.getTime() - lastDate.getTime())
setTimeout(send, minTimeBewteenRequestsInMs - (date.getTime() - lastDate.getTime()))
} else {
send()
}
lastDate = date
}
}
function simulateOriginalResponse(xhr) {
// console.log(xhr)
const thing = xhr._animethemesData.flatMap(i => [{
malID: i.resources[0].external_id,
name: i.name,
year: i.year,
season: i.season.toLowerCase(),
// themes: i.animethemes.flatMap(t => [{
// themeType: t.slug,
// themeName: t.song.title,
// mirror: {
// mirrorURL: t.animethemeentries[0].videos[0].link,
// priority: 3, // these 2 are useless?
// notes: ''
// }
// }]),
/*themes: i.animethemes.flatMap(a => Object.values(a.animethemeentries).flatMap((t) => [{
themeType: a.slug,
themeName: a.song.title,
mirror: {
mirrorURL: t.videos[0].link,
priority: 3,
notes: '',
// a: t
}
}])),*/
/*themes: i.animethemes.flatMap(a => Object.values(a.animethemeentries).flatMap((t, tindex, tarray) => t.videos.flatMap((v, vindex, varray) => vindex > 1 ? [] : [{
// themeType: a.slug,//array.length === 1 ? a.type : a.slug + ' V' + (index + 1),
themeType: tarray.length === 1 ? a.type : a.slug + ' V' + (tindex + 1),
themeName: a.song.title,
mirror: {
mirrorURL: v.link,
priority: 3,
notes: '',
// a: t
bb: {
t: [tindex, tarray],
v: [vindex, varray]
}
}
}]))),*/
themes: i.animethemes.flatMap(a => Object.values(a.animethemeentries).flatMap((t, tindex, tarray) => t.videos.flatMap((v, vindex, varray) => [{
// i don't understand the orignal naming scheme here
themeType: `${a.slug} V${tindex + 1}.${vindex}`,
themeName: a.song.title,
mirror: {
mirrorURL: v.link,
priority: 3,
notes: '',
// a: t
// bb: {
// t: [tindex, tarray],
// v: [vindex, varray]
// }
}
}]))),
score: (function(){
const e = (xhr._malData || xhr._anilistData).find(m => (m?.node?.id || m.mediaId) === i.resources[0].external_id)
return e.list_status ? e.list_status.score : e.score
}()),
watchStatus: (function() {
const malStatuses = {
'completed': 2,
'dropped': 4,
'on_hold': 3,
'watching': 1,
'plan_to_watch': 6
}
const anilistStatuses = {
'COMPLETED': 2,
'DROPPED': 4,
'PAUSED': 3,
'CURRENT': 1,
'PLANNING': 6
}
return xhr._malData ? malStatuses[xhr._malData.find(m => m.node.id === i.resources[0].external_id).list_status.status] :
anilistStatuses[xhr._anilistData.find(m => m.mediaId === i.resources[0].external_id).status]
}())
}])
xhr._emulatedResponse = thing
cachedEmulatedResponse = thing
xhr._ogsend()
// console.log(thing)
}