trakt comment fix

This commit is contained in:
tapframe 2025-12-09 14:24:49 +05:30
parent d457db5053
commit 057c709b41

View file

@ -1213,54 +1213,59 @@ export class TraktService {
*/ */
public async getTraktIdFromImdbId(imdbId: string, type: 'movie' | 'show'): Promise<number | null> { public async getTraktIdFromImdbId(imdbId: string, type: 'movie' | 'show'): Promise<number | null> {
try { try {
// Clean IMDb ID - remove 'tt' prefix if present // Ensure IMDb ID has the 'tt' prefix - Trakt API requires it for exact matches
const cleanImdbId = imdbId.startsWith('tt') ? imdbId.substring(2) : imdbId; const fullImdbId = imdbId.startsWith('tt') ? imdbId : `tt${imdbId}`;
logger.log(`[TraktService] Searching Trakt for ${type} with IMDb ID: ${cleanImdbId}`); logger.log(`[TraktService] Searching Trakt for ${type} with IMDb ID: ${fullImdbId}`);
// Try multiple search approaches // Use the correct Trakt API endpoint for exact IMDb ID lookup: /search/imdb/{id}
const searchUrls = [ // This returns exact matches instead of a general search
`${TRAKT_API_URL}/search/${type === 'show' ? 'show' : type}?id_type=imdb&id=${cleanImdbId}`, const searchUrl = `${TRAKT_API_URL}/search/imdb/${fullImdbId}?type=${type}`;
`${TRAKT_API_URL}/search/${type === 'show' ? 'show' : type}?query=${encodeURIComponent(cleanImdbId)}&id_type=imdb`,
// Also try with the full tt-prefixed ID in case the API accepts it
`${TRAKT_API_URL}/search/${type === 'show' ? 'show' : type}?id_type=imdb&id=tt${cleanImdbId}`
];
for (const searchUrl of searchUrls) { try {
try { logger.log(`[TraktService] Trying search URL: ${searchUrl}`);
logger.log(`[TraktService] Trying search URL: ${searchUrl}`);
const response = await fetch(searchUrl, { const response = await fetch(searchUrl, {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'trakt-api-version': '2', 'trakt-api-version': '2',
'trakt-api-key': TRAKT_CLIENT_ID 'trakt-api-key': TRAKT_CLIENT_ID
}
});
if (!response.ok) {
const errorText = await response.text();
logger.warn(`[TraktService] Search attempt failed (${response.status}): ${errorText}`);
continue; // Try next URL
} }
});
const data = await response.json(); if (!response.ok) {
logger.log(`[TraktService] Search response data:`, data); const errorText = await response.text();
logger.warn(`[TraktService] Search attempt failed (${response.status}): ${errorText}`);
return null;
}
if (data && data.length > 0) { const data = await response.json();
const traktId = data[0][type === 'show' ? 'show' : type]?.ids?.trakt; logger.log(`[TraktService] Search response data:`, data);
if (data && data.length > 0) {
// Find the result that matches our requested type
const matchingResult = data.find((item: any) => item.type === type);
if (matchingResult) {
const traktId = matchingResult[type]?.ids?.trakt;
if (traktId) { if (traktId) {
logger.log(`[TraktService] Found Trakt ID: ${traktId} for IMDb ID: ${cleanImdbId}`); logger.log(`[TraktService] Found Trakt ID: ${traktId} for IMDb ID: ${fullImdbId}`);
return traktId; return traktId;
} }
} }
} catch (urlError) {
logger.warn(`[TraktService] URL attempt failed:`, urlError); // Fallback: try the first result if type filtering didn't work
continue; const traktId = data[0][type]?.ids?.trakt;
if (traktId) {
logger.log(`[TraktService] Found Trakt ID (fallback): ${traktId} for IMDb ID: ${fullImdbId}`);
return traktId;
}
} }
} catch (urlError) {
logger.warn(`[TraktService] URL attempt failed:`, urlError);
} }
logger.warn(`[TraktService] No results found for IMDb ID: ${cleanImdbId} after trying all search methods`); logger.warn(`[TraktService] No results found for IMDb ID: ${fullImdbId}`);
return null; return null;
} catch (error) { } catch (error) {
logger.error('[TraktService] Failed to get Trakt ID from IMDb ID:', error); logger.error('[TraktService] Failed to get Trakt ID from IMDb ID:', error);