update hianime to always search english title

This commit is contained in:
Pas 2025-06-06 10:28:08 -06:00
parent e1ea6a4e99
commit 5827b35272

View file

@ -4,6 +4,9 @@ import { SourcererOutput, makeSourcerer } from '@/providers/base';
import { ShowScrapeContext } from '@/utils/context';
import { NotFoundError } from '@/utils/errors';
const apiKey = '5b9790d9305dca8713b9a0afad42ea8d'; // plz dont abuse
const baseUrl = 'https://hianime.pstream.org/';
interface HianimeSearchResult {
success: boolean;
data: {
@ -26,7 +29,7 @@ interface HianimeEpisodeResult {
}
async function searchAnime(title: string): Promise<string> {
const response = await fetch(`https://hianime.pstream.org/api/v2/hianime/search?q=${encodeURIComponent(title)}`);
const response = await fetch(`${baseUrl}api/v2/hianime/search?q=${encodeURIComponent(title)}`);
if (!response.ok) throw new Error('Failed to search anime');
const data: HianimeSearchResult = await response.json();
@ -41,8 +44,16 @@ async function searchAnime(title: string): Promise<string> {
return match?.id ?? data.data.animes[0].id;
}
async function fetchTmdbShowDetails(tmdbShowId: string): Promise<string> {
const response = await fetch(`https://api.themoviedb.org/3/tv/${tmdbShowId}?api_key=${apiKey}`);
if (!response.ok) throw new NotFoundError('Failed to fetch show data from TMDB');
const data = await response.json();
// Return the English title, falling back to the original title if not available
return data.name || data.original_name;
}
async function fetchTmdbSeasonEpisodes(tmdbShowId: string, seasonNumber: number): Promise<any[]> {
const apiKey = '5b9790d9305dca8713b9a0afad42ea8d'; // plz dont abuse
const response = await fetch(
`https://api.themoviedb.org/3/tv/${tmdbShowId}/season/${seasonNumber}?api_key=${apiKey}`,
);
@ -66,14 +77,17 @@ async function calculateAbsoluteEpisodeNumber(
}
async function fetchEpisodeData(animeId: string): Promise<HianimeEpisodeResult> {
const response = await fetch(`https://hianime.pstream.org/api/v2/hianime/anime/${animeId}/episodes`);
const response = await fetch(`${baseUrl}api/v2/hianime/anime/${animeId}/episodes`);
if (!response.ok) throw new NotFoundError('Failed to fetch episode data');
return response.json();
}
async function comboScraper(ctx: ShowScrapeContext): Promise<SourcererOutput> {
const animeId = await searchAnime(ctx.media.title);
// console.log(animeId);
// Get the English title from TMDB first
const englishTitle = await fetchTmdbShowDetails(ctx.media.tmdbId);
// Use the English title to search for the anime
const animeId = await searchAnime(englishTitle);
const absoluteEp = await calculateAbsoluteEpisodeNumber(
ctx.media.tmdbId,