]*>([\s\S]*?)<\/div>/;
+ const dubRegex = /
]*>([\s\S]*?)<\/div>/;
+ const subMatch = subRegex.exec(cleanedHtml);
+ const softsubMatch = softsubRegex.exec(cleanedHtml);
+ const dubMatch = dubRegex.exec(cleanedHtml);
+ const subContent = subMatch ? subMatch[1].trim() : "";
+ const softsubContent = softsubMatch ? softsubMatch[1].trim() : "";
+ const dubContent = dubMatch ? dubMatch[1].trim() : "";
+ const serverSpanRegex = /]*data-lid="([^"]+)"[^>]*>Server 1<\/span>/;
+ const serverIdDub = serverSpanRegex.exec(dubContent)?.[1];
+ const serverIdSoftsub = serverSpanRegex.exec(softsubContent)?.[1];
+ const serverIdSub = serverSpanRegex.exec(subContent)?.[1];
+
+ const tokenRequestData = [
+ { name: "Dub", data: serverIdDub },
+ { name: "Softsub", data: serverIdSoftsub },
+ { name: "Sub", data: serverIdSub }
+ ].filter(item => item.data);
+
+ const tokenPromises = tokenRequestData.map(item =>
+ fetchv2(`https://enc-dec.app/api/enc-kai?text=${encodeURIComponent(item.data)}`)
+ .then(res => res.json())
+ .then(json => ({ name: item.name, data: json.result }))
+ .catch(err => ({ name: item.name, error: err.toString() }))
+ );
+ const tokenResults = await Promise.all(tokenPromises);
+
+ const streamUrls = tokenResults.map(result => {
+ const serverIdMap = {
+ "Dub": serverIdDub,
+ "Softsub": serverIdSoftsub,
+ "Sub": serverIdSub
+ };
+ return {
+ type: result.name,
+ url: `https://anikai.to/ajax/links/view?id=${serverIdMap[result.name]}&_=${result.data}`
+ };
+ });
+
+ const processStreams = async (streamUrls) => {
+ const streamResponses = await Promise.all(
+ streamUrls.map(async ({ type, url }) => {
+ try {
+ const res = await fetchv2("https://deno-proxies-sznvnpnxwhbv.deno.dev/?url=" + encodeURIComponent(url));
+ const json = await res.json();
+ return {
+ type: type,
+ result: json.result
+ };
+ } catch (error) {
+ console.log(`Error fetching ${type} stream:` + error);
+ return {
+ type: type,
+ result: null
+ };
+ }
+ })
+ );
+
+ const decryptRequestData = streamResponses
+ .filter(item => item.result)
+ .map(item => ({
+ name: item.type,
+ data: item.result
+ }));
+
+ if (decryptRequestData.length === 0) {
+ return {};
+ }
+
+ const decryptPromises = decryptRequestData.map(item =>
+ fetchv2(`https://enc-dec.app/api/dec-kai?text=${encodeURIComponent(item.data)}`)
+ .then(res => res.json())
+ .then(json => ({ name: item.name, data: JSON.stringify(json.result) }))
+ .catch(err => ({ name: item.name, error: err.toString() }))
+ );
+ const decryptResults = await Promise.all(decryptPromises);
+
+ const finalResults = {};
+ decryptResults.forEach(result => {
+ try {
+ const parsed = JSON.parse(result.data);
+ finalResults[result.name] = parsed.url;
+ console.log(`decrypted${result.name} URL:` + parsed.url);
+ } catch (error) {
+ console.log(`Error parsing ${result.name} result:` + error);
+ finalResults[result.name] = null;
+ }
+ });
+
+ return finalResults;
+ };
+
+ const decryptedUrls = await processStreams(streamUrls);
+ const decryptedSub = decryptedUrls.Sub;
+ const decryptedDub = decryptedUrls.Dub;
+ const decryptedRaw = decryptedUrls.Softsub;
+
+ const headers = {
+ "Referer": "https://anikai.to/",
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36"
+ };
+
+ async function getStream(url) {
+ try {
+ const response = await fetchv2(url.replace("/e/", "/media/"), headers);
+ const responseJson = await response.json();
+
+ const result = responseJson?.result;
+
+ const postData = {
+ "text": result,
+ "agent": headers["User-Agent"]
+ };
+
+ const finalResponse = await fetchv2(
+ "https://enc-dec.app/api/dec-mega",
+ { "Content-Type": "application/json" },
+ "POST",
+ JSON.stringify(postData)
+ );
+
+ const finalJson = await finalResponse.json();
+ return finalJson?.result?.sources?.[0]?.file || null;
+ } catch {
+ return null;
+ }
+ }
+
+ const streams = [];
+
+ const [subStream, dubStream, rawStream] = await Promise.all([
+ decryptedSub ? getStream(decryptedSub) : Promise.resolve(null),
+ decryptedDub ? getStream(decryptedDub) : Promise.resolve(null),
+ decryptedRaw ? getStream(decryptedRaw) : Promise.resolve(null)
+ ]);
+
+ if (subStream) streams.push({ title: "Hardsub English", streamUrl: subStream });
+ if (dubStream) streams.push({ title: "Dubbed English", streamUrl: dubStream });
+ if (rawStream) streams.push({ title: "Original audio", streamUrl: rawStream });
+
+ const final = {
+ streams,
+ subtitles: ""
+ };
+
+ console.log("RETURN: " + JSON.stringify(final));
+ return JSON.stringify(final);
+
+ } catch (error) {
+ console.log("Animekai fetch error:" + error);
+ return "https://error.org";
+ }
+}
+
+function cleanHtmlSymbols(string) {
+ if (!string) {
+ return "";
+ }
+ return string
+ .replace(/’/g, "'")
+ .replace(/–/g, "-")
+ .replace(/[0-9]+;/g, "")
+ .replace(/\r?\n|\r/g, " ")
+ .replace(/\s+/g, " ")
+ .trim();
+}
+
+function cleanJsonHtml(jsonHtml) {
+ if (!jsonHtml) {
+ return "";
+ }
+ return jsonHtml
+ .replace(/\\"/g, "\"")
+ .replace(/\\'/g, "'")
+ .replace(/\\\\/g, "\\")
+ .replace(/\\n/g, "\n")
+ .replace(/\\t/g, "\t")
+ .replace(/\\r/g, "\r");
+}
+
+function decodeHtmlEntities(text) {
+ if (!text) {
+ return "";
+ }
+ return text
+ .replace(/'/g, "'")
+ .replace(/"/g, "\"")
+ .replace(/&/g, "&")
+ .replace(/</g, "<")
+ .replace(/>/g, ">")
+ .replace(/ /g, " ");
+}