calc / src /aither.js
no1b4me's picture
Upload 15 files
ea5d04f verified
Raw History Blame Contribute Delete
4.85 kB
import https from 'https';
const API_CONFIG = {
baseUrl: 'https://aither.cc',
apiKey: 'dRkYjNbT0S4ObFArTH1FXUTLPGsFRV4SqpPwwwEIo0IAtzsKhfF3zkPpTpCOcyBzab5q068680zVeHPk644q9sshtqpMv5mglfou'
};
const agent = new https.Agent({
rejectUnauthorized: true,
timeout: 30000,
keepAlive: true
});
async function fetchWithTimeout(url, options = {}, timeout = 30000) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
try {
const response = await fetch(url, {
...options,
signal: controller.signal,
agent
});
clearTimeout(timeoutId);
return response;
} catch (error) {
clearTimeout(timeoutId);
throw error;
}
}
async function fetchTorrents(imdbId) {
console.log('\n🔄 Fetching from Aither API for:', imdbId);
try {
const params = new URLSearchParams({
imdbId: imdbId.replace('tt', ''),
sortField: 'created_at',
sortDirection: 'desc',
perPage: '100'
});
const url = `${API_CONFIG.baseUrl}/api/torrents/filter?${params}`;
console.log('Request URL:', url);
const response = await fetchWithTimeout(url, {
headers: {
'Host': 'aither.cc',
'Authorization': `Bearer ${API_CONFIG.apiKey}`,
'Accept': '*/*',
'Accept-Language': '*',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
'Accept-Encoding': 'gzip, deflate'
},
method: 'GET',
compress: true
});
if (!response.ok) {
throw new Error(`API request failed: ${response.status}`);
}
const data = await response.json();
if (!data.data || !Array.isArray(data.data)) {
console.log('No torrents found');
return [];
}
console.log(`Found ${data.data.length} items`);
const streams = await Promise.all(data.data.map(async (item, index) => {
try {
console.log(`\nProcessing item ${index + 1}/${data.data.length}:`, item.attributes.name);
// Check if any of the files are video files
const hasVideoFiles = item.attributes.files.some(file =>
/\.(mp4|mkv|avi|mov|wmv)$/i.test(file.name)
);
if (!hasVideoFiles) {
console.log('No video files found in:', item.attributes.name);
return null;
}
// Find the main video file (largest video file)
const videoFiles = item.attributes.files
.filter(file => /\.(mp4|mkv|avi|mov|wmv)$/i.test(file.name))
.sort((a, b) => b.size - a.size);
const mainFile = videoFiles[0];
return {
magnetLink: `magnet:?xt=urn:btih:${item.attributes.info_hash}`,
filename: mainFile.name,
websiteTitle: item.attributes.name,
quality: extractQuality(item.attributes.name),
size: formatSize(item.attributes.size),
source: 'Aither',
infoHash: item.attributes.info_hash,
mainFileSize: mainFile.size
};
} catch (error) {
console.error(`Error processing item ${index + 1}:`, error);
return null;
}
}));
const validStreams = streams.filter(Boolean);
console.log(`✅ Processed ${validStreams.length} valid streams`);
validStreams.sort((a, b) => {
const qualityOrder = { '2160p': 4, '4k': 4, '1080p': 3, '720p': 2 };
return (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
});
return validStreams;
} catch (error) {
console.error('❌ Error fetching from Aither:', error);
console.error('Error details:', {
message: error.message,
cause: error.cause,
code: error.code,
syscall: error.syscall
});
return [];
}
}
function extractQuality(title) {
const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);
return qualityMatch ? qualityMatch[1].toLowerCase() : '';
}
function formatSize(bytes) {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes === 0) return '0 Bytes';
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i];
}
export { fetchTorrents };