Update anime/hianime/source.js

This commit is contained in:
2025-12-24 17:32:32 +01:00
parent cace57b460
commit 45a072c272

View File

@@ -1,25 +1,26 @@
console.log("[HiAnime-TV] source.js loaded"); console.log("[HiAnime-TV] source.js loaded");
function HiAnime() { class HiAnime {
constructor() {
console.log("[HiAnime-TV] constructor()"); console.log("[HiAnime-TV] constructor()");
this.type = "anime-board"; this.type = "anime-board";
this.version = "1.1"; this.version = "1.1";
this.baseUrl = "https://hianime.to"; this.baseUrl = "https://hianime.to";
} }
HiAnime.prototype.getSettings = function () { getSettings() {
console.log("[HiAnime-TV] getSettings()"); console.log("[HiAnime-TV] getSettings()");
return { return {
episodeServers: ["HD-1", "HD-2", "HD-3", "HD-4"], episodeServers: ["HD-1", "HD-2", "HD-3", "HD-4"],
supportsDub: true supportsDub: true
}; };
}; }
HiAnime.prototype.safeString = function (str) { safeString(str) {
return (typeof str === "string" ? str : ""); return (typeof str === "string" ? str : "");
}; }
HiAnime.prototype._decodeHtml = function (s) { _decodeHtml(s) {
return this.safeString(s) return this.safeString(s)
.replace(/&/g, "&") .replace(/&/g, "&")
.replace(/"/g, '"') .replace(/"/g, '"')
@@ -31,13 +32,13 @@ HiAnime.prototype._decodeHtml = function (s) {
var n = parseInt(d, 10); var n = parseInt(d, 10);
return (typeof n === "number" && !isNaN(n)) ? String.fromCharCode(n) : m; return (typeof n === "number" && !isNaN(n)) ? String.fromCharCode(n) : m;
}); });
}; }
HiAnime.prototype._escapeRe = function (s) { _escapeRe(s) {
return this.safeString(s).replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); return this.safeString(s).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}; }
HiAnime.prototype._fetch = function (url, opts) { _fetch(url, opts) {
var o = opts || {}; var o = opts || {};
var headers = {}; var headers = {};
@@ -45,6 +46,7 @@ HiAnime.prototype._fetch = function (url, opts) {
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"; "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36";
headers["Accept"] = "text/html,application/json;q=0.9,*/*;q=0.8"; headers["Accept"] = "text/html,application/json;q=0.9,*/*;q=0.8";
headers["Referer"] = this.baseUrl + "/"; headers["Referer"] = this.baseUrl + "/";
headers["Accept-Language"] = "en-US,en;q=0.9";
if (o.headers && typeof o.headers === "object") { if (o.headers && typeof o.headers === "object") {
for (var k in o.headers) { for (var k in o.headers) {
@@ -55,23 +57,30 @@ HiAnime.prototype._fetch = function (url, opts) {
} }
var method = o.method || "GET"; var method = o.method || "GET";
var body = (o.body === undefined || o.body === null) ? "" : String(o.body);
console.log( var init = { method: method, headers: headers };
"[HiAnime-TV] fetch", if (method !== "GET" && method !== "HEAD") {
method, var body = (o.body === undefined || o.body === null) ? "" : String(o.body);
url, init.body = body;
"headers=", }
Object.keys(headers).length,
"bodyLen=", console.log("[HiAnime-TV] fetch", method, url, "headers=", Object.keys(headers).length);
body.length var r = fetch(url, init);
);
var r = fetch(url, { method: method, headers: headers, body: body });
console.log("[HiAnime-TV] fetchResp status=", r.status, "ok=", r.ok); console.log("[HiAnime-TV] fetchResp status=", r.status, "ok=", r.ok);
return r; return r;
}; }
HiAnime.prototype.search = function (query) { _isBlockedHtml(html) {
var h = this.safeString(html).toLowerCase();
return (
h.indexOf("cloudflare") !== -1 ||
h.indexOf("just a moment") !== -1 ||
h.indexOf("cf-challenge") !== -1 ||
h.indexOf("attention required") !== -1
);
}
search(query) {
console.log("[HiAnime-TV] search() raw=", query); console.log("[HiAnime-TV] search() raw=", query);
try { try {
@@ -90,15 +99,9 @@ HiAnime.prototype.search = function (query) {
var start = (q.media && q.media.startDate) ? q.media.startDate : {}; var start = (q.media && q.media.startDate) ? q.media.startDate : {};
var year = start.year || ""; var year = start.year || "";
var month = start.month || ""; var month = start.month || "";
var dub = !!q.dub;
console.log( console.log("[HiAnime-TV] search() parsed queryText=", queryText, "year=", year, "month=", month, "dub=", dub);
"[HiAnime-TV] search() parsed queryText=",
queryText,
"year=",
year,
"month=",
month
);
if (!queryText) return []; if (!queryText) return [];
@@ -112,23 +115,32 @@ HiAnime.prototype.search = function (query) {
console.log("[HiAnime-TV] search() url=", url); console.log("[HiAnime-TV] search() url=", url);
var html = this._fetch(url).text(); var html = this._fetch(url).text(); // sync in host
console.log("[HiAnime-TV] search() htmlLen=", (html || "").length); console.log("[HiAnime-TV] search() htmlLen=", (html || "").length);
var regex = /<a[^>]+href="\/watch\/([^"]+)"[^>]*data-id="(\d+)"[^>]*title="([^"]+)"/gi; if (this._isBlockedHtml(html)) {
throw new Error("HiAnime blocked the request (Cloudflare/bot protection).");
}
var cardRegex =
/<a[^>]*href="\/watch\/([^"]+)"[^>]*\btitle="([^"]+)"[^>]*\bdata-id="(\d+)"[^>]*>|<a[^>]*href="\/watch\/([^"]+)"[^>]*\bdata-id="(\d+)"[^>]*\btitle="([^"]+)"[^>]*>/gi;
var out = []; var out = [];
var match; var match;
while ((match = regex.exec(html)) !== null) { while ((match = cardRegex.exec(html)) !== null) {
var pagePath = match[1]; var pagePath = match[1] || match[4] || "";
var id = match[2]; var titleRaw = match[2] || match[6] || "";
var title = this._decodeHtml(match[3]); var id = match[3] || match[5] || "";
if (!pagePath || !id) continue;
var title = this._decodeHtml(titleRaw);
var image = null; var image = null;
try { try {
var imageRegex = new RegExp( var imageRegex = new RegExp(
"<a[^>]+href=\"/watch/" + "<a[^>]*href=\"/watch/" +
this._escapeRe(pagePath) + this._escapeRe(pagePath) +
"\"[\\s\\S]*?<img[^>]+(?:data-src|src)=\"([^\"]+)\"", "\"[\\s\\S]*?<img[^>]+(?:data-src|src)=\"([^\"]+)\"",
"i" "i"
@@ -138,32 +150,23 @@ HiAnime.prototype.search = function (query) {
} catch (e2) {} } catch (e2) {}
out.push({ out.push({
id: String(id), id: String(id) + "/" + (dub ? "dub" : "sub"),
title: title, title: title,
image: image, image: image,
url: this.baseUrl + "/watch/" + pagePath, url: this.baseUrl + "/watch/" + pagePath,
subOrDub: "sub" subOrDub: dub ? "dub" : "sub"
}); });
} }
console.log( console.log("[HiAnime-TV] search() returning=", out.length, "first=", out[0] ? out[0].title : "none");
"[HiAnime-TV] search() returning=",
out.length,
"first=",
out[0] ? out[0].title : "none"
);
return out; return out;
} catch (e) { } catch (e) {
console.error( console.error("[HiAnime-TV] search() ERROR", String(e), e && e.stack ? e.stack : "");
"[HiAnime-TV] search() ERROR",
String(e),
e && e.stack ? e.stack : ""
);
throw e; throw e;
} }
}; }
HiAnime.prototype.findEpisodes = function (animeId) { findEpisodes(animeId) {
console.log("[HiAnime-TV] findEpisodes() animeId=", animeId); console.log("[HiAnime-TV] findEpisodes() animeId=", animeId);
try { try {
var raw = animeId; var raw = animeId;
@@ -203,14 +206,7 @@ HiAnime.prototype.findEpisodes = function (animeId) {
var json = res.json(); var json = res.json();
var html = this.safeString(json.html); var html = this.safeString(json.html);
console.log( console.log("[HiAnime-TV] findEpisodes() status=", res.status, "ok=", res.ok, "htmlLen=", html.length);
"[HiAnime-TV] findEpisodes() status=",
res.status,
"ok=",
res.ok,
"htmlLen=",
html.length
);
var episodes = []; var episodes = [];
var regex = var regex =
@@ -227,24 +223,15 @@ HiAnime.prototype.findEpisodes = function (animeId) {
}); });
} }
console.log( console.log("[HiAnime-TV] findEpisodes() returning=", episodes.length, "firstEp=", episodes[0] ? episodes[0].number : "none");
"[HiAnime-TV] findEpisodes() returning=",
episodes.length,
"firstEp=",
episodes[0] ? episodes[0].number : "none"
);
return episodes; return episodes;
} catch (e) { } catch (e) {
console.error( console.error("[HiAnime-TV] findEpisodes() ERROR", String(e), e && e.stack ? e.stack : "");
"[HiAnime-TV] findEpisodes() ERROR",
String(e),
e && e.stack ? e.stack : ""
);
throw e; throw e;
} }
}; }
HiAnime.prototype.findEpisodeServer = function (episode, _server) { findEpisodeServer(episode, _server) {
console.log("[HiAnime-TV] findEpisodeServer() episode=", episode, "server=", _server); console.log("[HiAnime-TV] findEpisodeServer() episode=", episode, "server=", _server);
try { try {
var ep = episode; var ep = episode;
@@ -281,14 +268,7 @@ HiAnime.prototype.findEpisodeServer = function (episode, _server) {
if (!episodeId) throw new Error("Missing episode id"); if (!episodeId) throw new Error("Missing episode id");
var serverName = (_server && _server !== "default") ? String(_server) : "HD-1"; var serverName = (_server && _server !== "default") ? String(_server) : "HD-1";
console.log( console.log("[HiAnime-TV] parsed episodeId=", episodeId, "subOrDub=", subOrDub, "serverName=", serverName);
"[HiAnime-TV] parsed episodeId=",
episodeId,
"subOrDub=",
subOrDub,
"serverName=",
serverName
);
if (serverName === "HD-1" || serverName === "HD-2" || serverName === "HD-3") { if (serverName === "HD-1" || serverName === "HD-2" || serverName === "HD-3") {
var serversUrl = this.baseUrl + "/ajax/v2/episode/servers?episodeId=" + encodeURIComponent(episodeId); var serversUrl = this.baseUrl + "/ajax/v2/episode/servers?episodeId=" + encodeURIComponent(episodeId);
@@ -325,10 +305,7 @@ HiAnime.prototype.findEpisodeServer = function (episode, _server) {
try { try {
decryptData = this.extractMegaCloud(sourcesJson.link, true); decryptData = this.extractMegaCloud(sourcesJson.link, true);
if (decryptData && decryptData.headersProvided) requiredHeaders = decryptData.headersProvided; if (decryptData && decryptData.headersProvided) requiredHeaders = decryptData.headersProvided;
console.log( console.log("[HiAnime-TV] extractMegaCloud ok sources=", (decryptData && decryptData.sources ? decryptData.sources.length : 0));
"[HiAnime-TV] extractMegaCloud ok sources=",
(decryptData && decryptData.sources ? decryptData.sources.length : 0)
);
} catch (err) { } catch (err) {
console.warn("[HiAnime-TV] Primary decrypter failed:", String(err)); console.warn("[HiAnime-TV] Primary decrypter failed:", String(err));
} }
@@ -351,10 +328,7 @@ HiAnime.prototype.findEpisodeServer = function (episode, _server) {
"X-Requested-With": "XMLHttpRequest" "X-Requested-With": "XMLHttpRequest"
}; };
console.log( console.log("[HiAnime-TV] fallback decrypt sources=", (decryptData && decryptData.sources ? decryptData.sources.length : 0));
"[HiAnime-TV] fallback decrypt sources=",
(decryptData && decryptData.sources ? decryptData.sources.length : 0)
);
} }
var sourcesArr = (decryptData && decryptData.sources) ? decryptData.sources : []; var sourcesArr = (decryptData && decryptData.sources) ? decryptData.sources : [];
@@ -362,19 +336,12 @@ HiAnime.prototype.findEpisodeServer = function (episode, _server) {
for (var i = 0; i < sourcesArr.length; i++) { for (var i = 0; i < sourcesArr.length; i++) {
var s = sourcesArr[i]; var s = sourcesArr[i];
if (s && s.type === "hls" && s.file) { if (s && s.type === "hls" && s.file) { streamSource = s; break; }
streamSource = s;
break;
} }
}
if (!streamSource) { if (!streamSource) {
for (var j = 0; j < sourcesArr.length; j++) { for (var j = 0; j < sourcesArr.length; j++) {
var s2 = sourcesArr[j]; var s2 = sourcesArr[j];
if (s2 && s2.type === "mp4" && s2.file) { if (s2 && s2.type === "mp4" && s2.file) { streamSource = s2; break; }
streamSource = s2;
break;
}
} }
} }
@@ -394,14 +361,7 @@ HiAnime.prototype.findEpisodeServer = function (episode, _server) {
} }
} }
console.log( console.log("[HiAnime-TV] FINAL stream file=", streamSource.file, "type=", streamSource.type, "subs=", subtitles.length);
"[HiAnime-TV] FINAL stream file=",
streamSource.file,
"type=",
streamSource.type,
"subs=",
subtitles.length
);
return { return {
server: serverName, server: serverName,
@@ -423,16 +383,12 @@ HiAnime.prototype.findEpisodeServer = function (episode, _server) {
console.warn("[HiAnime-TV] Unsupported server=", serverName); console.warn("[HiAnime-TV] Unsupported server=", serverName);
return null; return null;
} catch (e) { } catch (e) {
console.error( console.error("[HiAnime-TV] findEpisodeServer() ERROR", String(e), e && e.stack ? e.stack : "");
"[HiAnime-TV] findEpisodeServer() ERROR",
String(e),
e && e.stack ? e.stack : ""
);
throw e; throw e;
} }
}; }
HiAnime.prototype.extractMegaCloud = function (embedUrl, returnHeaders) { extractMegaCloud(embedUrl, returnHeaders) {
console.log("[HiAnime-TV] extractMegaCloud() embedUrl=", embedUrl, "returnHeaders=", returnHeaders); console.log("[HiAnime-TV] extractMegaCloud() embedUrl=", embedUrl, "returnHeaders=", returnHeaders);
var url = new URL(embedUrl); var url = new URL(embedUrl);
@@ -450,14 +406,7 @@ HiAnime.prototype.extractMegaCloud = function (embedUrl, returnHeaders) {
var r = this._fetch(embedUrl, { headers: headers }); var r = this._fetch(embedUrl, { headers: headers });
var html = r.text(); var html = r.text();
console.log( console.log("[HiAnime-TV] extractMegaCloud() embed status=", r.status, "ok=", r.ok, "htmlLen=", (html || "").length);
"[HiAnime-TV] extractMegaCloud() embed status=",
r.status,
"ok=",
r.ok,
"htmlLen=",
(html || "").length
);
var fileIdMatch = html.match(/<title>\s*File\s+#([a-zA-Z0-9]+)\s*-/i); var fileIdMatch = html.match(/<title>\s*File\s+#([a-zA-Z0-9]+)\s*-/i);
if (!fileIdMatch) throw new Error("file_id not found in embed page"); if (!fileIdMatch) throw new Error("file_id not found in embed page");
@@ -471,12 +420,8 @@ HiAnime.prototype.extractMegaCloud = function (embedUrl, returnHeaders) {
var regex16 = /["']([A-Za-z0-9]{16})["']/g; var regex16 = /["']([A-Za-z0-9]{16})["']/g;
var matches = []; var matches = [];
var m; var m;
while ((m = regex16.exec(html)) !== null) { while ((m = regex16.exec(html)) !== null) matches.push(m[1]);
matches.push(m[1]); if (matches.length >= 3) nonce = matches[0] + matches[1] + matches[2];
}
if (matches.length >= 3) {
nonce = matches[0] + matches[1] + matches[2];
}
} }
if (!nonce) throw new Error("nonce not found"); if (!nonce) throw new Error("nonce not found");
@@ -486,15 +431,9 @@ HiAnime.prototype.extractMegaCloud = function (embedUrl, returnHeaders) {
var sr = this._fetch(sourcesUrl, { headers: headers }); var sr = this._fetch(sourcesUrl, { headers: headers });
var sourcesJson = sr.json(); var sourcesJson = sr.json();
console.log( console.log("[HiAnime-TV] extractMegaCloud() sources status=", sr.status, "ok=", sr.ok,
"[HiAnime-TV] extractMegaCloud() sources status=", "sources=", (sourcesJson.sources ? sourcesJson.sources.length : 0),
sr.status, "tracks=", (sourcesJson.tracks ? sourcesJson.tracks.length : 0)
"ok=",
sr.ok,
"sources=",
(sourcesJson.sources ? sourcesJson.sources.length : 0),
"tracks=",
(sourcesJson.tracks ? sourcesJson.tracks.length : 0)
); );
return { return {
@@ -505,7 +444,8 @@ HiAnime.prototype.extractMegaCloud = function (embedUrl, returnHeaders) {
server: sourcesJson.server || null, server: sourcesJson.server || null,
headersProvided: returnHeaders ? headers : undefined headersProvided: returnHeaders ? headers : undefined
}; };
}; }
}
module.exports = HiAnime; module.exports = HiAnime;
console.log("[HiAnime-TV] module.exports set"); console.log("[HiAnime-TV] module.exports set");