Skip to content

Commit 197cd01

Browse files
committed
Adding AnimeInfo API.
1 parent f33da87 commit 197cd01

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

src/gogoAnime.ts

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
IPagination,
1010
IPopularOngoingUpdate,
1111
IRecentRelease,
12-
IUrlParamsType
12+
IUrlParamsType,
13+
IAnime
1314
} from './types';
1415
import { getIdFromPath } from './utils';
1516

@@ -451,6 +452,99 @@ class GoGoAnime {
451452
return animes;
452453
}
453454

455+
async animeInfo(
456+
id: string,
457+
axiosConfig?: AxiosRequestConfig
458+
): Promise<IAnime> {
459+
const link = this.getUrlWithBase(`/category/${id}`);
460+
const res = await axios.get(link, axiosConfig);
461+
const $ = cheerioLoad(res.data);
462+
463+
const animeInfoBody = $('.anime_info_body');
464+
const animeInfoBodyBg = animeInfoBody.children('.anime_info_body_bg');
465+
466+
const movieId =
467+
$('input#movie_id.movie_id[type="hidden"]').attr('value') ?? '';
468+
const thumbnail = animeInfoBodyBg.children('img').attr('src') ?? '';
469+
const title = animeInfoBodyBg.children('h1').text().trim();
470+
471+
const info: IAnime = {
472+
id,
473+
link,
474+
title,
475+
thumbnail,
476+
movieId,
477+
genres: [],
478+
episodeCount: 0,
479+
episodePages: []
480+
};
481+
482+
animeInfoBodyBg.children('p.type').each((_, ele) => {
483+
const p = $(ele);
484+
485+
const type = p
486+
.children('span')
487+
.text()
488+
.replace(':', '')
489+
.trim()
490+
.toLowerCase();
491+
492+
const text = p.text().slice(`${type}:`.length).trim();
493+
494+
if (type === 'type') {
495+
info.type = text;
496+
return;
497+
}
498+
499+
if (type === 'plot summary') {
500+
info.summary = text;
501+
return;
502+
}
503+
504+
if (type === 'genre') {
505+
p.children('a').each((_, _ele) => {
506+
info.genres.push(this._getEntityFromA($(_ele)));
507+
});
508+
return;
509+
}
510+
511+
if (type === 'status') {
512+
info.status = text;
513+
return;
514+
}
515+
516+
if (type === 'released') {
517+
info.released = text;
518+
return;
519+
}
520+
521+
if (type === 'other name') {
522+
info.otherNames = text
523+
.split(', ')
524+
.map(t => t.trim())
525+
.filter(t => t !== '');
526+
return;
527+
}
528+
});
529+
530+
$('.anime_video_body ul#episode_page li').each((_, ele) => {
531+
const a = $(ele).children('a');
532+
533+
const start = Number(a.attr('ep_start'));
534+
const end = Number(a.attr('ep_end'));
535+
536+
info.episodePages.push({ start, end });
537+
});
538+
539+
info.episodePages.forEach(d => {
540+
if (d.end > info.episodeCount) {
541+
info.episodeCount = d.end;
542+
}
543+
});
544+
545+
return info;
546+
}
547+
454548
getUrlWithBase(path: string, params?: IUrlParamsType) {
455549
return this.getUrl(this.baseUrl, path, params);
456550
}

src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ export type IAnimeBasic = IEntity & {
3030
summary?: string;
3131
};
3232

33+
export type IAnime = IAnimeBasic & {
34+
movieId: string;
35+
type?: string;
36+
otherNames?: Array<string>;
37+
episodeCount: number;
38+
episodePages: Array<{ start: number; end: number }>;
39+
};
40+
3341
export type IRecentRelease = IEntity & {
3442
episode: string;
3543
};

0 commit comments

Comments
 (0)