diff --git a/src/extractors/episodeList.extractor.js b/src/extractors/episodeList.extractor.js index f0effae..60cb64f 100644 --- a/src/extractors/episodeList.extractor.js +++ b/src/extractors/episodeList.extractor.js @@ -1,41 +1,64 @@ import * as cheerio from "cheerio"; import axios from "axios"; import { headers } from "../configs/header.config.js"; -import { URLS } from "../configs/dataUrl.js"; +import { URLS, BASE_URL } from "../configs/dataUrl.js"; const extractEpisodeList = async (slug) => { try { - const url = URLS.watch(slug); - const { data } = await axios.get(url, { headers }); - const $ = cheerio.load(data); + const infoUrl = URLS.watch(slug); + const { data: infoData } = await axios.get(infoUrl, { headers }); + const $ = cheerio.load(infoData); const animeId = parseInt($("#watch-main").attr("data-id")) || 0; - const episodes = []; - $("#w-episodes a[data-num], #w-episodes a[data-slug]").each((i, el) => { - const epId = $(el).attr("data-id") || $(el).attr("data-ep-id") || ""; - const epNum = parseInt($(el).attr("data-num")) || i + 1; - const epSlug = $(el).attr("data-slug") || ""; - const epTitle = $(el).find(".ep-title, .ep-name").text().trim() || ""; - const isActive = $(el).hasClass("active") || false; - const href = $(el).attr("href") || ""; + if (!animeId) { + return { animeId: 0, slug, totalEpisodes: 0, episodes: [] }; + } - episodes.push({ - id: epId, - episode_no: epNum, - slug: epSlug, - title: epTitle, - active: isActive, - href + try { + const ajaxUrl = `${BASE_URL}/ajax/episode/list/${animeId}`; + const { data: ajaxData } = await axios.get(ajaxUrl, { + headers: { + ...headers, + "X-Requested-With": "XMLHttpRequest" + } }); - }); - return { - animeId, - slug, - totalEpisodes: episodes.length, - episodes - }; + const $ep = cheerio.load(ajaxData); + + const episodes = []; + $ep("a[data-num], a[data-ep-id]").each((i, el) => { + const epId = $ep(el).attr("data-ep-id") || $ep(el).attr("data-id") || ""; + const epNum = parseInt($ep(el).attr("data-num")) || i + 1; + const epSlug = $ep(el).attr("data-slug") || ""; + const href = $ep(el).attr("href") || ""; + const epTitle = $ep(el).find(".ep-title, .ep-name").text().trim() || ""; + const isActive = $ep(el).hasClass("active") || false; + + episodes.push({ + id: epId, + episode_no: epNum, + slug: epSlug, + title: epTitle, + active: isActive, + href + }); + }); + + return { + animeId, + slug, + totalEpisodes: episodes.length, + episodes + }; + } catch (ajaxError) { + return { + animeId, + slug, + totalEpisodes: 0, + episodes: [] + }; + } } catch (error) { throw error; } diff --git a/src/extractors/filter.extractor.js b/src/extractors/filter.extractor.js index e94d149..e5ec49c 100644 --- a/src/extractors/filter.extractor.js +++ b/src/extractors/filter.extractor.js @@ -9,7 +9,11 @@ const extractFilter = async (params) => { try { const queryParams = new URLSearchParams(); - if (params.keyword) queryParams.set("keyword", params.keyword); + if (params.keyword) { + queryParams.set("keyword", params.keyword); + } else { + queryParams.set("keyword", ""); + } if (params.genre) { const genres = params.genre.split(",").map(g => GENRE_IDS[g.trim().toLowerCase()] || g.trim());