// Add this new endpoint for handling stream errors
app.get('/:apiKeys/error/:message', (req, res) => {
const { message } = req.params;
const decodedMessage = decodeURIComponent(message);
// Return a simple HTML page that Stremio can display
res.set('Content-Type', 'text/html');
res.send(`
Stream Not Available
ā ļø Stream Not Available
${decodedMessage}
Returning to sources in 3 seconds...
`);
});
// Updated magnet handler
app.get('/:apiKeys/:magnetLink', async (req, res) => {
const { apiKeys, magnetLink } = req.params;
try {
const debridServices = getDebridServices(apiKeys);
if (!debridServices.length) {
// Redirect to error page instead of throwing
const errorMsg = encodeURIComponent('No valid debrid service configured');
return res.redirect(`/${apiKeys}/error/${errorMsg}`);
}
console.log('\nš§² Processing magnet request');
const decodedMagnet = base64Decode(magnetLink);
const hash = extractInfoHash(decodedMagnet)?.toLowerCase();
if (!hash) {
const errorMsg = encodeURIComponent('Invalid magnet link - no hash found');
return res.redirect(`/${apiKeys}/error/${errorMsg}`);
}
// Optional: detect media type from referer
const mediaInfo = (() => {
try {
const urlParts = req.get('referer')?.split('/') || [];
const streamIndex = urlParts.findIndex(part => part === 'stream');
if (streamIndex !== -1 && urlParts[streamIndex + 1] === 'series') {
const seriesId = urlParts[streamIndex + 2];
const [, season, episode] = seriesId.split(':');
return {
type: 'series',
season: parseInt(season),
episode: parseInt(episode)
};
}
return { type: 'movie' };
} catch (err) {
console.warn('Could not extract media info from referer');
return { type: 'movie' };
}
})();
// Track which services failed and why
const serviceErrors = [];
for (const service of debridServices) {
try {
const serviceName = service.constructor.name;
console.log(`Trying service: ${serviceName}`);
if (service instanceof RealDebrid) {
// ā
Check cache status first
const availability = await service.checkInstantAvailability([hash]);
if (!availability[hash]?.cached) {
console.log(`ā ${serviceName}: Torrent is not cached, skipping`);
serviceErrors.push(`${serviceName}: Not cached`);
continue;
}
// ā
Get stream only if cached
const result = await service.getCachedUrl(decodedMagnet);
if (typeof result === 'string' && result.startsWith('http')) {
console.log(`ā
${serviceName} success - redirecting to stream`);
return res.redirect(result);
} else {
console.log(`ā ${serviceName} failed - no cached URL returned`);
serviceErrors.push(`${serviceName}: No cached URL available`);
continue;
}
} else {
// Handle other services normally
const streamUrl = await service.getStreamUrl(decodedMagnet);
console.log(`ā
${serviceName} success - redirecting to stream`);
return res.redirect(streamUrl);
}
} catch (error) {
if (error.message === ERROR.NOT_PREMIUM) {
console.log(`ā ļø Skipping non-premium service`);
serviceErrors.push(`${service.constructor.name}: Not premium`);
continue;
}
console.error(`${service.constructor.name} failed:`, error.message);
serviceErrors.push(`${service.constructor.name}: ${error.message}`);
}
}
// If we get here, no service could provide the stream
console.log('ā No cached stream available from any debrid service');
console.log('Service errors:', serviceErrors);
// Redirect to error page with a user-friendly message
const errorMsg = encodeURIComponent(
'Content not cached on your debrid services. ' +
serviceErrors.join(', ')
);
return res.redirect(`/${apiKeys}/error/${errorMsg}`);
} catch (error) {
console.error('ā Error processing magnet:', error.message);
// Redirect to error page instead of JSON response
const errorMsg = encodeURIComponent(`Stream processing failed: ${error.message}`);
return res.redirect(`/${apiKeys}/error/${errorMsg}`);
}
});