feat: add anilistID in response
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
import axios from "axios";
|
||||
import createHttpError from "http-errors";
|
||||
import { type RequestHandler } from "express";
|
||||
import { type CheerioAPI, load } from "cheerio";
|
||||
import { scrapeAnimeEpisodeSources } from "../parsers/index.js";
|
||||
import { USER_AGENT_HEADER, SRC_BASE_URL } from "../utils/constants.js";
|
||||
import { type AnimeServers, Servers } from "../models/anime.js";
|
||||
import { type AnimeEpisodeSrcsQueryParams } from "../models/controllers/index.js";
|
||||
|
||||
type AnilistID = number | null;
|
||||
|
||||
// /anime/episode-srcs?id=${episodeId}?server=${server}&category=${category (dub or sub)}
|
||||
const getAnimeEpisodeSources: RequestHandler<
|
||||
unknown,
|
||||
Awaited<ReturnType<typeof scrapeAnimeEpisodeSources>>,
|
||||
Awaited<ReturnType<typeof scrapeAnimeEpisodeSources & AnilistID>>,
|
||||
unknown,
|
||||
AnimeEpisodeSrcsQueryParams
|
||||
> = async (req, res, next) => {
|
||||
@@ -28,9 +33,34 @@ const getAnimeEpisodeSources: RequestHandler<
|
||||
throw createHttpError.BadRequest("Anime episode id required");
|
||||
}
|
||||
|
||||
const data = await scrapeAnimeEpisodeSources(episodeId, server, category);
|
||||
let anilistID: AnilistID;
|
||||
const animeURL = new URL(episodeId?.split("?ep=")[0], SRC_BASE_URL)?.href;
|
||||
|
||||
res.status(200).json(data);
|
||||
const [episodeSrcData, animeSrc] = await Promise.all([
|
||||
scrapeAnimeEpisodeSources(episodeId, server, category),
|
||||
axios.get(animeURL, {
|
||||
headers: {
|
||||
Referer: SRC_BASE_URL,
|
||||
"User-Agent": USER_AGENT_HEADER,
|
||||
"X-Requested-With": "XMLHttpRequest",
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
const $: CheerioAPI = load(animeSrc?.data);
|
||||
|
||||
try {
|
||||
anilistID = Number(
|
||||
JSON.parse($("body")?.find("#syncData")?.text())?.anilist_id
|
||||
);
|
||||
} catch (err) {
|
||||
anilistID = null;
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
...episodeSrcData,
|
||||
anilistID,
|
||||
});
|
||||
} catch (err: any) {
|
||||
console.error(err);
|
||||
next(err);
|
||||
|
||||
Reference in New Issue
Block a user