diff --git a/src/providers/sources/cuevana3.ts b/src/providers/sources/cuevana3.ts index 87adbb1..d68d8fa 100644 --- a/src/providers/sources/cuevana3.ts +++ b/src/providers/sources/cuevana3.ts @@ -4,6 +4,7 @@ import { flags } from '@/entrypoint/utils/targets'; import { SourcererOutput, makeSourcerer } from '@/providers/base'; import { MovieScrapeContext, ShowScrapeContext } from '@/utils/context'; import { NotFoundError } from '@/utils/errors'; +import { fetchTMDBName } from '@/utils/tmdb'; const baseUrl = 'https://www.cuevana3.eu'; @@ -88,20 +89,6 @@ async function extractVideos(ctx: MovieScrapeContext | ShowScrapeContext, videos return videoList; } -async function fetchTmdbTitleInSpanish(tmdbId: number, apiKey: string, mediaType: 'movie' | 'show'): Promise { - const endpoint = - mediaType === 'movie' - ? `https://api.themoviedb.org/3/movie/${tmdbId}?api_key=${apiKey}&language=es-ES` - : `https://api.themoviedb.org/3/tv/${tmdbId}?api_key=${apiKey}&language=es-ES`; - - const response = await fetch(endpoint); - if (!response.ok) { - throw new Error(`Error fetching TMDB data: ${response.statusText}`); - } - const tmdbData = await response.json(); - return mediaType === 'movie' ? tmdbData.title : tmdbData.name; -} - async function fetchTitleSubstitutes(): Promise> { try { const response = await fetch('https://raw.githubusercontent.com/moonpic/fixed-titles/refs/heads/main/main.json'); @@ -115,13 +102,12 @@ async function fetchTitleSubstitutes(): Promise> { async function comboScraper(ctx: ShowScrapeContext | MovieScrapeContext): Promise { const mediaType = ctx.media.type; const tmdbId = ctx.media.tmdbId; - const apiKey = 'a500049f3e06109fe3e8289b06cf5685'; if (!tmdbId) { throw new NotFoundError('TMDB ID is required to fetch the title in Spanish'); } - const translatedTitle = await fetchTmdbTitleInSpanish(Number(tmdbId), apiKey, mediaType); + const translatedTitle = await fetchTMDBName(ctx, 'es-ES'); let normalizedTitle = normalizeTitle(translatedTitle); let pageUrl = diff --git a/src/providers/sources/dopebox/index.ts b/src/providers/sources/dopebox/index.ts index 6721e99..fcfb1af 100644 --- a/src/providers/sources/dopebox/index.ts +++ b/src/providers/sources/dopebox/index.ts @@ -2,6 +2,7 @@ import Fuse from 'fuse.js'; import { SourcererEmbed, SourcererOutput, makeEmbed, makeSourcerer } from '@/providers/base'; import { MovieScrapeContext, ShowScrapeContext } from '@/utils/context'; +import { fetchTMDBName } from '@/utils/tmdb'; import { Media, MediaPlayer, getEpisodePlayers, getEpisodes, getMoviePlayers, getSeasons, searchMedia } from './search'; import { scrapeUpCloudEmbed } from './upcloud'; @@ -13,12 +14,13 @@ async function handleContext(ctx: ShowScrapeContext | MovieScrapeContext) { } const mediaType = ctx.media.type === 'show' ? 'TV' : 'Movie'; - const results = (await searchMedia(ctx, getSearchQuery(ctx.media.title))).filter((r) => r.info.includes(mediaType)); + const mediaTitle = await fetchTMDBName(ctx); + const results = (await searchMedia(ctx, getSearchQuery(mediaTitle))).filter((r) => r.info.includes(mediaType)); const fuse = new Fuse(results, { keys: ['title'], }); - const media = fuse.search(ctx.media.title).find((r) => r.item.info.includes(ctx.media.releaseYear.toString()))?.item; + const media = fuse.search(mediaTitle).find((r) => r.item.info.includes(ctx.media.releaseYear.toString()))?.item; if (!media) { throw new Error('Could not find movie'); } diff --git a/src/providers/sources/fsonline/index.ts b/src/providers/sources/fsonline/index.ts index 0e8df62..fc48728 100644 --- a/src/providers/sources/fsonline/index.ts +++ b/src/providers/sources/fsonline/index.ts @@ -4,9 +4,10 @@ import type { CheerioAPI } from 'cheerio'; import { FetcherResponse } from '@/fetchers/types'; import { SourcererEmbed, SourcererOutput, makeEmbed, makeSourcerer } from '@/providers/base'; import { MovieScrapeContext, ScrapeContext, ShowScrapeContext } from '@/utils/context'; +import { fetchTMDBName } from '@/utils/tmdb'; import { scrapeDoodstreamEmbed } from './doodstream'; -import { EMBED_URL, ORIGIN_HOST, fetchENTMDBName, getMoviePageURL, throwOnResponse } from './utils'; +import { EMBED_URL, ORIGIN_HOST, getMoviePageURL, throwOnResponse } from './utils'; export const LOG_PREFIX = '[FSOnline]'; @@ -87,7 +88,7 @@ function addEmbedFromSources(name: string, sources: Map, embeds: } async function comboScraper(ctx: ShowScrapeContext | MovieScrapeContext): Promise { - const movieName = await fetchENTMDBName(Number(ctx.media.tmdbId), ctx.media.type); + const movieName = await fetchTMDBName(ctx); const moviePageURL = getMoviePageURL( ctx.media.type === 'movie' ? `${movieName} ${ctx.media.releaseYear}` : movieName, ctx.media.type === 'show' ? ctx.media.season.number : undefined, diff --git a/src/providers/sources/fsonline/utils.ts b/src/providers/sources/fsonline/utils.ts index 157e9d7..f6983ba 100644 --- a/src/providers/sources/fsonline/utils.ts +++ b/src/providers/sources/fsonline/utils.ts @@ -5,7 +5,6 @@ export const ORIGIN_HOST = 'https://www3.fsonline.app'; export const MOVIE_PAGE_URL = 'https://www3.fsonline.app/film/'; export const SHOW_PAGE_URL = 'https://www3.fsonline.app/episoade/{{MOVIE}}-sezonul-{{SEASON}}-episodul-{{EPISODE}}/'; export const EMBED_URL = 'https://www3.fsonline.app/wp-admin/admin-ajax.php'; -const TMDB_API_KEY = 'a500049f3e06109fe3e8289b06cf5685'; export function throwOnResponse(response: FetcherResponse) { if (response.statusCode >= 400) { @@ -30,21 +29,6 @@ export function getMoviePageURL(name: string, season?: number, episode?: number) return `${MOVIE_PAGE_URL}${n}/`; } -export async function fetchENTMDBName(tmdbId: number, mediaType: 'movie' | 'show'): Promise { - const endpoint = - mediaType === 'movie' - ? `https://api.themoviedb.org/3/movie/${tmdbId}?api_key=${TMDB_API_KEY}&language=en-US` - : `https://api.themoviedb.org/3/tv/${tmdbId}?api_key=${TMDB_API_KEY}&language=en-US`; - - const response = await fetch(endpoint); - if (!response.ok) { - throw new Error(`Error fetching TMDB data: ${response.statusText}`); - } - - const tmdbData = await response.json(); - return mediaType === 'movie' ? tmdbData.title : tmdbData.name; -} - export async function fetchIFrame(ctx: ScrapeContext, url: string): Promise { const response: FetcherResponse = await ctx.proxiedFetcher.full(url, { headers: { diff --git a/src/utils/tmdb.ts b/src/utils/tmdb.ts new file mode 100644 index 0000000..8bb3a2f --- /dev/null +++ b/src/utils/tmdb.ts @@ -0,0 +1,19 @@ +import { MovieScrapeContext, ShowScrapeContext } from '@/utils/context'; + +const TMDB_API_KEY = 'a500049f3e06109fe3e8289b06cf5685'; + +export async function fetchTMDBName( + ctx: ShowScrapeContext | MovieScrapeContext, + lang: string = 'en-US', +): Promise { + const type = ctx.media.type === 'movie' ? 'movie' : 'tv'; + const url = `https://api.themoviedb.org/3/${type}/${ctx.media.tmdbId}?api_key=${TMDB_API_KEY}&language=${lang}`; + + const response = await fetch(url); + if (!response.ok) { + throw new Error(`Error fetching TMDB data: ${response.statusText}`); + } + + const data = await response.json(); + return ctx.media.type === 'movie' ? data.title : data.name; +}