diff options
| author | Ritesh Ghosh <[email protected]> | 2023-08-02 21:18:29 +0530 |
|---|---|---|
| committer | Ritesh Ghosh <[email protected]> | 2023-08-02 21:18:29 +0530 |
| commit | 22ef7a54f6b3b01366cbad15ab2ca62574d30ecb (patch) | |
| tree | c24f5586059a10797aab84679dc57f27140906d7 /src/utils | |
| parent | dd3df97493e4a530e065819bf3f998faad6fb5ee (diff) | |
| download | aniwatch-api-22ef7a54f6b3b01366cbad15ab2ca62574d30ecb.tar.xz aniwatch-api-22ef7a54f6b3b01366cbad15ab2ca62574d30ecb.zip | |
feat: updated `extractAnimes` & added `extractMostPopularAnimes`
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/index.ts | 84 |
1 files changed, 76 insertions, 8 deletions
diff --git a/src/utils/index.ts b/src/utils/index.ts index 6099be4..e34849c 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,7 +1,12 @@ import { config } from "dotenv"; import createHttpError, { HttpError } from "http-errors"; import { CheerioAPI, SelectorType } from "cheerio"; -import { Anime, Top10Anime, Top10AnimeTimePeriod } from "../models"; +import { + Anime, + Top10Anime, + Top10AnimeTimePeriod, + MostPopularAnime, +} from "../models"; config(); @@ -49,13 +54,26 @@ export function extractAnimes( ?.text() ?.trim(), rating: $(el).find(".film-poster .tick-rate")?.text()?.trim() || null, - episodes: - $(el) - .find(".film-poster .tick-eps") - ?.text() - ?.trim() - .split(" ") - .pop() || null, + episodes: { + sub: + Number( + $(el) + .find(".film-poster .tick-sub") + ?.text() + ?.trim() + .split(" ") + .pop() + ) || null, + dub: + Number( + $(el) + .find(".film-poster .tick-dub") + ?.text() + ?.trim() + .split(" ") + .pop() + ) || null, + }, }); }); @@ -116,3 +134,53 @@ export function extractTop10Animes( ); } } + +export function extractMostPopularAnimes( + $: CheerioAPI, + selector: SelectorType +): Array<MostPopularAnime> | HttpError { + try { + const animes: Array<MostPopularAnime> = []; + + $(selector).each((i, el) => { + const otherInfoSrc = $(el) + ?.find(".fd-infor .tick") + ?.text() + ?.trim() + ?.replace(/\n/g, "") + .split(" "); + + let otherInfos: string[] = [ + otherInfoSrc[0] || "", + otherInfoSrc?.pop() || "", + ]; + + animes.push({ + id: + $(el) + .find(".film-detail .dynamic-name") + ?.attr("href") + ?.slice(1) + .trim() || null, + name: $(el).find(".film-detail .dynamic-name")?.text()?.trim() || null, + poster: + $(el) + .find(".film-poster .film-poster-img") + ?.attr("data-src") + ?.trim() || null, + jname: + $(el) + .find(".film-detail .film-name .dynamic-name") + .attr("data-jname") + ?.trim() || null, + otherInfo: otherInfos.filter((i) => i !== ""), + }); + }); + + return animes; + } catch (err: any) { + throw createHttpError.InternalServerError( + err?.message || "Something went wrong" + ); + } +} |
