]*\\bserver-item\\b[^>]*data-type="${subOrDub}"[^>]*data-id="(\\d+)"[\\s\\S]*?>[\\s\\S]*?${escapedName}[\\s\\S]*?<\\/div>`,
+ "i"
);
- try {
- let episodeId, subOrDub;
+ let match = blockRe.exec(serverHtml);
- if (typeof episode === "string") {
- if (episode.includes("/")) {
- [episodeId, subOrDub] = episode.split("/");
- } else {
- try {
- const parsed = JSON.parse(episode);
- episodeId = parsed.id || parsed.episodeId || episode;
- subOrDub = parsed.subOrDub || "sub";
- } catch (e) {
- episodeId = episode;
- subOrDub = "sub";
- }
- }
- } else if (typeof episode === "object" && episode) {
- const epId = episode.id || episode.episodeId || "";
- if (epId.includes("/")) {
- [episodeId, subOrDub] = epId.split("/");
- } else {
- episodeId = epId;
- subOrDub = episode.subOrDub || "sub";
- }
- } else {
- episodeId = String(episode);
- subOrDub = "sub";
- }
-
- if (episodeId && episodeId.includes("/"))
- [episodeId, subOrDub] = episodeId.split("/");
-
- console.log(
- "[HiAnime] Parsed server params: episodeId=",
- episodeId,
- "subOrDub=",
- subOrDub
- );
-
- let serverName =
- _server && _server !== "default" ? _server : "HD-1";
- console.log("[HiAnime] Using server:", serverName);
-
- const serversUrl = `${this.baseUrl}/ajax/v2/episode/servers?episodeId=${episodeId}`;
- console.log("[HiAnime] Fetching servers from:", serversUrl);
-
- const serverResponse = this.fetchAjax(serversUrl);
- console.log("[HiAnime] Servers fetch status:", serverResponse.status);
-
- const serverJson = serverResponse.json();
- const serverHtml = this.safeString(serverJson.html);
- console.log("[HiAnime] Server HTML length:", serverHtml.length);
-
- const escapedName = serverName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
-
- const blockRe = new RegExp(
- `
]*\\bserver-item\\b[^>]*data-type="${subOrDub}"[^>]*data-id="(\\d+)"[\\s\\S]*?>[\\s\\S]*?${escapedName}[\\s\\S]*?<\\/div>`,
+ if (!match) {
+ const altRe = new RegExp(
+ `data-type="${subOrDub}"[^>]*data-id="(\\d+)"[\\s\\S]*?>[\\s\\S]*?
]*>[\\s\\S]*?${escapedName}[\\s\\S]*?<\\/a>`,
"i"
);
-
- let match = blockRe.exec(serverHtml);
-
- if (!match) {
- const altRe = new RegExp(
- `data-type="${subOrDub}"[^>]*data-id="(\\d+)"[\\s\\S]*?>[\\s\\S]*?]*>[\\s\\S]*?${escapedName}[\\s\\S]*?<\\/a>`,
- "i"
- );
- match = altRe.exec(serverHtml);
- }
-
- if (!match) {
- console.error(
- `[HiAnime] Server "${serverName}" (${subOrDub}) not found in HTML`
- );
- console.warn(
- "[HiAnime] Server HTML preview:",
- serverHtml.slice(0, 300)
- );
- throw new Error(`Server "${serverName}" (${subOrDub}) not found`);
- }
-
- const serverId = match[1];
- console.log("[HiAnime] Found serverId:", serverId);
-
- const sourcesUrl = `${this.baseUrl}/ajax/v2/episode/sources?id=${serverId}`;
- console.log("[HiAnime] Fetching sources from:", sourcesUrl);
-
- const sourcesResponse = this.fetchAjax(sourcesUrl);
- console.log("[HiAnime] Sources fetch status:", sourcesResponse.status);
-
- const sourcesJson = sourcesResponse.json();
- console.log("[HiAnime] Sources JSON link:", sourcesJson.link);
-
- let decryptData = null;
- let requiredHeaders = {};
-
- try {
- console.log("[HiAnime] Attempting primary decrypter...");
- decryptData = this.extractMegaCloud(sourcesJson.link, true);
- if (decryptData && decryptData.headersProvided) {
- requiredHeaders = decryptData.headersProvided;
- }
- console.log("[HiAnime] Primary decrypter succeeded");
- } catch (err) {
- console.warn("[HiAnime] Primary decrypter failed:", err);
- }
-
- if (!decryptData) {
- console.log("[HiAnime] Trying fallback API...");
- const fallbackUrl = `https://ac-api.ofchaos.com/api/anime/embed/convert/v2?embedUrl=${encodeURIComponent(
- sourcesJson.link
- )}`;
- const fallbackResponse = fetch(fallbackUrl);
- console.log("[HiAnime] Fallback fetch status:", fallbackResponse.status);
-
- decryptData = fallbackResponse.json();
-
- requiredHeaders = {
- "Referer": "https://megacloud.club/",
- "Origin": "https://megacloud.club",
- "User-Agent":
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
- "X-Requested-With": "XMLHttpRequest"
- };
- console.log("[HiAnime] Fallback succeeded");
- }
-
- const sourcesArr = decryptData && decryptData.sources ? decryptData.sources : [];
- const streamSource =
- sourcesArr.find((s) => s && s.type === "hls") ||
- sourcesArr.find((s) => s && s.type === "mp4");
-
- if (!streamSource || !streamSource.file) {
- console.error("[HiAnime] No valid stream file found in sources");
- throw new Error("No valid stream file found");
- }
-
- console.log("[HiAnime] Stream URL:", streamSource.file);
-
- const tracksArr = decryptData && decryptData.tracks ? decryptData.tracks : [];
- const subtitles = tracksArr
- .filter((t) => t && t.kind === "captions" && t.file)
- .map((track, index) => ({
- id: `sub-${index}`,
- language: track.label || "Unknown",
- url: track.file,
- isDefault: !!track.default
- }));
-
- console.log("[HiAnime] Found", subtitles.length, "subtitles");
-
- const result = {
- server: serverName,
- headers: requiredHeaders,
- videoSources: [
- {
- url: streamSource.file,
- type: streamSource.type === "hls" ? "m3u8" : "mp4",
- quality: "auto",
- subtitles
- }
- ]
- };
-
- console.log("[HiAnime] Returning server result");
- return result;
- } catch (error) {
- console.error("[HiAnime] findEpisodeServer error:", error);
- throw error;
+ match = altRe.exec(serverHtml);
}
+
+ if (!match) {
+ throw new Error(`Server "${serverName}" (${subOrDub}) not found`);
+ }
+
+ const serverId = match[1];
+
+ const sourcesUrl = `${this.baseUrl}/ajax/v2/episode/sources?id=${serverId}`;
+ const sourcesResponse = this.fetchAjax(sourcesUrl);
+ const sourcesJson = sourcesResponse.json();
+
+ let decryptData = null;
+ let requiredHeaders = {};
+
+ try {
+ decryptData = this.extractMegaCloud(sourcesJson.link, true);
+ if (decryptData && decryptData.headersProvided) {
+ requiredHeaders = decryptData.headersProvided;
+ }
+ } catch (err) {}
+
+ if (!decryptData) {
+ const fallbackUrl = `https://ac-api.ofchaos.com/api/anime/embed/convert/v2?embedUrl=${encodeURIComponent(
+ sourcesJson.link
+ )}`;
+ const fallbackResponse = fetch(fallbackUrl);
+ decryptData = fallbackResponse.json();
+
+ requiredHeaders = {
+ "Referer": "https://megacloud.club/",
+ "Origin": "https://megacloud.club",
+ "User-Agent":
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
+ "X-Requested-With": "XMLHttpRequest"
+ };
+ }
+
+ const sourcesArr = decryptData && decryptData.sources ? decryptData.sources : [];
+ const hls = sourcesArr.find((s) => s && s.type === "hls" && s.file);
+ const mp4 = sourcesArr.find((s) => s && s.type === "mp4" && s.file);
+
+ const streamSource = hls || mp4;
+ if (!streamSource || !streamSource.file) {
+ throw new Error("No valid stream file found");
+ }
+
+ const tracksArr = decryptData && decryptData.tracks ? decryptData.tracks : [];
+ const subtitles = tracksArr
+ .filter((t) => t && t.kind === "captions" && t.file)
+ .map((track, index) => ({
+ id: `sub-${index}`,
+ language: track.label || "Unknown",
+ url: track.file,
+ isDefault: !!track.default
+ }));
+
+ return {
+ server: serverName,
+ headers: requiredHeaders,
+ videoSources: [
+ {
+ url: streamSource.file,
+ type: streamSource.type === "hls" ? "m3u8" : "mp4",
+ quality: "auto",
+ subtitles
+ }
+ ]
+ };
}
extractMegaCloud(embedUrl, returnHeaders) {
- console.log("[HiAnime] extractMegaCloud called with:", embedUrl);
+ const url = new URL(embedUrl);
+ const baseDomain = `${url.protocol}//${url.host}/`;
- try {
- const url = new URL(embedUrl);
- const baseDomain = `${url.protocol}//${url.host}/`;
+ const headers = {
+ "Accept": "*/*",
+ "X-Requested-With": "XMLHttpRequest",
+ "Referer": baseDomain,
+ "Origin": `${url.protocol}//${url.host}`,
+ "User-Agent":
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36"
+ };
- const headers = {
- "Accept": "*/*",
- "X-Requested-With": "XMLHttpRequest",
- "Referer": baseDomain,
- "Origin": `${url.protocol}//${url.host}`,
- "User-Agent":
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36"
- };
+ const response = fetch(embedUrl, { headers });
+ const html = response.text();
- const response = fetch(embedUrl, { headers });
- console.log("[HiAnime] MegaCloud embed fetch status:", response.status);
-
- const html = response.text();
- console.log("[HiAnime] MegaCloud HTML length:", html.length);
-
- const fileIdMatch = html.match(/\s*File\s+#([a-zA-Z0-9]+)\s*-/i);
- if (!fileIdMatch) {
- console.error("[HiAnime] file_id not found in embed page");
- throw new Error("file_id not found in embed page");
- }
- const fileId = fileIdMatch[1];
- console.log("[HiAnime] Found fileId:", fileId);
-
- let nonce = null;
- const match48 = html.match(/\b[a-zA-Z0-9]{48}\b/);
- if (match48) {
- nonce = match48[0];
- console.log("[HiAnime] Found 48-char nonce");
- } else {
- const match3x16 = [...html.matchAll(/["']([A-Za-z0-9]{16})["']/g)];
- if (match3x16.length >= 3) {
- nonce = match3x16[0][1] + match3x16[1][1] + match3x16[2][1];
- console.log("[HiAnime] Found 3x16-char nonce");
- }
- }
-
- if (!nonce) {
- console.error("[HiAnime] nonce not found");
- throw new Error("nonce not found");
- }
-
- const sourcesUrl = `${baseDomain}embed-2/v3/e-1/getSources?id=${fileId}&_k=${nonce}`;
- console.log("[HiAnime] Fetching sources from:", sourcesUrl);
-
- const sourcesResponse = fetch(sourcesUrl, { headers });
- console.log("[HiAnime] Sources fetch status:", sourcesResponse.status);
-
- const sourcesJson = sourcesResponse.json();
- console.log(
- "[HiAnime] Sources has",
- (sourcesJson.sources || []).length,
- "sources"
- );
-
- return {
- sources: sourcesJson.sources || [],
- tracks: sourcesJson.tracks || [],
- intro: sourcesJson.intro || null,
- outro: sourcesJson.outro || null,
- server: sourcesJson.server || null,
- headersProvided: returnHeaders ? headers : undefined
- };
- } catch (error) {
- console.error("[HiAnime] extractMegaCloud error:", error);
- throw error;
+ const fileIdMatch = html.match(/\s*File\s+#([a-zA-Z0-9]+)\s*-/i);
+ if (!fileIdMatch) {
+ throw new Error("file_id not found in embed page");
}
+ const fileId = fileIdMatch[1];
+
+ let nonce = null;
+ const match48 = html.match(/\b[a-zA-Z0-9]{48}\b/);
+ if (match48) {
+ nonce = match48[0];
+ } else {
+ const match3x16 = [...html.matchAll(/["']([A-Za-z0-9]{16})["']/g)];
+ if (match3x16.length >= 3) {
+ nonce = match3x16[0][1] + match3x16[1][1] + match3x16[2][1];
+ }
+ }
+
+ if (!nonce) {
+ throw new Error("nonce not found");
+ }
+
+ const sourcesUrl = `${baseDomain}embed-2/v3/e-1/getSources?id=${fileId}&_k=${nonce}`;
+ const sourcesResponse = fetch(sourcesUrl, { headers });
+ const sourcesJson = sourcesResponse.json();
+
+ return {
+ sources: sourcesJson.sources || [],
+ tracks: sourcesJson.tracks || [],
+ intro: sourcesJson.intro || null,
+ outro: sourcesJson.outro || null,
+ server: sourcesJson.server || null,
+ headersProvided: returnHeaders ? headers : undefined
+ };
}
}