Refactor sources that need TMDB title in a fixed language

This commit is contained in:
vlOd 2025-12-10 03:26:06 +02:00
parent 6544795344
commit 2faa1816e5
5 changed files with 28 additions and 36 deletions

View file

@ -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<string> {
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<Record<string, string>> {
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<Record<string, string>> {
async function comboScraper(ctx: ShowScrapeContext | MovieScrapeContext): Promise<SourcererOutput> {
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 =

View file

@ -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<Media>(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');
}

View file

@ -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<string, string>, embeds:
}
async function comboScraper(ctx: ShowScrapeContext | MovieScrapeContext): Promise<SourcererOutput> {
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,

View file

@ -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<string> {
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<FetcherResponse | undefined> {
const response: FetcherResponse = await ctx.proxiedFetcher.full(url, {
headers: {

19
src/utils/tmdb.ts Normal file
View file

@ -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<string> {
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;
}