feat: upgrade to v2.0.0 with multi-mirror fallback, new endpoints, and LRU cache

- Add multi-mirror fallback system with 5 mirror domains
- Add LRU cache with configurable TTL per endpoint
- Add response compression (gzip)
- Add Seasons endpoint (/api/seasons/:id)
- Add Watch Order endpoint (/api/watch-order/:id)
- Add Latest Updated endpoint (/api/latest-updated)
- Add Download endpoint (/api/download)
- Add cache stats endpoint (/api/cache/stats)
- Add mirror status/reset endpoints (/api/mirrors, /api/mirrors/reset)
- Add configurable rate limiting with X-RateLimit headers
- Add request timeouts and entity too large handling
- Update documentation with all new endpoints
- Update CHANGELOG.md with v2.0.0 entry
- Update package.json version to 2.0.0
- Add 6 AI agent prompts for development
This commit is contained in:
Shinei Nouzen
2026-07-04 15:07:07 +05:30
parent 30feda703a
commit fe51275db0
51 changed files with 5151 additions and 342 deletions
+187
View File
@@ -0,0 +1,187 @@
/*
* ======= • ======= • ======= • ======= • =======• =======
* AniKotoAPI — download.extractor.js
* Repository: https://github.com/Shineii86/AniKotoAPI
*
* @description
* Extracts download links for anime episodes from anikototv.to.
* Decodes base64 download-data attributes and follows redirects
* to get streaming server URLs.
*
* Download flow:
* 1. Watch page → base64 decode → nekostream.site URL
* 2. nekostream.site → Cloudflare Worker → kwik.cx URL
* 3. kwik.cx → actual download URL (requires browser)
*
* @exports
* extractDownloadLinks
*
* @author Shinei Nouzen
* @license MIT
* ======= • ======= • ======= • ======= • =======• =======
*/
import * as cheerio from "cheerio";
import axios from "axios";
import { fetchWithMirror } from "../helper/mirror.helper.js";
import { headers } from "../configs/header.config.js";
/**
* Decodes base64 download data from the website
* @param {string} encoded - Base64 encoded string
* @returns {Object} Decoded download data
*/
function decodeDownloadData(encoded) {
try {
const decoded = Buffer.from(encoded, "base64").toString("utf-8");
return JSON.parse(decoded);
} catch (error) {
return null;
}
}
/**
* Follows redirect chain to get the final URL
* @param {string} url - Starting URL
* @returns {Promise<string>} Final URL after redirects
*/
async function followRedirects(url) {
try {
const response = await axios.get(url, {
headers,
maxRedirects: 5,
timeout: 10000
});
return response.request?.res?.responseUrl || response.config?.url || url;
} catch (error) {
// If we get a redirect error, try to extract the location
if (error.response?.headers?.location) {
return error.response.headers.location;
}
return url;
}
}
/**
* Fetches and parses download links for a specific episode.
* Scrapes the watch page to extract download-data attributes
* and follows the redirect chain to get streaming server URLs.
*
* Note: Download data may be loaded dynamically via JavaScript.
* If no download links are found in the initial HTML, try using
* a browser or headless browser to get the full page content.
*
* @param {string} slug - The anime slug (e.g., "kill-blue-gcqj5")
* @param {number} ep - Episode number
* @returns {Promise<Object>} Object with slug, episode, and downloads array
*
* @example
* const downloads = await extractDownloadLinks("kill-blue-gcqj5", 1);
* console.log(downloads.downloads[0].server); // server name
* console.log(downloads.downloads[0].kwikUrl); // kwik.cx URL
*/
const extractDownloadLinks = async (slug, ep) => {
try {
const { data } = await fetchWithMirror(`/watch/${slug}/ep-${ep}`);
// Handle both string HTML and JSON responses
let html = "";
if (typeof data === "string") {
try {
const json = JSON.parse(data);
html = json.result || data;
} catch (e) {
html = data;
}
} else if (data?.result) {
html = data.result;
} else if (data?.data) {
html = data.data;
} else {
html = String(data);
}
const $ = cheerio.load(html);
const downloads = [];
// Find all download buttons with download-data attribute
// Multiple selectors to handle different HTML structures
const selectors = [
"[download-data]",
"li.download-icon",
".download-icon",
"[onclick*='openDownloadModalDownload']",
"[onclick*='openDownloadModal']"
];
for (const selector of selectors) {
$(selector).each((_, el) => {
const encoded = $(el).attr("download-data") || "";
const serverType = $(el).closest(".type").attr("data-type") || "unknown";
const serverLabel = $(el).closest(".type").find("label").text().trim() || serverType;
if (encoded && encoded.length > 10) {
const decoded = decodeDownloadData(encoded);
if (decoded) {
// Extract URLs from the decoded data
for (const [serverGroup, servers] of Object.entries(decoded)) {
for (const [serverName, url] of Object.entries(servers)) {
// Avoid duplicates
const exists = downloads.some(d => d.url === url);
if (!exists) {
// Construct worker URL from nekostream.site URL
const workerUrl = url.includes("nekostream.site")
? url.replace("https://pahe.nekostream.site/", "https://proud-dew-d754.download992.workers.dev/")
: null;
downloads.push({
server: serverName,
serverGroup,
type: serverType,
typeLabel: serverLabel,
url, // nekostream.site URL
workerUrl,
kwikUrl: null, // Will be filled after redirect
note: "Visit kwikUrl in browser to get actual download link"
});
}
}
}
}
}
});
if (downloads.length > 0) break;
}
// Try to follow redirects for each download URL
for (const download of downloads) {
if (download.workerUrl) {
try {
const finalUrl = await followRedirects(download.workerUrl);
if (finalUrl.includes("kwik.cx")) {
download.kwikUrl = finalUrl;
}
} catch (error) {
// If redirect fails, construct a placeholder kwik URL
download.kwikUrl = "https://kwik.cx/f/[requires-browser]";
}
}
}
return {
slug,
episode: ep,
totalDownloads: downloads.length,
downloads,
note: downloads.length > 0
? "kwikUrl requires browser to bypass Cloudflare protection"
: "Download links may be loaded dynamically via JavaScript. Try using a browser."
};
} catch (error) {
throw error;
}
};
export { extractDownloadLinks };