feat: implement robust IMDb to MAL resolution for AniSkip support

This commit is contained in:
paregi12 2026-01-04 19:23:53 +05:30
parent 0919a40c75
commit 6a7d6a1458
2 changed files with 59 additions and 2 deletions

View file

@ -1,5 +1,6 @@
import axios from 'axios';
import { logger } from '../utils/logger';
import { tmdbService } from './tmdbService';
/**
* IntroDB API service for fetching TV show intro timestamps
@ -47,6 +48,56 @@ async function getMalIdFromKitsu(kitsuId: string): Promise<string | null> {
return null;
}
async function getMalIdFromImdb(imdbId: string): Promise<string | null> {
try {
// 1. Try direct Kitsu mapping (IMDb -> Kitsu)
const kitsuDirectResponse = await axios.get(`${KITSU_API_URL}/mappings`, {
params: {
'filter[external_site]': 'imdb',
'filter[external_id]': imdbId,
'include': 'item'
}
});
if (kitsuDirectResponse.data?.data?.length > 0) {
const kitsuId = kitsuDirectResponse.data.data[0].relationships?.item?.data?.id;
if (kitsuId) {
return await getMalIdFromKitsu(kitsuId);
}
}
// 2. Try TMDB -> TVDB -> Kitsu path (Robust for Cinemeta users)
const tmdbId = await tmdbService.findTMDBIdByIMDB(imdbId);
if (tmdbId) {
const extIds = await tmdbService.getShowExternalIds(tmdbId);
const tvdbId = extIds?.tvdb_id;
if (tvdbId) {
// Search Kitsu for TVDB mapping
const kitsuTvdbResponse = await axios.get(`${KITSU_API_URL}/mappings`, {
params: {
'filter[external_site]': 'thetvdb/series',
'filter[external_id]': tvdbId.toString(),
'include': 'item'
}
});
if (kitsuTvdbResponse.data?.data?.length > 0) {
const kitsuId = kitsuTvdbResponse.data.data[0].relationships?.item?.data?.id;
if (kitsuId) {
logger.log(`[IntroService] Resolved Kitsu ID ${kitsuId} from TVDB ID ${tvdbId} (via IMDb ${imdbId})`);
return await getMalIdFromKitsu(kitsuId);
}
}
}
}
} catch (error) {
// Silent fail - it might just not be an anime or API limit reached
}
return null;
}
async function fetchFromAniSkip(malId: string, episode: number): Promise<SkipInterval[]> {
try {
// Fetch OP, ED, and Recap
@ -129,6 +180,12 @@ export async function getSkipTimes(
finalMalId = await getMalIdFromKitsu(kitsuId) || undefined;
}
// If we still don't have MAL ID but have IMDb ID (e.g. Cinemeta), try to resolve it
if (!finalMalId && imdbId) {
logger.log(`[IntroService] Attempting to resolve MAL ID from IMDb ID: ${imdbId}`);
finalMalId = await getMalIdFromImdb(imdbId) || undefined;
}
if (finalMalId) {
logger.log(`[IntroService] Fetching AniSkip for MAL ID: ${finalMalId} Ep: ${episode}`);
const aniSkipIntervals = await fetchFromAniSkip(finalMalId, episode);

View file

@ -850,9 +850,9 @@ export class TMDBService {
}
/**
* Get external IDs for a TV show (including IMDb ID)
* Get external IDs for a TV show (including IMDb ID and TVDB ID)
*/
async getShowExternalIds(tmdbId: number): Promise<{ imdb_id: string | null } | null> {
async getShowExternalIds(tmdbId: number): Promise<{ imdb_id: string | null, tvdb_id?: number | null, [key: string]: any } | null> {
const cacheKey = this.generateCacheKey(`tv_${tmdbId}_external_ids`);
// Check cache (local or remote)