aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRitesh Ghosh <[email protected]>2023-08-17 23:48:55 +0530
committerRitesh Ghosh <[email protected]>2023-08-17 23:48:55 +0530
commite152221f8fa85550b8a834bf86a743661e542d07 (patch)
tree06774bf8a30750017a43e6547ea472ee81d33c44 /src
parentc0ce89a828e838a32769efeac78eebe3207d2027 (diff)
downloadaniwatch-api-e152221f8fa85550b8a834bf86a743661e542d07.tar.xz
aniwatch-api-e152221f8fa85550b8a834bf86a743661e542d07.zip
feat(animeEpisodeSrcs): added `animeEpisodeSrcs` parser
Diffstat (limited to 'src')
-rw-r--r--src/parsers/animeEpisodeSrcs.ts127
1 files changed, 127 insertions, 0 deletions
diff --git a/src/parsers/animeEpisodeSrcs.ts b/src/parsers/animeEpisodeSrcs.ts
new file mode 100644
index 0000000..7a0bc0a
--- /dev/null
+++ b/src/parsers/animeEpisodeSrcs.ts
@@ -0,0 +1,127 @@
+import {
+ SRC_BASE_URL,
+ SRC_AJAX_URL,
+ USER_AGENT_HEADER,
+ retrieveServerId,
+} from "../utils";
+
+import axios, { AxiosError } from "axios";
+import { load, CheerioAPI } from "cheerio";
+import { AnimeServers, Servers } from "../models/anime";
+import createHttpError, { HttpError } from "http-errors";
+import { ScrapedAnimeEpisodesSources } from "../models/parsers";
+import { RapidCloud, StreamSB, StreamTape } from "../extractors";
+
+// vidtreaming -> 4
+// rapidcloud -> 1
+// streamsb -> 5
+// streamtape -> 3
+
+// /anime/episode-src?id=${episodeId}?server=${server}&category=${category (dub or sub)}
+async function scrapeAnimeEpisodeSources(
+ episodeId: string,
+ server: AnimeServers = Servers.VidStreaming,
+ category: "sub" | "dub" = "sub"
+ // server:
+): Promise<ScrapedAnimeEpisodesSources | HttpError> {
+ if (episodeId.startsWith("http")) {
+ const serverUrl = new URL(episodeId);
+ switch (server) {
+ case Servers.VidStreaming:
+ case Servers.VidCloud:
+ return {
+ ...(await new RapidCloud().extract(serverUrl)),
+ };
+ case Servers.StreamSB:
+ return {
+ headers: {
+ Referer: serverUrl.href,
+ watchsb: "streamsb",
+ "User-Agent": USER_AGENT_HEADER,
+ },
+ sources: await new StreamSB().extract(serverUrl, true),
+ };
+ case Servers.StreamTape:
+ return {
+ headers: { Referer: serverUrl.href, "User-Agent": USER_AGENT_HEADER },
+ sources: await new StreamTape().extract(serverUrl),
+ };
+ default: // vidcloud
+ return {
+ headers: { Referer: serverUrl.href },
+ ...(await new RapidCloud().extract(serverUrl)),
+ };
+ }
+ }
+
+ const epId = new URL(`/watch/${episodeId}`, SRC_BASE_URL).href;
+ console.log(epId);
+
+ try {
+ const resp = await axios.get(
+ `${SRC_AJAX_URL}/v2/episode/servers?episodeId=${epId.split("?ep=")[1]}`,
+ {
+ headers: {
+ Referer: epId,
+ "User-Agent": USER_AGENT_HEADER,
+ "X-Requested-With": "XMLHttpRequest",
+ },
+ }
+ );
+ // console.log(resp, "\n\n");
+
+ const $: CheerioAPI = load(resp.data.html);
+
+ let serverId: string | null = null;
+
+ try {
+ console.log("THIS IS THE SERVER: ", server);
+
+ switch (server) {
+ case Servers.VidCloud: {
+ serverId = retrieveServerId($, 1, category);
+ if (!serverId) throw new Error("RapidCloud not found");
+ break;
+ }
+ case Servers.VidStreaming: {
+ serverId = retrieveServerId($, 4, category);
+ console.log("SERVER_ID: ", serverId);
+ if (!serverId) throw new Error("VidStreaming not found");
+ break;
+ }
+ case Servers.StreamSB: {
+ serverId = retrieveServerId($, 5, category);
+ if (!serverId) throw new Error("StreamSB not found");
+ break;
+ }
+ case Servers.StreamTape: {
+ serverId = retrieveServerId($, 3, category);
+ if (!serverId) throw new Error("StreamTape not found");
+ break;
+ }
+ }
+ } catch (err) {
+ throw createHttpError.NotFound(
+ "Couldn't find server. Try another server"
+ );
+ }
+
+ const {
+ data: { link },
+ } = await axios.get(`${SRC_AJAX_URL}/v2/episode/sources?id=${serverId}`);
+ console.log("THE LINK: ", link);
+
+ return await scrapeAnimeEpisodeSources(link, server);
+ } catch (err: any) {
+ console.log(err);
+ if (err instanceof AxiosError) {
+ throw createHttpError(
+ err?.response?.status || 500,
+ err?.response?.statusText || "Something went wrong"
+ );
+ }
+ throw createHttpError.InternalServerError(err?.message);
+ }
+}
+
+export default scrapeAnimeEpisodeSources;