mirror of
https://github.com/Shineii86/AniKotoAPI.git
synced 2026-10-22 10:55:10 +00:00
- Fix extractServerList JSON parsing: fetchWithMirror returns raw text but code treated it as parsed object, causing Cheerio to parse JSON string as HTML resulting in empty server arrays - Fix extractMapperServers JSON parsing: same raw text issue caused data processing loop to be skipped, returning empty arrays - Fix streamInfo controller to return 404 with error message when stream URL is null instead of silently returning null values - Fix server list controller to use proper TTL and handle empty results - Fix JSON.parse error handling in episodeList and episodeListAjax extractors - Update CHANGELOG.md with v2.2.1 bug fixes
155 lines
6.4 KiB
JavaScript
155 lines
6.4 KiB
JavaScript
/*
|
|
* ======= • ======= • ======= • ======= • =======• =======
|
|
* AniKotoAPI — streamInfo.controller.js
|
|
* Repository: https://github.com/Shineii86/AniKotoAPI
|
|
*
|
|
* @description
|
|
* Controller for streaming information. Provides stream URLs,
|
|
* available servers, and mapped server data for episode playback.
|
|
* Three endpoints for different streaming data needs.
|
|
*
|
|
* @exports
|
|
* getStreamInfo - Express route handler for GET /api/stream
|
|
* getServerList - Express route handler for GET /api/servers
|
|
* getMapperServers - Express route handler for GET /api/mapper-servers
|
|
*
|
|
* @author Shinei Nouzen
|
|
* @license MIT
|
|
* ======= • ======= • ======= • ======= • =======• =======
|
|
*/
|
|
|
|
import { extractStreamInfo, extractServerList, extractMapperServers } from "../extractors/streamInfo.extractor.js";
|
|
import { getCache, setCache, TTL } from "../helper/cache.helper.js";
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// CONTROLLER: STREAM INFO
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
// ---- FEATURE: Stream URL and metadata endpoint ----
|
|
/**
|
|
* Handles GET /api/stream requests. Returns stream URL and
|
|
* metadata for a specific episode link ID.
|
|
*
|
|
* @param {object} req - Express request object
|
|
* @param {string} req.query.id - Episode link ID
|
|
* @param {object} res - Express response object
|
|
* @param {Function} next - Express next middleware function
|
|
* @returns {void} Sends JSON response with stream information
|
|
*
|
|
* @example
|
|
* GET /api/stream?id=abc123
|
|
* Response: { success: true, results: { url, quality, ... } }
|
|
*
|
|
* @throws {400} If 'id' query parameter is missing
|
|
*/
|
|
const getStreamInfo = async (req, res, next) => {
|
|
try {
|
|
const { id } = req.query;
|
|
if (!id) {
|
|
return res.status(400).json({ success: false, message: "Link ID is required" });
|
|
}
|
|
const cacheKey = `stream_${id}`;
|
|
const cached = getCache(cacheKey);
|
|
if (cached) {
|
|
return res.json({ success: true, results: cached });
|
|
}
|
|
const data = await extractStreamInfo(id);
|
|
if (!data.url) {
|
|
return res.status(404).json({ success: false, message: "Stream URL not found for this link ID", results: data });
|
|
}
|
|
setCache(cacheKey, data, TTL.stream);
|
|
res.json({ success: true, results: data });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
};
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// CONTROLLER: SERVER LIST
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
// ---- FEATURE: Available streaming servers endpoint ----
|
|
/**
|
|
* Handles GET /api/servers requests. Returns list of available
|
|
* streaming servers for the given episode IDs.
|
|
*
|
|
* @param {object} req - Express request object
|
|
* @param {string} req.query.ids - Comma-separated episode IDs
|
|
* @param {object} res - Express response object
|
|
* @param {Function} next - Express next middleware function
|
|
* @returns {void} Sends JSON response with server list
|
|
*
|
|
* @example
|
|
* GET /api/servers?ids=123,456,789
|
|
* Response: { success: true, results: [{ server, name, ... }] }
|
|
*
|
|
* @throws {400} If 'ids' query parameter is missing
|
|
*/
|
|
const getServerList = async (req, res, next) => {
|
|
try {
|
|
const { ids } = req.query;
|
|
if (!ids) {
|
|
return res.status(400).json({ success: false, message: "Episode IDs are required" });
|
|
}
|
|
const cacheKey = `servers_${ids}`;
|
|
const cached = getCache(cacheKey);
|
|
if (cached) {
|
|
return res.json({ success: true, results: cached });
|
|
}
|
|
const data = await extractServerList(ids);
|
|
if (!data || data.length === 0) {
|
|
return res.status(404).json({ success: false, message: "No servers found for the given episode IDs", results: [] });
|
|
}
|
|
setCache(cacheKey, data, TTL.servers);
|
|
res.json({ success: true, results: data });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
};
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// CONTROLLER: MAPPER SERVERS
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
// ---- FEATURE: Cross-episode mapped servers endpoint ----
|
|
/**
|
|
* Handles GET /api/mapper-servers requests. Returns servers mapped
|
|
* across multiple episodes for continuous playback.
|
|
*
|
|
* @param {object} req - Express request object
|
|
* @param {string} req.query.malId - MyAnimeList ID
|
|
* @param {string} req.query.slug - Anime slug
|
|
* @param {string} req.query.timestamp - Timestamp for mapping
|
|
* @param {object} res - Express response object
|
|
* @param {Function} next - Express next middleware function
|
|
* @returns {void} Sends JSON response with mapped server data
|
|
*
|
|
* @example
|
|
* GET /api/mapper-servers?malId=21&slug=one-piece×tamp=1234567890
|
|
* Response: { success: true, results: [...] }
|
|
*
|
|
* @throws {400} If any required parameter is missing
|
|
* NOTE: All three parameters (malId, slug, timestamp) are required
|
|
*/
|
|
const getMapperServers = async (req, res, next) => {
|
|
try {
|
|
const { malId, slug, timestamp } = req.query;
|
|
if (!malId || !slug || !timestamp) {
|
|
return res.status(400).json({ success: false, message: "malId, slug, and timestamp are required" });
|
|
}
|
|
const cacheKey = `mapper_${malId}_${slug}_${timestamp}`;
|
|
const cached = getCache(cacheKey);
|
|
if (cached) {
|
|
return res.json({ success: true, results: cached });
|
|
}
|
|
const data = await extractMapperServers(malId, slug, timestamp);
|
|
setCache(cacheKey, data, TTL.default);
|
|
res.json({ success: true, results: data });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
};
|
|
|
|
export { getStreamInfo, getServerList, getMapperServers };
|
|
|
|
// ══════════════════════════════════════════════════════════════ END: streamInfo.controller.js
|