Update anime/hianime/source.js
This commit is contained in:
@@ -1,25 +1,26 @@
|
||||
console.log("[HiAnime-TV] source.js loaded");
|
||||
|
||||
function HiAnime() {
|
||||
class HiAnime {
|
||||
constructor() {
|
||||
console.log("[HiAnime-TV] constructor()");
|
||||
this.type = "anime-board";
|
||||
this.version = "1.1";
|
||||
this.baseUrl = "https://hianime.to";
|
||||
}
|
||||
|
||||
HiAnime.prototype.getSettings = function () {
|
||||
getSettings() {
|
||||
console.log("[HiAnime-TV] getSettings()");
|
||||
return {
|
||||
episodeServers: ["HD-1", "HD-2", "HD-3", "HD-4"],
|
||||
supportsDub: true
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
HiAnime.prototype.safeString = function (str) {
|
||||
safeString(str) {
|
||||
return (typeof str === "string" ? str : "");
|
||||
};
|
||||
}
|
||||
|
||||
HiAnime.prototype._decodeHtml = function (s) {
|
||||
_decodeHtml(s) {
|
||||
return this.safeString(s)
|
||||
.replace(/&/g, "&")
|
||||
.replace(/"/g, '"')
|
||||
@@ -31,13 +32,13 @@ HiAnime.prototype._decodeHtml = function (s) {
|
||||
var n = parseInt(d, 10);
|
||||
return (typeof n === "number" && !isNaN(n)) ? String.fromCharCode(n) : m;
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
HiAnime.prototype._escapeRe = function (s) {
|
||||
_escapeRe(s) {
|
||||
return this.safeString(s).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
};
|
||||
}
|
||||
|
||||
HiAnime.prototype._fetch = function (url, opts) {
|
||||
_fetch(url, opts) {
|
||||
var o = opts || {};
|
||||
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";
|
||||
headers["Accept"] = "text/html,application/json;q=0.9,*/*;q=0.8";
|
||||
headers["Referer"] = this.baseUrl + "/";
|
||||
headers["Accept-Language"] = "en-US,en;q=0.9";
|
||||
|
||||
if (o.headers && typeof o.headers === "object") {
|
||||
for (var k in o.headers) {
|
||||
@@ -55,23 +57,30 @@ HiAnime.prototype._fetch = function (url, opts) {
|
||||
}
|
||||
|
||||
var method = o.method || "GET";
|
||||
var body = (o.body === undefined || o.body === null) ? "" : String(o.body);
|
||||
|
||||
console.log(
|
||||
"[HiAnime-TV] fetch",
|
||||
method,
|
||||
url,
|
||||
"headers=",
|
||||
Object.keys(headers).length,
|
||||
"bodyLen=",
|
||||
body.length
|
||||
);
|
||||
var r = fetch(url, { method: method, headers: headers, body: body });
|
||||
var init = { method: method, headers: headers };
|
||||
if (method !== "GET" && method !== "HEAD") {
|
||||
var body = (o.body === undefined || o.body === null) ? "" : String(o.body);
|
||||
init.body = body;
|
||||
}
|
||||
|
||||
console.log("[HiAnime-TV] fetch", method, url, "headers=", Object.keys(headers).length);
|
||||
var r = fetch(url, init);
|
||||
console.log("[HiAnime-TV] fetchResp status=", r.status, "ok=", r.ok);
|
||||
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);
|
||||
|
||||
try {
|
||||
@@ -90,15 +99,9 @@ HiAnime.prototype.search = function (query) {
|
||||
var start = (q.media && q.media.startDate) ? q.media.startDate : {};
|
||||
var year = start.year || "";
|
||||
var month = start.month || "";
|
||||
var dub = !!q.dub;
|
||||
|
||||
console.log(
|
||||
"[HiAnime-TV] search() parsed queryText=",
|
||||
queryText,
|
||||
"year=",
|
||||
year,
|
||||
"month=",
|
||||
month
|
||||
);
|
||||
console.log("[HiAnime-TV] search() parsed queryText=", queryText, "year=", year, "month=", month, "dub=", dub);
|
||||
|
||||
if (!queryText) return [];
|
||||
|
||||
@@ -112,23 +115,32 @@ HiAnime.prototype.search = function (query) {
|
||||
|
||||
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);
|
||||
|
||||
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 match;
|
||||
|
||||
while ((match = regex.exec(html)) !== null) {
|
||||
var pagePath = match[1];
|
||||
var id = match[2];
|
||||
var title = this._decodeHtml(match[3]);
|
||||
while ((match = cardRegex.exec(html)) !== null) {
|
||||
var pagePath = match[1] || match[4] || "";
|
||||
var titleRaw = match[2] || match[6] || "";
|
||||
var id = match[3] || match[5] || "";
|
||||
|
||||
if (!pagePath || !id) continue;
|
||||
|
||||
var title = this._decodeHtml(titleRaw);
|
||||
|
||||
var image = null;
|
||||
try {
|
||||
var imageRegex = new RegExp(
|
||||
"<a[^>]+href=\"/watch/" +
|
||||
"<a[^>]*href=\"/watch/" +
|
||||
this._escapeRe(pagePath) +
|
||||
"\"[\\s\\S]*?<img[^>]+(?:data-src|src)=\"([^\"]+)\"",
|
||||
"i"
|
||||
@@ -138,32 +150,23 @@ HiAnime.prototype.search = function (query) {
|
||||
} catch (e2) {}
|
||||
|
||||
out.push({
|
||||
id: String(id),
|
||||
id: String(id) + "/" + (dub ? "dub" : "sub"),
|
||||
title: title,
|
||||
image: image,
|
||||
url: this.baseUrl + "/watch/" + pagePath,
|
||||
subOrDub: "sub"
|
||||
subOrDub: dub ? "dub" : "sub"
|
||||
});
|
||||
}
|
||||
|
||||
console.log(
|
||||
"[HiAnime-TV] search() returning=",
|
||||
out.length,
|
||||
"first=",
|
||||
out[0] ? out[0].title : "none"
|
||||
);
|
||||
console.log("[HiAnime-TV] search() returning=", out.length, "first=", out[0] ? out[0].title : "none");
|
||||
return out;
|
||||
} catch (e) {
|
||||
console.error(
|
||||
"[HiAnime-TV] search() ERROR",
|
||||
String(e),
|
||||
e && e.stack ? e.stack : ""
|
||||
);
|
||||
console.error("[HiAnime-TV] search() ERROR", String(e), e && e.stack ? e.stack : "");
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
HiAnime.prototype.findEpisodes = function (animeId) {
|
||||
findEpisodes(animeId) {
|
||||
console.log("[HiAnime-TV] findEpisodes() animeId=", animeId);
|
||||
try {
|
||||
var raw = animeId;
|
||||
@@ -203,14 +206,7 @@ HiAnime.prototype.findEpisodes = function (animeId) {
|
||||
var json = res.json();
|
||||
var html = this.safeString(json.html);
|
||||
|
||||
console.log(
|
||||
"[HiAnime-TV] findEpisodes() status=",
|
||||
res.status,
|
||||
"ok=",
|
||||
res.ok,
|
||||
"htmlLen=",
|
||||
html.length
|
||||
);
|
||||
console.log("[HiAnime-TV] findEpisodes() status=", res.status, "ok=", res.ok, "htmlLen=", html.length);
|
||||
|
||||
var episodes = [];
|
||||
var regex =
|
||||
@@ -227,24 +223,15 @@ HiAnime.prototype.findEpisodes = function (animeId) {
|
||||
});
|
||||
}
|
||||
|
||||
console.log(
|
||||
"[HiAnime-TV] findEpisodes() returning=",
|
||||
episodes.length,
|
||||
"firstEp=",
|
||||
episodes[0] ? episodes[0].number : "none"
|
||||
);
|
||||
console.log("[HiAnime-TV] findEpisodes() returning=", episodes.length, "firstEp=", episodes[0] ? episodes[0].number : "none");
|
||||
return episodes;
|
||||
} catch (e) {
|
||||
console.error(
|
||||
"[HiAnime-TV] findEpisodes() ERROR",
|
||||
String(e),
|
||||
e && e.stack ? e.stack : ""
|
||||
);
|
||||
console.error("[HiAnime-TV] findEpisodes() ERROR", String(e), e && e.stack ? e.stack : "");
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
HiAnime.prototype.findEpisodeServer = function (episode, _server) {
|
||||
findEpisodeServer(episode, _server) {
|
||||
console.log("[HiAnime-TV] findEpisodeServer() episode=", episode, "server=", _server);
|
||||
try {
|
||||
var ep = episode;
|
||||
@@ -281,14 +268,7 @@ HiAnime.prototype.findEpisodeServer = function (episode, _server) {
|
||||
if (!episodeId) throw new Error("Missing episode id");
|
||||
|
||||
var serverName = (_server && _server !== "default") ? String(_server) : "HD-1";
|
||||
console.log(
|
||||
"[HiAnime-TV] parsed episodeId=",
|
||||
episodeId,
|
||||
"subOrDub=",
|
||||
subOrDub,
|
||||
"serverName=",
|
||||
serverName
|
||||
);
|
||||
console.log("[HiAnime-TV] parsed episodeId=", episodeId, "subOrDub=", subOrDub, "serverName=", serverName);
|
||||
|
||||
if (serverName === "HD-1" || serverName === "HD-2" || serverName === "HD-3") {
|
||||
var serversUrl = this.baseUrl + "/ajax/v2/episode/servers?episodeId=" + encodeURIComponent(episodeId);
|
||||
@@ -325,10 +305,7 @@ HiAnime.prototype.findEpisodeServer = function (episode, _server) {
|
||||
try {
|
||||
decryptData = this.extractMegaCloud(sourcesJson.link, true);
|
||||
if (decryptData && decryptData.headersProvided) requiredHeaders = decryptData.headersProvided;
|
||||
console.log(
|
||||
"[HiAnime-TV] extractMegaCloud ok sources=",
|
||||
(decryptData && decryptData.sources ? decryptData.sources.length : 0)
|
||||
);
|
||||
console.log("[HiAnime-TV] extractMegaCloud ok sources=", (decryptData && decryptData.sources ? decryptData.sources.length : 0));
|
||||
} catch (err) {
|
||||
console.warn("[HiAnime-TV] Primary decrypter failed:", String(err));
|
||||
}
|
||||
@@ -351,10 +328,7 @@ HiAnime.prototype.findEpisodeServer = function (episode, _server) {
|
||||
"X-Requested-With": "XMLHttpRequest"
|
||||
};
|
||||
|
||||
console.log(
|
||||
"[HiAnime-TV] fallback decrypt sources=",
|
||||
(decryptData && decryptData.sources ? decryptData.sources.length : 0)
|
||||
);
|
||||
console.log("[HiAnime-TV] fallback decrypt sources=", (decryptData && decryptData.sources ? decryptData.sources.length : 0));
|
||||
}
|
||||
|
||||
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++) {
|
||||
var s = sourcesArr[i];
|
||||
if (s && s.type === "hls" && s.file) {
|
||||
streamSource = s;
|
||||
break;
|
||||
if (s && s.type === "hls" && s.file) { streamSource = s; break; }
|
||||
}
|
||||
}
|
||||
|
||||
if (!streamSource) {
|
||||
for (var j = 0; j < sourcesArr.length; j++) {
|
||||
var s2 = sourcesArr[j];
|
||||
if (s2 && s2.type === "mp4" && s2.file) {
|
||||
streamSource = s2;
|
||||
break;
|
||||
}
|
||||
if (s2 && s2.type === "mp4" && s2.file) { streamSource = s2; break; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -394,14 +361,7 @@ HiAnime.prototype.findEpisodeServer = function (episode, _server) {
|
||||
}
|
||||
}
|
||||
|
||||
console.log(
|
||||
"[HiAnime-TV] FINAL stream file=",
|
||||
streamSource.file,
|
||||
"type=",
|
||||
streamSource.type,
|
||||
"subs=",
|
||||
subtitles.length
|
||||
);
|
||||
console.log("[HiAnime-TV] FINAL stream file=", streamSource.file, "type=", streamSource.type, "subs=", subtitles.length);
|
||||
|
||||
return {
|
||||
server: serverName,
|
||||
@@ -423,16 +383,12 @@ HiAnime.prototype.findEpisodeServer = function (episode, _server) {
|
||||
console.warn("[HiAnime-TV] Unsupported server=", serverName);
|
||||
return null;
|
||||
} catch (e) {
|
||||
console.error(
|
||||
"[HiAnime-TV] findEpisodeServer() ERROR",
|
||||
String(e),
|
||||
e && e.stack ? e.stack : ""
|
||||
);
|
||||
console.error("[HiAnime-TV] findEpisodeServer() ERROR", String(e), e && e.stack ? e.stack : "");
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
HiAnime.prototype.extractMegaCloud = function (embedUrl, returnHeaders) {
|
||||
extractMegaCloud(embedUrl, returnHeaders) {
|
||||
console.log("[HiAnime-TV] extractMegaCloud() embedUrl=", embedUrl, "returnHeaders=", returnHeaders);
|
||||
|
||||
var url = new URL(embedUrl);
|
||||
@@ -450,14 +406,7 @@ HiAnime.prototype.extractMegaCloud = function (embedUrl, returnHeaders) {
|
||||
var r = this._fetch(embedUrl, { headers: headers });
|
||||
var html = r.text();
|
||||
|
||||
console.log(
|
||||
"[HiAnime-TV] extractMegaCloud() embed status=",
|
||||
r.status,
|
||||
"ok=",
|
||||
r.ok,
|
||||
"htmlLen=",
|
||||
(html || "").length
|
||||
);
|
||||
console.log("[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);
|
||||
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 matches = [];
|
||||
var m;
|
||||
while ((m = regex16.exec(html)) !== null) {
|
||||
matches.push(m[1]);
|
||||
}
|
||||
if (matches.length >= 3) {
|
||||
nonce = matches[0] + matches[1] + matches[2];
|
||||
}
|
||||
while ((m = regex16.exec(html)) !== null) matches.push(m[1]);
|
||||
if (matches.length >= 3) nonce = matches[0] + matches[1] + matches[2];
|
||||
}
|
||||
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 sourcesJson = sr.json();
|
||||
|
||||
console.log(
|
||||
"[HiAnime-TV] extractMegaCloud() sources status=",
|
||||
sr.status,
|
||||
"ok=",
|
||||
sr.ok,
|
||||
"sources=",
|
||||
(sourcesJson.sources ? sourcesJson.sources.length : 0),
|
||||
"tracks=",
|
||||
(sourcesJson.tracks ? sourcesJson.tracks.length : 0)
|
||||
console.log("[HiAnime-TV] extractMegaCloud() sources status=", sr.status, "ok=", sr.ok,
|
||||
"sources=", (sourcesJson.sources ? sourcesJson.sources.length : 0),
|
||||
"tracks=", (sourcesJson.tracks ? sourcesJson.tracks.length : 0)
|
||||
);
|
||||
|
||||
return {
|
||||
@@ -505,7 +444,8 @@ HiAnime.prototype.extractMegaCloud = function (embedUrl, returnHeaders) {
|
||||
server: sourcesJson.server || null,
|
||||
headersProvided: returnHeaders ? headers : undefined
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HiAnime;
|
||||
console.log("[HiAnime-TV] module.exports set");
|
||||
|
||||
Reference in New Issue
Block a user