diff options
| author | Ritesh Ghosh <[email protected]> | 2023-08-17 23:17:25 +0530 |
|---|---|---|
| committer | Ritesh Ghosh <[email protected]> | 2023-08-17 23:17:25 +0530 |
| commit | 6e8d84970e19ac43186fa3837dff0e18b168e213 (patch) | |
| tree | d7180ea9c1914fda2c57e858f04508fe3bb50914 /src | |
| parent | dc4bf477c9e261f7a86c9c2542be0dac83c2a002 (diff) | |
| download | aniwatch-api-6e8d84970e19ac43186fa3837dff0e18b168e213.tar.xz aniwatch-api-6e8d84970e19ac43186fa3837dff0e18b168e213.zip | |
feat(animeEpisodes): added `animeEpisodes` parser
Diffstat (limited to 'src')
| -rw-r--r-- | src/parsers/animeEpisodes.ts | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/parsers/animeEpisodes.ts b/src/parsers/animeEpisodes.ts new file mode 100644 index 0000000..c5bcb7f --- /dev/null +++ b/src/parsers/animeEpisodes.ts @@ -0,0 +1,61 @@ +import { + SRC_BASE_URL, + SRC_AJAX_URL, + ACCEPT_HEADER, + USER_AGENT_HEADER, + ACCEPT_ENCODING_HEADER, +} from "../utils"; +import axios, { AxiosError } from "axios"; +import createHttpError, { HttpError } from "http-errors"; +import { load, CheerioAPI } from "cheerio"; +import { ScrapedAnimeEpisodes } from "../models/parsers"; + +// /anime/episodes/${anime-id} +async function scrapeAnimeEpisodes( + animeId: string +): Promise<ScrapedAnimeEpisodes | HttpError> { + const res: ScrapedAnimeEpisodes = { + totalEpisodes: 0, + episodes: [], + }; + + try { + const episodesAjax = await axios.get( + `${SRC_AJAX_URL}/v2/episode/list/${animeId.split("-").pop()}`, + { + headers: { + Accept: ACCEPT_HEADER, + "User-Agent": USER_AGENT_HEADER, + "X-Requested-With": "XMLHttpRequest", + "Accept-Encoding": ACCEPT_ENCODING_HEADER, + Referer: `${SRC_BASE_URL}/watch/${animeId}`, + }, + } + ); + + const $: CheerioAPI = load(episodesAjax.data.html); + + res.totalEpisodes = Number($(".detail-infor-content .ss-list a").length); + + $(".detail-infor-content .ss-list a").each((i, el) => { + res.episodes.push({ + title: $(el)?.attr("title")?.trim() || null, + episodeId: $(el)?.attr("href")?.split("/")?.pop() || null, + number: Number($(el).attr("data-number")), + isFiller: $(el).hasClass("ssl-item-filler"), + }); + }); + + return res; + } catch (err: any) { + if (err instanceof AxiosError) { + throw createHttpError( + err?.response?.status || 500, + err?.response?.statusText || "Something went wrong" + ); + } + throw createHttpError.InternalServerError(err?.message); + } +} + +export default scrapeAnimeEpisodes; |
