Update anime/hianime/source.js

This commit is contained in:
2025-12-21 14:25:26 +01:00
parent adc622ee22
commit 5a3d228505

View File

@@ -1,7 +1,7 @@
class HiAnime { class HiAnime {
constructor() { constructor() {
this.type = "anime-streaming"; this.type = "anime-board";
this.version = "1.0.0"; this.version = "1.0"
this.baseUrl = "https://hianime.to"; this.baseUrl = "https://hianime.to";
} }
@@ -13,16 +13,15 @@ class HiAnime {
} }
async search(query) { async search(query) {
const normalize = (str) => const normalize = (str) => this.safeString(str).toLowerCase().replace(/[^a-z0-9]+/g, "");
this.safeString(str).toLowerCase().replace(/[^a-z0-9]+/g, "");
const start = query.media.startDate; const start = query.media.startDate;
const fetchMatches = async (url) => { const fetchMatches = async (url) => {
const html = await fetch(url).then((res) => res.text()); const html = await fetch(url).then(res => res.text());
const regex = /<a href="\/watch\/([^"]+)"[^>]+title="([^"]+)"[^>]+data-id="(\d+)"/g; const regex = /<a href="\/watch\/([^"]+)"[^>]+title="([^"]+)"[^>]+data-id="(\d+)"/g;
return [...html.matchAll(regex)].map((m) => { return [...html.matchAll(regex)].map(m => {
const id = m[3]; const id = m[3];
const pageUrl = m[1]; const pageUrl = m[1];
const title = m[2]; const title = m[2];
@@ -47,19 +46,17 @@ class HiAnime {
title, title,
image, image,
normTitleJP: normalize(this.normalizeSeasonParts(jname)), normTitleJP: normalize(this.normalizeSeasonParts(jname)),
normTitle: normalize(this.normalizeSeasonParts(title)) normTitle: normalize(this.normalizeSeasonParts(title)),
}; };
}); });
}; };
const url = let url = `${this.baseUrl}/search?keyword=${encodeURIComponent(query.query)}&sy=${start.year}&sm=${start.month}&sort=default`;
`${this.baseUrl}/search?keyword=${encodeURIComponent(query.query)}` + let matches = await fetchMatches(url);
`&sy=${start.year}&sm=${start.month}&sort=default`;
const matches = await fetchMatches(url);
if (matches.length === 0) return []; if (matches.length === 0) return [];
return matches.map((m) => ({ return matches.map(m => ({
id: `${m.id}/${query.dub ? "dub" : "sub"}`, id: `${m.id}/${query.dub ? "dub" : "sub"}`,
title: m.title, title: m.title,
image: m.image, image: m.image,
@@ -75,10 +72,11 @@ class HiAnime {
}); });
const json = await res.json(); const json = await res.json();
const html = json.html; const html = json.html;
console.log(html)
const episodes = []; const episodes = [];
const regex = const regex = /<a[^>]*class="[^"]*\bep-item\b[^"]*"[^>]*data-number="(\d+)"[^>]*data-id="(\d+)"[^>]*href="([^"]+)"[\s\S]*?<div class="ep-name[^"]*"[^>]*title="([^"]+)"/g;
/<a[^>]*class="[^"]*\bep-item\b[^"]*"[^>]*data-number="(\d+)"[^>]*data-id="(\d+)"[^>]*href="([^"]+)"[\s\S]*?<div class="ep-name[^"]*"[^>]*title="([^"]+)"/g;
let match; let match;
while ((match = regex.exec(html)) !== null) { while ((match = regex.exec(html)) !== null) {
@@ -86,7 +84,7 @@ class HiAnime {
id: `${match[2]}/${subOrDub}`, id: `${match[2]}/${subOrDub}`,
number: parseInt(match[1], 10), number: parseInt(match[1], 10),
url: this.baseUrl + match[3], url: this.baseUrl + match[3],
title: match[4] title: match[4],
}); });
} }
@@ -95,13 +93,12 @@ class HiAnime {
async findEpisodeServer(episode, _server) { async findEpisodeServer(episode, _server) {
const [id, subOrDub] = episode.id.split("/"); const [id, subOrDub] = episode.id.split("/");
const serverName = _server !== "default" ? _server : "HD-1"; let serverName = _server !== "default" ? _server : "HD-1";
if (_server === "HD-1" || _server === "HD-2" || _server === "HD-3" || _server === "default") { if (_server === "HD-1" || _server === "HD-2" || _server === "HD-3") {
const serverJson = await fetch( const serverJson = await fetch(`${this.baseUrl}/ajax/v2/episode/servers?episodeId=${id}`, {
`${this.baseUrl}/ajax/v2/episode/servers?episodeId=${id}`, headers: { "X-Requested-With": "XMLHttpRequest" }
{ headers: { "X-Requested-With": "XMLHttpRequest" } } }).then(res => res.json());
).then((res) => res.json());
const serverHtml = serverJson.html; const serverHtml = serverJson.html;
const regex = new RegExp( const regex = new RegExp(
@@ -109,46 +106,45 @@ class HiAnime {
"i" "i"
); );
const m = regex.exec(serverHtml); const match = regex.exec(serverHtml);
if (!m) throw new Error(`Server "${serverName}" (${subOrDub}) not found`); if (!match) throw new Error(`Server "${serverName}" (${subOrDub}) not found`);
const serverId = m[1]; const serverId = match[1];
const sourcesJson = await fetch( const sourcesJson = await fetch(`${this.baseUrl}/ajax/v2/episode/sources?id=${serverId}`, {
`${this.baseUrl}/ajax/v2/episode/sources?id=${serverId}`, headers: { "X-Requested-With": "XMLHttpRequest" }
{ headers: { "X-Requested-With": "XMLHttpRequest" } } }).then(res => res.json());
).then((res) => res.json());
let decryptData = null; let decryptData = null;
let requiredHeaders = {}; let requiredHeaders = {};
try { try {
decryptData = await this.extractMegaCloud(sourcesJson.link, true); decryptData = await this.extractMegaCloud(sourcesJson.link, true);
if (decryptData && decryptData.headersProvided) requiredHeaders = decryptData.headersProvided; if (decryptData && decryptData.headersProvided) {
requiredHeaders = decryptData.headersProvided;
}
} catch (err) { } catch (err) {
// ignore console.warn("Primary decrypter failed:", err);
} }
if (!decryptData) { if (!decryptData) {
console.warn("Primary decrypter failed — trying ShadeOfChaos fallback...");
const fallbackRes = await fetch( const fallbackRes = await fetch(
`https://ac-api.ofchaos.com/api/anime/embed/convert/v2?embedUrl=${encodeURIComponent( `https://ac-api.ofchaos.com/api/anime/embed/convert/v2?embedUrl=${encodeURIComponent(sourcesJson.link)}`
sourcesJson.link
)}`
); );
decryptData = await fallbackRes.json(); decryptData = await fallbackRes.json();
requiredHeaders = { requiredHeaders = {
"Referer": "https://megacloud.club/", "Referer": "https://megacloud.club/",
"Origin": "https://megacloud.club", "Origin": "https://megacloud.club",
"User-Agent": "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",
"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" "X-Requested-With": "XMLHttpRequest"
}; };
} }
const streamSource = const streamSource =
(decryptData.sources || []).find((s) => s.type === "hls") || decryptData.sources.find((s) => s.type === "hls") ||
(decryptData.sources || []).find((s) => s.type === "mp4"); decryptData.sources.find((s) => s.type === "mp4");
if (!streamSource?.file) throw new Error("No valid stream file found"); if (!streamSource?.file) throw new Error("No valid stream file found");
@@ -158,38 +154,32 @@ class HiAnime {
id: `sub-${index}`, id: `sub-${index}`,
language: track.label || "Unknown", language: track.label || "Unknown",
url: track.file, url: track.file,
isDefault: !!track.default isDefault: !!track.default,
})); }));
return { return {
server: serverName, server: serverName,
headers: requiredHeaders, headers: requiredHeaders,
videoSources: [ videoSources: [{
{
url: streamSource.file, url: streamSource.file,
type: streamSource.type === "hls" ? "m3u8" : "mp4", type: streamSource.type === "hls" ? "m3u8" : "mp4",
quality: "auto", quality: "auto",
subtitles subtitles
} }]
]
}; };
} }
else if (_server === "HD-4") {
if (_server === "HD-4") {
return null; return null;
} }
return null;
} }
safeString(str) { safeString(str) {
return typeof str === "string" ? str : ""; return (typeof str === "string" ? str : "");
} }
normalizeSeasonParts(title) { normalizeSeasonParts(title) {
const s = this.safeString(title); const s = this.safeString(title);
return s return s.toLowerCase()
.toLowerCase()
.replace(/[^a-z0-9]+/g, "") .replace(/[^a-z0-9]+/g, "")
.replace(/\d+(st|nd|rd|th)/g, (m) => m.replace(/st|nd|rd|th/, "")) .replace(/\d+(st|nd|rd|th)/g, (m) => m.replace(/st|nd|rd|th/, ""))
.replace(/season|cour|part/g, ""); .replace(/season|cour|part/g, "");
@@ -204,12 +194,10 @@ class HiAnime {
"X-Requested-With": "XMLHttpRequest", "X-Requested-With": "XMLHttpRequest",
"Referer": baseDomain, "Referer": baseDomain,
"Origin": `${url.protocol}//${url.host}`, "Origin": `${url.protocol}//${url.host}`,
"User-Agent": "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",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36"
}; };
const html = await fetch(embedUrl, { headers }).then((r) => r.text()); const html = await fetch(embedUrl, { headers }).then((r) => r.text());
const fileIdMatch = html.match(/<title>\s*File\s+#([a-zA-Z0-9]+)\s*-/i); const 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");
const fileId = fileIdMatch[1]; const fileId = fileIdMatch[1];