aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/constants.ts129
-rw-r--r--src/utils/index.ts2
-rw-r--r--src/utils/methods.ts297
3 files changed, 0 insertions, 428 deletions
diff --git a/src/utils/constants.ts b/src/utils/constants.ts
deleted file mode 100644
index de7617f..0000000
--- a/src/utils/constants.ts
+++ /dev/null
@@ -1,129 +0,0 @@
-import { config } from "dotenv";
-
-config();
-
-export const ACCEPT_ENCODING_HEADER = "gzip, deflate, br";
-export const USER_AGENT_HEADER =
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4692.71 Safari/537.36";
-export const ACCEPT_HEADER =
- "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
-
-// previously aniwatch.to || aniwatchtv.to
-const DOMAIN = process.env.DOMAIN || "hianime.to";
-
-export const SRC_BASE_URL = `https://${DOMAIN}` as const;
-export const SRC_AJAX_URL = `${SRC_BASE_URL}/ajax` as const;
-export const SRC_HOME_URL = `${SRC_BASE_URL}/home` as const;
-export const SRC_SEARCH_URL = `${SRC_BASE_URL}/search` as const;
-
-// <SearchPageFilters>
-export const genresIdMap: Record<string, number> = {
- action: 1,
- adventure: 2,
- cars: 3,
- comedy: 4,
- dementia: 5,
- demons: 6,
- drama: 8,
- ecchi: 9,
- fantasy: 10,
- game: 11,
- harem: 35,
- historical: 13,
- horror: 14,
- isekai: 44,
- josei: 43,
- kids: 15,
- magic: 16,
- "martial-arts": 17,
- mecha: 18,
- military: 38,
- music: 19,
- mystery: 7,
- parody: 20,
- police: 39,
- psychological: 40,
- romance: 22,
- samurai: 21,
- school: 23,
- "sci-fi": 24,
- seinen: 42,
- shoujo: 25,
- "shoujo-ai": 26,
- shounen: 27,
- "shounen-ai": 28,
- "slice-of-life": 36,
- space: 29,
- sports: 30,
- "super-power": 31,
- supernatural: 37,
- thriller: 41,
- vampire: 32,
-} as const;
-
-export const typeIdMap: Record<string, number> = {
- all: 0,
- movie: 1,
- tv: 2,
- ova: 3,
- ona: 4,
- special: 5,
- music: 6,
-} as const;
-
-export const statusIdMap: Record<string, number> = {
- all: 0,
- "finished-airing": 1,
- "currently-airing": 2,
- "not-yet-aired": 3,
-} as const;
-
-export const ratedIdMap: Record<string, number> = {
- all: 0,
- g: 1,
- pg: 2,
- "pg-13": 3,
- r: 4,
- "r+": 5,
- rx: 6,
-} as const;
-
-export const scoreIdMap: Record<string, number> = {
- all: 0,
- appalling: 1,
- horrible: 2,
- "very-bad": 3,
- bad: 4,
- average: 5,
- fine: 6,
- good: 7,
- "very-good": 8,
- great: 9,
- masterpiece: 10,
-} as const;
-
-export const seasonIdMap: Record<string, number> = {
- all: 0,
- spring: 1,
- summer: 2,
- fall: 3,
- winter: 4,
-} as const;
-
-export const languageIdMap: Record<string, number> = {
- all: 0,
- sub: 1,
- dub: 2,
- "sub-&-dub": 3,
-} as const;
-
-export const sortIdMap: Record<string, string> = {
- default: "default",
- "recently-added": "recently_added",
- "recently-updated": "recently_updated",
- score: "score",
- "name-a-z": "name_az",
- "released-date": "released_date",
- "most-watched": "most_watched",
-} as const;
-// </SearchPageFilters>
diff --git a/src/utils/index.ts b/src/utils/index.ts
deleted file mode 100644
index b514c85..0000000
--- a/src/utils/index.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-export * from "./methods.js";
-export * from "./constants.js";
diff --git a/src/utils/methods.ts b/src/utils/methods.ts
deleted file mode 100644
index 6aed9f7..0000000
--- a/src/utils/methods.ts
+++ /dev/null
@@ -1,297 +0,0 @@
-import type {
- Anime,
- Top10Anime,
- MostPopularAnime,
- Top10AnimeTimePeriod,
-} from "../types/anime.js";
-import type { CheerioAPI, SelectorType } from "cheerio";
-import {
- genresIdMap,
- languageIdMap,
- ratedIdMap,
- scoreIdMap,
- seasonIdMap,
- sortIdMap,
- statusIdMap,
- typeIdMap,
-} from "./constants.js";
-import { type FilterKeys } from "../types/controllers/animeSearch.js";
-import createHttpError, { HttpError } from "http-errors";
-
-export const 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(),
- jname:
- $(el)
- .find(".film-detail .film-name .dynamic-name")
- ?.attr("data-jname")
- ?.trim() || null,
- 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: {
- 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,
- },
- });
- });
-
- return animes;
- } catch (err: any) {
- throw createHttpError.InternalServerError(
- err?.message || "Something went wrong"
- );
- }
-};
-
-export const 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,
- jname:
- $(el)
- .find(".film-detail .dynamic-name")
- ?.attr("data-jname")
- ?.trim() || null,
- poster:
- $(el)
- .find(".film-poster .film-poster-img")
- ?.attr("data-src")
- ?.trim() || null,
- episodes: {
- 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"
- );
- }
-};
-
-export const extractMostPopularAnimes = (
- $: CheerioAPI,
- selector: SelectorType
-): Array<MostPopularAnime> | HttpError => {
- try {
- const animes: Array<MostPopularAnime> = [];
-
- $(selector).each((i, el) => {
- 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,
- jname:
- $(el)
- .find(".film-detail .film-name .dynamic-name")
- .attr("data-jname")
- ?.trim() || null,
- poster:
- $(el)
- .find(".film-poster .film-poster-img")
- ?.attr("data-src")
- ?.trim() || null,
- episodes: {
- sub:
- Number($(el)?.find(".fd-infor .tick .tick-sub")?.text()?.trim()) ||
- null,
- dub:
- Number($(el)?.find(".fd-infor .tick .tick-dub")?.text()?.trim()) ||
- null,
- },
- type:
- $(el)
- ?.find(".fd-infor .tick")
- ?.text()
- ?.trim()
- ?.replace(/[\s\n]+/g, " ")
- ?.split(" ")
- ?.pop() || null,
- });
- });
-
- return animes;
- } catch (err: any) {
- throw createHttpError.InternalServerError(
- err?.message || "Something went wrong"
- );
- }
-};
-
-export function retrieveServerId(
- $: CheerioAPI,
- index: number,
- category: "sub" | "dub" | "raw"
-) {
- return (
- $(`.ps_-block.ps_-block-sub.servers-${category} > .ps__-list .server-item`)
- ?.map((_, el) =>
- $(el).attr("data-server-id") == `${index}` ? $(el) : null
- )
- ?.get()[0]
- ?.attr("data-id") || null
- );
-}
-
-function getGenresFilterVal(genreNames: string[]): string | undefined {
- if (genreNames.length < 1) {
- return undefined;
- }
- return genreNames.map((name) => genresIdMap[name]).join(",");
-}
-
-export function getSearchFilterValue(
- key: FilterKeys,
- rawValue: string
-): string | undefined {
- rawValue = rawValue.trim();
- if (!rawValue) return undefined;
-
- switch (key) {
- case "genres": {
- return getGenresFilterVal(rawValue.split(","));
- }
- case "type": {
- const val = typeIdMap[rawValue] ?? 0;
- return val === 0 ? undefined : `${val}`;
- }
- case "status": {
- const val = statusIdMap[rawValue] ?? 0;
- return val === 0 ? undefined : `${val}`;
- }
- case "rated": {
- const val = ratedIdMap[rawValue] ?? 0;
- return val === 0 ? undefined : `${val}`;
- }
- case "score": {
- const val = scoreIdMap[rawValue] ?? 0;
- return val === 0 ? undefined : `${val}`;
- }
- case "season": {
- const val = seasonIdMap[rawValue] ?? 0;
- return val === 0 ? undefined : `${val}`;
- }
- case "language": {
- const val = languageIdMap[rawValue] ?? 0;
- return val === 0 ? undefined : `${val}`;
- }
- case "sort": {
- return sortIdMap[rawValue] ?? undefined;
- }
- default:
- return undefined;
- }
-}
-
-// this fn tackles both start_date and end_date
-export function getSearchDateFilterValue(
- isStartDate: boolean,
- rawValue: string
-): string[] | undefined {
- rawValue = rawValue.trim();
- if (!rawValue) return undefined;
-
- const dateRegex = /^\d{4}-([0-9]|1[0-2])-([0-9]|[12][0-9]|3[01])$/;
- const dateCategory = isStartDate ? "s" : "e";
- const [year, month, date] = rawValue.split("-");
-
- if (!dateRegex.test(rawValue)) {
- return undefined;
- }
-
- // sample return -> [sy=2023, sm=10, sd=11]
- return [
- Number(year) > 0 ? `${dateCategory}y=${year}` : "",
- Number(month) > 0 ? `${dateCategory}m=${month}` : "",
- Number(date) > 0 ? `${dateCategory}d=${date}` : "",
- ].filter((d) => Boolean(d));
-}
-
-export function substringAfter(str: string, toFind: string) {
- const index = str.indexOf(toFind);
- return index == -1 ? "" : str.substring(index + toFind.length);
-}
-
-export function substringBefore(str: string, toFind: string) {
- const index = str.indexOf(toFind);
- return index == -1 ? "" : str.substring(0, index);
-}