aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRitesh Ghosh <[email protected]>2023-08-13 19:33:58 +0530
committerRitesh Ghosh <[email protected]>2023-08-13 19:33:58 +0530
commite4ffe2aba70fd73fa5e75daf3900e92b5881e1c4 (patch)
tree8f2edce08850fd4f258155a01ad6de4a5ce2ccbf /src
parent9714f7f59b15621f0a36aef16e481ee155e9e4c1 (diff)
downloadaniwatch-api-e4ffe2aba70fd73fa5e75daf3900e92b5881e1c4.tar.xz
aniwatch-api-e4ffe2aba70fd73fa5e75daf3900e92b5881e1c4.zip
feat(animeSearchSuggestion): added `animeSearchSuggestion` parser
Diffstat (limited to 'src')
-rw-r--r--src/parsers/animeSearchSuggestion.ts78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/parsers/animeSearchSuggestion.ts b/src/parsers/animeSearchSuggestion.ts
new file mode 100644
index 0000000..6fb8242
--- /dev/null
+++ b/src/parsers/animeSearchSuggestion.ts
@@ -0,0 +1,78 @@
+import axios, { AxiosError } from "axios";
+import { load, CheerioAPI, SelectorType } from "cheerio";
+import {
+ SRC_HOME_URL,
+ SRC_AJAX_URL,
+ USER_AGENT_HEADER,
+ ACCEPT_ENCODING_HEADER,
+} from "../utils";
+
+import createHttpError, { HttpError } from "http-errors";
+import { ScrapedAnimeSearchSuggestion } from "../models/parsers";
+
+// /anime/search/suggest?q=${query}
+async function scrapeAnimeSearchSuggestion(
+ q: string
+): Promise<ScrapedAnimeSearchSuggestion | HttpError> {
+ const res: ScrapedAnimeSearchSuggestion = {
+ suggestions: [],
+ };
+
+ try {
+ const { data } = await axios.get(
+ `${SRC_AJAX_URL}/search/suggest?keyword=${encodeURIComponent(q)}`,
+ {
+ headers: {
+ Accept: "*/*",
+ Pragma: "no-cache",
+ Referer: SRC_HOME_URL,
+ "User-Agent": USER_AGENT_HEADER,
+ "X-Requested-With": "XMLHttpRequest",
+ "Accept-Encoding": ACCEPT_ENCODING_HEADER,
+ },
+ }
+ );
+
+ const $: CheerioAPI = load(data.html);
+ const selector: SelectorType = ".nav-item:has(.film-poster)";
+
+ if ($(selector).length < 1) return res;
+
+ $(selector).each((_, el) => {
+ const id = $(el).attr("href")?.split("?")[0].includes("javascript")
+ ? null
+ : $(el).attr("href")?.split("?")[0]?.slice(1);
+
+ res.suggestions.push({
+ id,
+ name: $(el).find(".srp-detail .film-name")?.text()?.trim() || null,
+ jname:
+ $(el).find(".srp-detail .film-name")?.attr("data-jname")?.trim() ||
+ $(el).find(".srp-detail .alias-name")?.text()?.trim() ||
+ null,
+ poster: $(el)
+ .find(".film-poster .film-poster-img")
+ ?.attr("data-src")
+ ?.trim(),
+ moreInfo: [
+ ...$(el)
+ .find(".film-infor")
+ .contents()
+ .map((_, el) => $(el).text().trim()),
+ ].filter((i) => i),
+ });
+ });
+
+ 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 scrapeAnimeSearchSuggestion;