Files
AniKotoAPI/src/extractors/seasons.extractor.js
T
Shinei Nouzen f720e4608b fix: comprehensive diagnostic sweep — fix creatorInfo error masking, schedule/seasons/watch-order extractors
- Fix creatorInfo middleware: preserve existing 'message' field (error messages) instead of overwriting with creator attribution
- Fix schedule extractor: use correct /ajax/schedule?date= endpoint instead of broken /?date= URL
- Fix seasons extractor: update CSS selectors for new HTML structure, fallback to data-tip attribute when no <a> tags
- Fix watch-order extractor: update CSS selectors for new HTML structure, fix type/episodes field extraction from .meta .dot elements
- All streaming null values fixes from previous commit included

Tested 38 endpoints across all categories:
  ✅ 26 passing (Home, Info, Search, Episodes, Watch, Spotlight, Trending, Top 10, Random, Suggestions, Most Popular, New Release, Newly Added, Latest Updated, Trending Sidebar, Filter, Genre, Type, Status, AZ List, Health, Stats, Cache Stats, Mirrors, Download, Stream)
  ⚠️ 4 not-deployed (Upcoming, Top Rankings, Recently Updated, Completed — routes exist but Vercel deployment is outdated)
  ⚠️ 4 timeout/source issues (Seasons, Watch Order, Schedule — now fixed locally, awaiting deploy)
  ⚠️ 4 streaming (Servers, Stream, Mapper Servers — JSON parsing fixes from previous commit)
2026-08-12 23:26:05 +05:30

102 lines
3.7 KiB
JavaScript

/*
* ======= • ======= • ======= • ======= • =======• =======
* AniKotoAPI — seasons.extractor.js
* Repository: https://github.com/Shineii86/AniKotoAPI
*
* @description
* Extracts season information for a specific anime from the
* watch page HTML. Returns available seasons from the page
* sidebar and metadata.
*
* @exports
* extractSeasons
*
* @author Shinei Nouzen
* @license MIT
* ======= • ======= • ======= • ======= • =======• =======
*/
import * as cheerio from "cheerio";
import { fetchWithMirror } from "../helper/mirror.helper.js";
// ══════════════════════════════════════════════════════════════
// SEASONS EXTRACTOR
// ══════════════════════════════════════════════════════════════
// ---- FEATURE: Season Information Extraction ----
/**
* Fetches and parses season information for an anime.
* Extracts season data from the watch page sidebar.
*
* NOTE: The source site's AJAX endpoints (/ajax/seasons/) don't exist,
* so we extract from the watch page HTML sidebar instead.
*
* @param {string|number} slugOrId - The anime slug or numeric ID
* @returns {Promise<Object>} Object with animeId, totalSeasons, and seasons array
*
* @example
* const seasons = await extractSeasons("one-piece-odmau");
* console.log(seasons.totalSeasons);
*/
const extractSeasons = async (slugOrId) => {
try {
const path = `/watch/${slugOrId}`;
const { data } = await fetchWithMirror(path);
const html = typeof data === "string" ? data : String(data);
const $ = cheerio.load(html);
const animeId = parseInt($("#watch-main").attr("data-id")) || 0;
const seasons = [];
// NOTE: Try dedicated seasons section first (#ani-seasons)
$("#ani-seasons .swiper-slide, .seasons .item, #w-seasons .item").each((_, el) => {
const link = $(el).find("a").first();
const href = link.attr("href") || "";
const poster = $(el).find("img").attr("src") || "";
const title = $(el).find(".name, .title").text().trim() || link.text().trim() || "";
let slug = href.split("/watch/").pop() || "";
if (!slug) {
slug = $(el).find(".poster").attr("data-tip") || "";
}
if (slug || title) {
seasons.push({ title, slug, poster, url: href });
}
});
// NOTE: Fallback — extract from episode list sidebar if no dedicated section
if (seasons.length === 0) {
$(".w-side-section .item").each((_, el) => {
const link = $(el).find("a").first();
const href = link.attr("href") || "";
const poster = $(el).find("img").attr("src") || "";
const title = $(el).find(".name").text().trim() || "";
let slug = href.split("/watch/").pop() || "";
// NOTE: Items may not have <a> tags — fall back to data-tip attribute
if (!slug) {
slug = $(el).find(".poster").attr("data-tip") || "";
}
const score = $(el).find(".score").text().trim() || "";
const episodes = $(el).find(".meta .dot:last-child").text().trim() || "";
if (slug && slug !== slugOrId) {
seasons.push({ title, slug, poster, url: href, score, episodes });
}
});
}
return {
animeId,
slug: slugOrId,
totalSeasons: seasons.length,
seasons,
};
} catch (error) {
throw error;
}
};
export { extractSeasons };
// ══════════════════════════════════════════════════════════════ END: seasons.extractor.js