-
Notifications
You must be signed in to change notification settings - Fork 5
/
anime.ts
498 lines (405 loc) · 15.6 KB
/
anime.ts
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import { Client } from '../../core/client'
import {
Content,
ContentRelationType,
ContentRelationGroup,
ContentStatistics,
ContentNews,
ContentUserUpdate,
ContentReactions,
ContentReview,
ContentExternal
} from './base'
import { BaseClass, BaseResource } from '../base'
import { YoutubeVideo, Link, ImageFormatCollection } from '../misc'
import {
ProducerMeta,
AnimeGenreMeta,
PersonMeta,
CharacterMeta,
AnimeMeta,
MangaMeta
} from '../meta'
import ParseDuration from 'parse-duration'
import { animeExplicitGenres } from '../../manager/genre'
export type AnimeType = 'TV' | 'OVA' | 'Movie' | 'Special' | 'ONA' | 'Music' | 'CM' | 'PV' | 'TV Special' | 'Unknown'
export type AnimeAirStatus = 'Finished Airing' | 'Airing' | 'Not Yet Aired' | 'Unknown'
export type AnimeRating = 'None' | 'G' | 'PG' | 'PG-13+' | 'R-17+' | 'R+' | 'Rx' | 'Unknown'
export type AnimeSeason = 'Summer' | 'Winter' | 'Spring' | 'Fall' | 'Unknown'
export class AnimeAirInformation extends BaseClass {
/** @hidden */
public static parseStatus (input: any): AnimeAirStatus {
const status = input?.toLowerCase().trim()
switch (status) {
case 'finished airing': return 'Finished Airing'
case 'currently airing': return 'Airing'
case 'not yet aired': return 'Not Yet Aired'
default: return 'Unknown'
}
}
public readonly status: AnimeAirStatus
public readonly airing: boolean
public readonly airedFrom: Date | null
public readonly airedTo: Date | null
public constructor (client: Client, data: any) {
super(client)
this.status = AnimeAirInformation.parseStatus(data.status)
this.airing = !!data.airing
this.airedFrom = AnimeAirInformation.parseDate(data.aired.from, true)
this.airedTo = AnimeAirInformation.parseDate(data.aired.to, true)
}
}
export class Anime extends Content {
/** @hidden */
public static parseType (input: any): AnimeType {
switch (input?.toLowerCase().trim()) {
case 'tv': return 'TV'
case 'ova': return 'OVA'
case 'movie': return 'Movie'
case 'special': return 'Special'
case 'ona': return 'ONA'
case 'music': return 'Music'
case 'cm': return 'CM'
case 'pv': return 'PV'
case 'tv special': return 'TV Special'
case 'unknow':
case 'unknown':
case '-':
default: return 'Unknown'
}
}
/** @hidden */
public static parseRating (input: any): AnimeRating {
switch (input?.toLowerCase().trim()) {
case 'none': return 'None'
case 'g - all ages': return 'G'
case 'pg - childre': return 'PG'
case 'pg - children': return 'PG'
case 'pg-13 - teens 13 or older': return 'PG-13+'
case 'r - 17+ (violence & profanity)': return 'R-17+'
case 'r+ - mild nudity': return 'R+'
case 'rx - hentai': return 'Rx'
default: return 'Unknown'
}
}
/** @hidden */
public static parseSeason (input: any): AnimeSeason | null {
switch (input) {
case 'summer': return 'Summer'
case 'winter': return 'Winter'
case 'spring': return 'Spring'
case 'fall': return 'Fall'
default: return 'Unknown'
}
}
public readonly trailer: YoutubeVideo | null
public readonly type: AnimeType
public readonly source: string | null
public readonly episodes: number | null
public readonly airInfo: AnimeAirInformation
public readonly duration: number | null
public readonly rating: AnimeRating
public readonly season: AnimeSeason | null
public readonly year: number | null
public readonly producers: Array<ProducerMeta>
public readonly licensors: Array<ProducerMeta>
public readonly studios: Array<ProducerMeta>
public readonly genres: Array<AnimeGenreMeta<'Genre'>>
public readonly explicitGenres: Array<AnimeGenreMeta<'Explicit'>>
public readonly themes: Array<AnimeGenreMeta<'Theme'>>
public readonly demographics: Array<AnimeGenreMeta<'Demographic'>>
public readonly broadcast: AnimeBroadcast | null
public get isExplicit (): boolean {
return !!(
['Rx', 'R-17+'].includes(this.rating) ||
this.genres.find((genre) => !!animeExplicitGenres.find((genreEntry) => genreEntry[0] === genre.id))
)
}
public getCharacters () {
return <Promise<Array<AnimeCharacterReference>>> this.client.anime.getCharacters(this.id)
}
public getStaff () {
return <Promise<Array<AnimeStaffReference>>> this.client.anime.getStaff(this.id)
}
public getEpisodes (offset?: number, maxCount?: number) {
return <Promise<Array<AnimePartialEpisode>>> this.client.anime.getEpisodes(this.id, offset, maxCount)
}
public getEpisode (episodeId: number) {
return <Promise<AnimeEpisode>> this.client.anime.getEpisode(this.id, episodeId)
}
public getNews (offset?: number, maxCount?: number) {
return <Promise<Array<ContentNews>>> this.client.anime.getNews(this.id, offset, maxCount)
}
public getTopics (topic?: 'all' | 'episode' | 'other') {
return <Promise<Array<AnimeTopic>>> this.client.anime.getTopics(this.id, topic)
}
public getVideos () {
return <Promise<AnimeVideo>> this.client.anime.getVideos(this.id)
}
public getVideosEpisodes (offset?: number, maxCount?: number) {
return <Promise<Array<AnimeEpisodeVideo>>> this.client.anime.getVideosEpisodes(this.id, offset, maxCount)
}
public getPictures () {
return <Promise<Array<ImageFormatCollection>>> this.client.anime.getPictures(this.id)
}
public getStatistics () {
return <Promise<AnimeStatistics>> this.client.anime.getStatistics(this.id)
}
public getMoreInfo () {
return <Promise<string | null>> this.client.anime.getMoreInfo(this.id)
}
public getRecommendations () {
return <Promise<Array<AnimeRecommendation>>> this.client.anime.getRecommendations(this.id)
}
public getUserUpdates (offset?: number, maxCount?: number) {
return <Promise<Array<AnimeUserUpdate>>> this.client.anime.getUserUpdates(this.id, offset, maxCount)
}
public getReviews (offset?: number, maxCount?: number) {
return <Promise<Array<AnimeReview>>> this.client.anime.getReviews(this.id, offset, maxCount)
}
public getRelations () {
return <Promise<Array<AnimeRelationGroup<ContentRelationType>>>> this.client.anime.getRelations(this.id)
}
public getThemes () {
return <Promise<{ openings: Array<string>, endings: Array<string> }>> this.client.anime.getThemes(this.id)
}
public getExternal () {
return <Promise<Array<ContentExternal>>> this.client.anime.getExternal(this.id)
}
public getStreamingLinks () {
return <Promise<Array<Link>>> this.client.anime.getStreamingLinks(this.id)
}
public getFull () {
return <Promise<AnimeFull>> this.client.anime.getFull(this.id)
}
public constructor (client: Client, data: any) {
super(client, data)
this.trailer = data.trailer ? new YoutubeVideo(client, data.trailer) : null
this.type = Anime.parseType(data.type)
this.source = data.source || null
this.episodes = data.episodes ?? null
this.airInfo = new AnimeAirInformation(client, data)
this.duration = ParseDuration(data.duration, 'millisecond') || null
this.rating = Anime.parseRating(data.rating)
this.season = Anime.parseSeason(data.season)
this.year = data.year || null
this.producers = data.producers?.map((producer: any) => new ProducerMeta(this.client, producer)) || []
this.licensors = data.licensors?.map((licensor: any) => new ProducerMeta(this.client, licensor)) || []
this.studios = data.studios?.map((studio: any) => new ProducerMeta(this.client, studio)) || []
this.genres = data.genres?.map((genre: any) => new AnimeGenreMeta(this.client, genre, 'Genre')) || []
this.explicitGenres = data.explicit_genres?.map((genre: any) => new AnimeGenreMeta(this.client, genre, 'Explicit')) || []
this.demographics = data.demographics?.map((genre: any) => new AnimeGenreMeta(this.client, genre, 'Demographic')) || []
this.themes = data.themes?.map((genre: any) => new AnimeGenreMeta(this.client, genre, 'Theme')) || []
this.broadcast = new AnimeBroadcast(client, data.broadcast)
}
}
export class AnimeBroadcast extends BaseClass {
public readonly day?: string
public readonly time?: string
public readonly timezone?: string
public readonly string?: string
public constructor (client: Client, data: any) {
super(client)
this.day = data.day
this.time = data.time
this.timezone = data.timezone
this.string = data.string
}
}
export class AnimeVoiceActorReference extends BaseClass {
public readonly language: string
public readonly person: PersonMeta
public constructor (client: Client, data: any) {
super(client)
this.language = data.language
this.person = new PersonMeta(client, data.person)
}
}
export class AnimeCharacterReference extends BaseClass {
public readonly role: string
public readonly character: CharacterMeta
public readonly voiceActors: Array<AnimeVoiceActorReference>
public constructor (client: Client, data: any) {
super(client)
this.role = data.role
this.character = new CharacterMeta(client, data.character)
this.voiceActors = data.voice_actors?.map((voiceActor: any) => new AnimeVoiceActorReference(this.client, voiceActor)) || []
}
}
export class AnimeStaffReference extends BaseClass {
public readonly positions: Array<string>
public readonly person: PersonMeta
public constructor (client: Client, data: any) {
super(client)
this.positions = data.positions.filter((position: any) => !!position)
this.person = new PersonMeta(client, data.person)
}
}
export class AnimeEpisodeTitle extends BaseClass {
public readonly default: string
public readonly japanese: string | null
public readonly romanji: string | null
public toString () {
return this.default
}
public constructor (client: Client, data: any) {
super(client)
this.default = data.title
this.japanese = data.japanese || null
this.romanji = data.romanji || null
}
}
export class AnimeEpisode extends BaseClass {
public readonly animeId: number
public readonly episodeId: number
public readonly URL: URL | null
public readonly title: AnimeEpisodeTitle
public readonly duration: number
public readonly aired: Date | null
public readonly filler: boolean
public readonly recap: boolean
public readonly synopsis: string | null
public constructor (client: Client, animeId: number, data: any) {
super(client)
this.animeId = animeId
this.episodeId = data.mal_id
this.URL = AnimeEpisode.parseURL(data.url, true)
this.title = new AnimeEpisodeTitle(client, data)
this.duration = data.duration || null
this.aired = data.aired ? new Date(data.aired) : null
this.filler = !!data.filler
this.recap = !!data.recap
this.synopsis = data.synopsis || null
}
}
export class AnimePartialEpisode extends AnimeEpisode {
public readonly synopsis: null
public readonly forumUrl: URL | null
public getFullEpisode () {
return <Promise<AnimeEpisode>> this.client.anime.getEpisode(this.animeId, this.episodeId)
}
public constructor (client: Client, animeId: number, data: any) {
super(client, animeId, data)
this.synopsis = null
this.forumUrl = AnimePartialEpisode.parseURL(data.forum_url, true)
}
}
export class AnimeTopic extends BaseResource {
public readonly title: string
public readonly date: Date
public readonly authorUsername: string
public readonly authorURL: URL
public readonly comments: number
public constructor (client: Client, data: any) {
super(client, data)
this.title = data.title
this.date = new Date(data.date)
this.authorUsername = data.author_username
this.authorURL = AnimeTopic.parseURL(data.author_url)
this.comments = data.comments
}
}
export class AnimePromo extends BaseClass {
public readonly title: string
public readonly trailer: YoutubeVideo
public constructor (client: Client, data: any) {
super(client)
this.title = data.title
this.trailer = new YoutubeVideo(client, data.trailer)
}
}
export class AnimeEpisodeVideo extends BaseResource {
public readonly title: string
public readonly episode: number
public readonly image: ImageFormatCollection | null
public constructor (client: Client, data: any) {
super(client, data)
this.title = data.title
this.episode = typeof (data.episode) === 'string' ? Number(data.episode.toLowerCase().split('episode')[1]?.trim()) || 0 : 0
this.image = data.images != null ? new ImageFormatCollection(client, data.images) : null
}
}
export class AnimeMusicVideo extends BaseClass {
public constructor (client: Client, data: any) {
super(client)
this.title = data.title
this.video = new YoutubeVideo(client, data.video)
this.meta = data.meta
}
public readonly title: string
public readonly video: YoutubeVideo
public readonly meta: {
title: string
author: string
}
}
export class AnimeVideo extends BaseClass {
public readonly promos: Array<AnimePromo>
public readonly episodes: Array<AnimeEpisodeVideo>
public readonly musicVideos: Array<AnimeMusicVideo>
public constructor (client: Client, data: any) {
super(client)
this.promos = data.promo?.map((promo: any) => new AnimePromo(this.client, promo)) || []
this.episodes = data.episodes?.map((episodeVideo: any) => new AnimeEpisodeVideo(this.client, episodeVideo)) || []
this.musicVideos = data.music_videos?.map((musicVideo: any) => new AnimeMusicVideo(client, musicVideo)) || []
}
}
export class AnimeStatistics extends ContentStatistics {
public readonly watching: number
public readonly planToWatch: number
public constructor (client: Client, data: any) {
super(client, data)
this.watching = data.watching
this.planToWatch = data.plan_to_watch
}
}
export class AnimeRecommendation extends BaseClass {
public readonly entry: AnimeMeta
public readonly URL: URL | null
public readonly votes: number
public constructor (client: Client, data: any) {
super(client)
this.entry = new AnimeMeta(client, data.entry)
this.URL = AnimeRecommendation.parseURL(data.url)
this.votes = data.votes
}
}
export class AnimeUserUpdate extends ContentUserUpdate {
public readonly episodesSeen: number
public readonly episodesTotal: number
public constructor (client: Client, data: any) {
super(client, data)
this.episodesSeen = data.episodes_seen
this.episodesTotal = data.episodes_total
}
}
export class AnimeReview extends ContentReview {
public readonly episodesWatched: number
public constructor (client: Client, data: any) {
super(client, data)
this.episodesWatched = data.episodes_watched || 0
}
}
export class AnimeRelationGroup<T extends ContentRelationType> extends ContentRelationGroup<T> {
public readonly items: T extends 'Adaptation' ? Array<MangaMeta> : Array<AnimeMeta>
public constructor (client: Client, relation: T, data: any) {
super(client, relation, data)
this.items = data.entry?.map((item: any) => new (this.relation === 'Adaptation' ? MangaMeta : AnimeMeta)(this.client, item)) || []
}
}
export class AnimeFull extends Anime {
public readonly relations: Array<AnimeRelationGroup<ContentRelationType>>
public readonly themeSongs: {
optenings: Array<string>
endings: Array<string>
}
public readonly external: Array<ContentExternal>
public readonly streamingLinks: Array<Link>
public constructor (client: Client, data: any) {
super(client, data)
this.relations = data.relations?.map((relation: any) => new AnimeRelationGroup(this.client, AnimeRelationGroup.parseRelation(relation.relation), relation)) || []
this.themeSongs = data.theme || data.theme_songs || []
this.external = data.external?.map((external: any) => new ContentExternal(client, external))
this.streamingLinks = data.streaming
}
}