mirror of
https://github.com/JustAnimeCore/HiAnime-Api.git
synced 2026-04-17 22:01:44 +00:00
feature, not a bug
This commit is contained in:
20
src/controllers/actors.controller.js
Normal file
20
src/controllers/actors.controller.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import extractVoiceActor from "../extractors/actors.extractor.js";
|
||||
|
||||
const getVoiceActor = async (req, res) => {
|
||||
const id = req.params.id;
|
||||
try {
|
||||
const voiceActorData = await extractVoiceActor(id);
|
||||
|
||||
// Ensure the data is structured correctly
|
||||
if (!voiceActorData || voiceActorData.results.data.length === 0) {
|
||||
return res.status(404).json({ error: "No voice actor found." });
|
||||
}
|
||||
|
||||
return res.json(voiceActorData); // Return the desired structure
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return res.status(500).json({ error: "An error occurred" });
|
||||
}
|
||||
};
|
||||
|
||||
export default getVoiceActor;
|
||||
27
src/controllers/animeInfo.controller.js
Normal file
27
src/controllers/animeInfo.controller.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import extractAnimeInfo from "../extractors/animeInfo.extractor.js";
|
||||
import extractSeasons from "../extractors/seasons.extractor.js";
|
||||
import { getCachedData, setCachedData } from "../helper/cache.helper.js";
|
||||
|
||||
export const getAnimeInfo = async (req, res) => {
|
||||
const { id } = req.query;
|
||||
// const cacheKey = `animeInfo_${id}`;
|
||||
|
||||
try {
|
||||
// const cachedResponse = await getCachedData(cacheKey);
|
||||
// if (cachedResponse && Object.keys(cachedResponse).length > 0) {
|
||||
// return cachedResponse;
|
||||
// }
|
||||
const [seasons, data] = await Promise.all([
|
||||
extractSeasons(id),
|
||||
extractAnimeInfo(id),
|
||||
]);
|
||||
const responseData = { data: data, seasons: seasons };
|
||||
// setCachedData(cacheKey, responseData).catch((err) => {
|
||||
// console.error("Failed to set cache:", err);
|
||||
// });
|
||||
return responseData;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return res.status(500).json({ error: "An error occurred" });
|
||||
}
|
||||
};
|
||||
29
src/controllers/category.controller.js
Normal file
29
src/controllers/category.controller.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import { extractor } from "../extractors/category.extractor.js";
|
||||
import { getCachedData, setCachedData } from "../helper/cache.helper.js";
|
||||
|
||||
export const getCategory = async (req, res, routeType) => {
|
||||
if (routeType === "genre/martial-arts") {
|
||||
routeType = "genre/marial-arts";
|
||||
}
|
||||
const requestedPage = parseInt(req.query.page) || 1;
|
||||
// const cacheKey = `${routeType.replace(/\//g, "_")}_page_${requestedPage}`;
|
||||
try {
|
||||
// const cachedResponse = await getCachedData(cacheKey);
|
||||
// if (cachedResponse && Object.keys(cachedResponse).length > 0)
|
||||
// return cachedResponse;
|
||||
const { data, totalPages } = await extractor(routeType, requestedPage);
|
||||
if (requestedPage > totalPages) {
|
||||
const error = new Error("Requested page exceeds total available pages.");
|
||||
error.status = 404;
|
||||
throw error;
|
||||
}
|
||||
const responseData = { totalPages: totalPages, data: data };
|
||||
// setCachedData(cacheKey, responseData).catch((err) => {
|
||||
// console.error("Failed to set cache:", err);
|
||||
// });
|
||||
return responseData;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return e;
|
||||
}
|
||||
};
|
||||
20
src/controllers/characters.controller.js
Normal file
20
src/controllers/characters.controller.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import extractCharacter from "../extractors/characters.extractor.js";
|
||||
|
||||
const getCharacter = async (req, res) => {
|
||||
const id = req.params.id;
|
||||
try {
|
||||
const characterData = await extractCharacter(id);
|
||||
|
||||
// Ensure the data is structured correctly
|
||||
if (!characterData || characterData.results.data.length === 0) {
|
||||
return res.status(404).json({ error: "Character not found." });
|
||||
}
|
||||
|
||||
return res.json(characterData); // Return the desired structure
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return res.status(500).json({ error: "An error occurred" });
|
||||
}
|
||||
};
|
||||
|
||||
export default getCharacter;
|
||||
21
src/controllers/episodeList.controller.js
Normal file
21
src/controllers/episodeList.controller.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import extractEpisodesList from "../extractors/episodeList.extractor.js";
|
||||
import { getCachedData, setCachedData } from "../helper/cache.helper.js";
|
||||
|
||||
export const getEpisodes = async (req,res) => {
|
||||
const { id } = req.params;
|
||||
// const cacheKey = `episodes_${id}`;
|
||||
try {
|
||||
// const cachedResponse = await getCachedData(cacheKey);
|
||||
// if (cachedResponse && Object.keys(cachedResponse).length > 0) {
|
||||
// return cachedResponse;
|
||||
// }
|
||||
const data = await extractEpisodesList(encodeURIComponent(id));
|
||||
// setCachedData(cacheKey, data).catch((err) => {
|
||||
// console.error("Failed to set cache:", err);
|
||||
// });
|
||||
return data;
|
||||
} catch (e) {
|
||||
console.error("Error fetching episodes:", e);
|
||||
return e;
|
||||
}
|
||||
};
|
||||
66
src/controllers/filter.controller.js
Normal file
66
src/controllers/filter.controller.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import extractFilterResults from "../extractors/filter.extractor.js";
|
||||
|
||||
export const filter = async (req) => {
|
||||
try {
|
||||
// Extract all possible query parameters
|
||||
const {
|
||||
type,
|
||||
status,
|
||||
rated,
|
||||
score,
|
||||
season,
|
||||
language,
|
||||
genres,
|
||||
sort,
|
||||
sy, // Start year
|
||||
sm, // Start month
|
||||
sd, // Start day
|
||||
ey, // End year
|
||||
em, // End month
|
||||
ed, // End day
|
||||
keyword,
|
||||
page = 1
|
||||
} = req.query;
|
||||
|
||||
// Convert page to number
|
||||
const pageNum = parseInt(page);
|
||||
|
||||
// Create params object only with provided values
|
||||
const params = {};
|
||||
if (type) params.type = type;
|
||||
if (status) params.status = status;
|
||||
if (rated) params.rated = rated;
|
||||
if (score) params.score = score;
|
||||
if (season) params.season = season;
|
||||
if (language) params.language = language;
|
||||
if (genres) params.genres = genres;
|
||||
if (sort) params.sort = sort;
|
||||
if (sy) params.sy = sy;
|
||||
if (sm) params.sm = sm;
|
||||
if (sd) params.sd = sd;
|
||||
if (ey) params.ey = ey;
|
||||
if (em) params.em = em;
|
||||
if (ed) params.ed = ed;
|
||||
if (keyword) params.keyword = keyword;
|
||||
if (pageNum > 1) params.page = pageNum;
|
||||
|
||||
// Log params for debugging
|
||||
// console.log("Controller params:", params);
|
||||
|
||||
const [totalPage, data, currentPage, hasNextPage] = await extractFilterResults(params);
|
||||
|
||||
if (pageNum > totalPage) {
|
||||
const error = new Error("Requested page exceeds total available pages.");
|
||||
error.status = 404;
|
||||
throw error;
|
||||
}
|
||||
|
||||
return { data, totalPage, currentPage, hasNextPage };
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
if (e.status === 404) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("An error occurred while processing your request.");
|
||||
}
|
||||
};
|
||||
68
src/controllers/homeInfo.controller.js
Normal file
68
src/controllers/homeInfo.controller.js
Normal file
@@ -0,0 +1,68 @@
|
||||
import getSpotlights from "../extractors/spotlight.extractor.js";
|
||||
import getTrending from "../extractors/trending.extractor.js";
|
||||
import extractPage from "../helper/extractPages.helper.js";
|
||||
import extractTopTen from "../extractors/topten.extractor.js";
|
||||
import { routeTypes } from "../routes/category.route.js";
|
||||
import extractSchedule from "../extractors/schedule.extractor.js";
|
||||
import { getCachedData, setCachedData } from "../helper/cache.helper.js";
|
||||
|
||||
const genres = routeTypes
|
||||
.slice(0, 41)
|
||||
.map((genre) => genre.replace("genre/", ""));
|
||||
|
||||
export const getHomeInfo = async (req,res) => {
|
||||
// const cacheKey = "homeInfo";
|
||||
try {
|
||||
// const cachedResponse = await getCachedData(cacheKey);
|
||||
// if (cachedResponse && Object.keys(cachedResponse).length > 0) {
|
||||
// return cachedResponse;
|
||||
// }
|
||||
const [
|
||||
spotlights,
|
||||
trending,
|
||||
topTen,
|
||||
schedule,
|
||||
topAiring,
|
||||
mostPopular,
|
||||
mostFavorite,
|
||||
latestCompleted,
|
||||
latestEpisode,
|
||||
topUpcoming,
|
||||
recentlyAdded,
|
||||
] = await Promise.all([
|
||||
getSpotlights(),
|
||||
getTrending(),
|
||||
extractTopTen(),
|
||||
extractSchedule(new Date().toISOString().split("T")[0]),
|
||||
extractPage(1, "top-airing"),
|
||||
extractPage(1, "most-popular"),
|
||||
extractPage(1, "most-favorite"),
|
||||
extractPage(1, "completed"),
|
||||
extractPage(1, "recently-updated"),
|
||||
extractPage(1, "top-upcoming"),
|
||||
extractPage(1, "recently-added"),
|
||||
]);
|
||||
const responseData = {
|
||||
spotlights,
|
||||
trending,
|
||||
topTen,
|
||||
today: { schedule },
|
||||
topAiring: topAiring[0],
|
||||
mostPopular: mostPopular[0],
|
||||
mostFavorite: mostFavorite[0],
|
||||
latestCompleted: latestCompleted[0],
|
||||
latestEpisode: latestEpisode[0],
|
||||
topUpcoming: topUpcoming[0],
|
||||
recentlyAdded: recentlyAdded[0],
|
||||
genres,
|
||||
};
|
||||
|
||||
// setCachedData(cacheKey, responseData).catch((err) => {
|
||||
// console.error("Failed to set cache:", err);
|
||||
// });
|
||||
return responseData;
|
||||
} catch (fetchError) {
|
||||
console.error("Error fetching fresh data:", fetchError);
|
||||
return fetchError;
|
||||
}
|
||||
};
|
||||
12
src/controllers/nextEpisodeSchedule.controller.js
Normal file
12
src/controllers/nextEpisodeSchedule.controller.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import extractNextEpisodeSchedule from "../extractors/getNextEpisodeSchedule.extractor.js";
|
||||
|
||||
export const getNextEpisodeSchedule = async (req) => {
|
||||
const { id } = req.params;
|
||||
try {
|
||||
const nextEpisodeSchedule = await extractNextEpisodeSchedule(id);
|
||||
return { nextEpisodeSchedule: nextEpisodeSchedule };
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return e;
|
||||
}
|
||||
};
|
||||
32
src/controllers/producer.controller.js
Normal file
32
src/controllers/producer.controller.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import { getCachedData, setCachedData } from "../helper/cache.helper.js";
|
||||
import extractPage from "../helper/extractPages.helper.js";
|
||||
|
||||
export const getProducer = async (req) => {
|
||||
const { id } = req.params;
|
||||
const routeType = `producer/${id}`;
|
||||
const requestedPage = parseInt(req.query.page) || 1;
|
||||
// const cacheKey = `${routeType.replace(/\//g, "_")}_page_${requestedPage}`;
|
||||
try {
|
||||
// const cachedResponse = await getCachedData(cacheKey);
|
||||
// if (cachedResponse && Object.keys(cachedResponse).length > 0) {
|
||||
// return cachedResponse;
|
||||
// }
|
||||
const [data, totalPages] = await extractPage(requestedPage, routeType);
|
||||
if (requestedPage > totalPages) {
|
||||
const error = new Error("Requested page exceeds total available pages.");
|
||||
error.status = 404;
|
||||
throw error;
|
||||
}
|
||||
const responseData = { totalPages: totalPages, data: data };
|
||||
// setCachedData(cacheKey, responseData).catch((err) => {
|
||||
// console.error("Failed to set cache:", err);
|
||||
// });
|
||||
return { data, totalPages };
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
if (e.status === 404) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("An error occurred while processing your request.");
|
||||
}
|
||||
};
|
||||
12
src/controllers/qtip.controller.js
Normal file
12
src/controllers/qtip.controller.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import extractQtip from "../extractors/qtip.extractor.js";
|
||||
|
||||
export const getQtip = async (req) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const data = await extractQtip(id);
|
||||
return data;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return e;
|
||||
}
|
||||
};
|
||||
11
src/controllers/random.controller.js
Normal file
11
src/controllers/random.controller.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import extractRandom from "../extractors/random.extractor.js";
|
||||
|
||||
export const getRandom = async (req,res) => {
|
||||
try {
|
||||
const data = await extractRandom();
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error("Error getting random anime:", error.message);
|
||||
return e;
|
||||
}
|
||||
};
|
||||
11
src/controllers/randomId.controller.js
Normal file
11
src/controllers/randomId.controller.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import extractRandomId from "../extractors/randomId.extractor.js";
|
||||
|
||||
export const getRandomId = async (req,res) => {
|
||||
try {
|
||||
const data = await extractRandomId();
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error("Error getting random anime ID:", error.message);
|
||||
return e;
|
||||
}
|
||||
};
|
||||
13
src/controllers/schedule.controller.js
Normal file
13
src/controllers/schedule.controller.js
Normal file
@@ -0,0 +1,13 @@
|
||||
import extractSchedule from "../extractors/schedule.extractor.js";
|
||||
|
||||
export const getSchedule = async (req) => {
|
||||
const date = req.query.date;
|
||||
const tzOffset = req.query.tzOffset || -330;
|
||||
try {
|
||||
const data = await extractSchedule(date, tzOffset);
|
||||
return data;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return e;
|
||||
}
|
||||
};
|
||||
43
src/controllers/search.controller.js
Normal file
43
src/controllers/search.controller.js
Normal file
@@ -0,0 +1,43 @@
|
||||
import extractSearchResults from "../extractors/search.extractor.js";
|
||||
import convertForeignLanguage from "../helper/foreignInput.helper.js";
|
||||
|
||||
export const search = async (req) => {
|
||||
try {
|
||||
let { keyword, type, status, rated, score, season, language, genres, sort, sy, sm, sd, ey, em, ed } = req.query;
|
||||
let page = parseInt(req.query.page) || 1;
|
||||
|
||||
// Check if the search keyword is in a foreign language and if it can be converted
|
||||
keyword = await convertForeignLanguage(keyword);
|
||||
|
||||
const [totalPage, data] = await extractSearchResults({
|
||||
keyword: keyword,
|
||||
type: type,
|
||||
status: status,
|
||||
rated: rated,
|
||||
score: score,
|
||||
season: season,
|
||||
language: language,
|
||||
genres: genres,
|
||||
sort: sort,
|
||||
page: page,
|
||||
sy: sy,
|
||||
sm: sm,
|
||||
sd: sd,
|
||||
ey: ey,
|
||||
em: em,
|
||||
ed: ed,
|
||||
});
|
||||
if (page > totalPage) {
|
||||
const error = new Error("Requested page exceeds total available pages.");
|
||||
error.status = 404;
|
||||
throw error;
|
||||
}
|
||||
return { data, totalPage };
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
if (e.status === 404) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("An error occurred while processing your request.");
|
||||
}
|
||||
};
|
||||
12
src/controllers/servers.controller.js
Normal file
12
src/controllers/servers.controller.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import { extractServers } from "../extractors/streamInfo.extractor.js";
|
||||
|
||||
export const getServers = async (req) => {
|
||||
try {
|
||||
const { ep } = req.query;
|
||||
const servers = await extractServers(ep);
|
||||
return servers;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return e;
|
||||
}
|
||||
};
|
||||
17
src/controllers/streamInfo.controller.js
Normal file
17
src/controllers/streamInfo.controller.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import { extractStreamingInfo } from "../extractors/streamInfo.extractor.js";
|
||||
|
||||
export const getStreamInfo = async (req, res, fallback = false) => {
|
||||
try {
|
||||
const input = req.query.id;
|
||||
const server = req.query.server;
|
||||
const type = req.query.type;
|
||||
const match = input.match(/ep=(\d+)/);
|
||||
if (!match) throw new Error("Invalid URL format");
|
||||
const finalId = match[1];
|
||||
const streamingInfo = await extractStreamingInfo(finalId, server, type, fallback);
|
||||
return streamingInfo;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return { error: e.message };
|
||||
}
|
||||
};
|
||||
17
src/controllers/suggestion.controller.js
Normal file
17
src/controllers/suggestion.controller.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import getSuggestion from "../extractors/suggestion.extractor.js";
|
||||
import convertForeignLanguage from "../helper/foreignInput.helper.js";
|
||||
|
||||
export const getSuggestions = async (req) => {
|
||||
let { keyword } = req.query;
|
||||
|
||||
// Check if the search keyword is in a foreign language and if it can be converted
|
||||
keyword = await convertForeignLanguage(keyword);
|
||||
|
||||
try {
|
||||
const data = await getSuggestion(encodeURIComponent(keyword));
|
||||
return data;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return e;
|
||||
}
|
||||
};
|
||||
13
src/controllers/topsearch.controller.js
Normal file
13
src/controllers/topsearch.controller.js
Normal file
@@ -0,0 +1,13 @@
|
||||
import extractTopSearch from "../extractors/topsearch.extractor.js";
|
||||
|
||||
const getTopSearch = async () => {
|
||||
try {
|
||||
const data = await extractTopSearch();
|
||||
return data;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return e;
|
||||
}
|
||||
};
|
||||
|
||||
export default getTopSearch;
|
||||
22
src/controllers/topten.controller.js
Normal file
22
src/controllers/topten.controller.js
Normal file
@@ -0,0 +1,22 @@
|
||||
import extractTopTen from "../extractors/topten.extractor.js";
|
||||
import { getCachedData, setCachedData } from "../helper/cache.helper.js";
|
||||
|
||||
export const getTopTen = async (req,res) => {
|
||||
// const cacheKey = "topTen";
|
||||
try {
|
||||
// const cachedResponse = await getCachedData(cacheKey);
|
||||
// if (cachedResponse && Object.keys(cachedResponse).length > 0) {
|
||||
// return cachedResponse;
|
||||
// }
|
||||
const topTen = await extractTopTen();
|
||||
// await setCachedData(cacheKey, topTen).catch((err) => {
|
||||
// console.error("Failed to set cache:", err);
|
||||
// });
|
||||
return topTen;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return c
|
||||
.status(500)
|
||||
.json({ success: false, error: "Internal Server Error" });
|
||||
}
|
||||
};
|
||||
16
src/controllers/voiceactor.controller.js
Normal file
16
src/controllers/voiceactor.controller.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import extractVoiceActor from "../extractors/voiceactor.extractor.js";
|
||||
|
||||
export const getVoiceActors = async (req, res) => {
|
||||
const requestedPage = parseInt(req.query.page) || 1;
|
||||
const id = req.params.id;
|
||||
try {
|
||||
const { totalPages, charactersVoiceActors: data } = await extractVoiceActor(
|
||||
id,
|
||||
requestedPage
|
||||
);
|
||||
return { currentPage: requestedPage, totalPages, data };
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return e;
|
||||
}
|
||||
};
|
||||
40
src/controllers/watchlist.controller.js
Normal file
40
src/controllers/watchlist.controller.js
Normal file
@@ -0,0 +1,40 @@
|
||||
import extractWatchlist from "../extractors/watchlist.extractor.js";
|
||||
|
||||
export const getWatchlist = async (req, res) => {
|
||||
const { userId, page = 1 } = req.params;
|
||||
|
||||
try {
|
||||
const { watchlist, totalPages } = await extractWatchlist(userId, page);
|
||||
|
||||
// Restructuring the response
|
||||
return res.json({
|
||||
success: true,
|
||||
results: {
|
||||
totalPages, // Include total pages in the response
|
||||
data: watchlist.map(item => ({
|
||||
id: item.id,
|
||||
data_id: item.data_id,
|
||||
poster: item.poster,
|
||||
title: item.title,
|
||||
japanese_title: item.japanese_title,
|
||||
description: item.description,
|
||||
tvInfo: {
|
||||
showType: item.tvInfo.showType,
|
||||
duration: item.tvInfo.duration,
|
||||
sub: item.tvInfo.sub,
|
||||
dub: item.tvInfo.dub,
|
||||
// Include eps if it exists
|
||||
...(item.tvInfo.eps && { eps: item.tvInfo.eps })
|
||||
},
|
||||
adultContent: item.adultContent,
|
||||
}))
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error getting watchlist:", error.message);
|
||||
|
||||
if (!res.headersSent) {
|
||||
return res.status(500).json({ error: "An error occurred while fetching the watchlist." });
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user