aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorRitesh Ghosh <[email protected]>2023-08-02 13:41:37 +0530
committerRitesh Ghosh <[email protected]>2023-08-02 13:41:37 +0530
commit38ae13dcdce8b49970d8f51d314856767be40ab1 (patch)
tree6dafb11ed37bd0f0e93600ee9f24e2d6729027de /src/utils
parente758c74d15a5158003b8fb6d3729c2224503fe68 (diff)
downloadaniwatch-api-38ae13dcdce8b49970d8f51d314856767be40ab1.tar.xz
aniwatch-api-38ae13dcdce8b49970d8f51d314856767be40ab1.zip
feat: added utility props & methods
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/index.ts118
1 files changed, 118 insertions, 0 deletions
diff --git a/src/utils/index.ts b/src/utils/index.ts
new file mode 100644
index 0000000..6099be4
--- /dev/null
+++ b/src/utils/index.ts
@@ -0,0 +1,118 @@
+import { config } from "dotenv";
+import createHttpError, { HttpError } from "http-errors";
+import { CheerioAPI, SelectorType } from "cheerio";
+import { Anime, Top10Anime, Top10AnimeTimePeriod } from "../models";
+
+config();
+
+export const USER_AGENT_HEADER = process.env.APP_SRC_USER_AGENT;
+export const ACCEPT_HEADER = process.env.APP_SRC_ACCEPT_HEADER;
+export const ACCEPT_ENCODING_HEADER = process.env.APP_SRC_ACCEPT_HEADER;
+
+export const SRC_BASE_URL = process.env.APP_SRC_BASE_URL;
+export const SRC_AJAX_URL = process.env.APP_SRC_AJAX_URL;
+export const SRC_HOME_URL = process.env.APP_SRC_HOME_URL;
+export const SRC_SEARCH_URL = process.env.APP_SRC_SEARCH_URL;
+
+export function extractAnimes(
+ $: CheerioAPI,
+ selector: SelectorType
+): Array<Anime> | HttpError {
+ try {
+ const animes: Array<Anime> = [];
+
+ $(selector).each((i, el) => {
+ const animeId =
+ $(el)
+ .find(".film-detail .film-name .dynamic-name")
+ ?.attr("href")
+ ?.slice(1)
+ .split("?ref=search")[0] || null;
+
+ animes.push({
+ id: animeId,
+ name: $(el)
+ .find(".film-detail .film-name .dynamic-name")
+ ?.text()
+ ?.trim(),
+ poster:
+ $(el)
+ .find(".film-poster .film-poster-img")
+ ?.attr("data-src")
+ ?.trim() || null,
+ duration: $(el)
+ .find(".film-detail .fd-infor .fdi-item.fdi-duration")
+ ?.text()
+ ?.trim(),
+ type: $(el)
+ .find(".film-detail .fd-infor .fdi-item:nth-of-type(1)")
+ ?.text()
+ ?.trim(),
+ rating: $(el).find(".film-poster .tick-rate")?.text()?.trim() || null,
+ episodes:
+ $(el)
+ .find(".film-poster .tick-eps")
+ ?.text()
+ ?.trim()
+ .split(" ")
+ .pop() || null,
+ });
+ });
+
+ return animes;
+ } catch (err: any) {
+ throw createHttpError.InternalServerError(
+ err?.message || "Something went wrong"
+ );
+ }
+}
+
+export function extractTop10Animes(
+ $: CheerioAPI,
+ period: Top10AnimeTimePeriod
+): Array<Top10Anime> | HttpError {
+ try {
+ const animes: Array<Top10Anime> = [];
+ const selector = `#top-viewed-${period} ul li`;
+
+ $(selector).each((i, el) => {
+ animes.push({
+ id:
+ $(el)
+ .find(".film-detail .dynamic-name")
+ ?.attr("href")
+ ?.slice(1)
+ .trim() || null,
+ rank: Number($(el).find(".film-number span")?.text()?.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,
+ eps: {
+ sub:
+ Number(
+ $(el)
+ .find(".film-detail .fd-infor .tick-item.tick-sub")
+ ?.text()
+ ?.trim()
+ ) || null,
+ dub:
+ Number(
+ $(el)
+ .find(".film-detail .fd-infor .tick-item.tick-dub")
+ ?.text()
+ ?.trim()
+ ) || null,
+ },
+ });
+ });
+
+ return animes;
+ } catch (err: any) {
+ throw createHttpError.InternalServerError(
+ err?.message || "Something went wrong"
+ );
+ }
+}