Create torr9.js
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140import fetch from 'node-fetch';import { JSDOM } from 'jsdom';
// Function to extract video quality from the titlefunction extractQuality(title) { if (!title) return ''; const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i); return qualityMatch ? qualityMatch[1].toLowerCase() : '';}
// Function to extract hash from torrentz9 detail URLsfunction extractHash(detailUrl) { const hashMatch = detailUrl.match(/\/([A-Fa-f0-9]{40})$/); return hashMatch ? hashMatch[1] : '';}
// Function to generate a direct magnet link from a hashfunction createMagnetLink(hash) { return `magnet:?xt=urn:btih:${hash}`;}
// β
Function to extract magnet links from torrentz9 detail pagesasync function extractMagnetLinksFromPage(url) { try { await new Promise(resolve => setTimeout(resolve, 2000)); // Prevents rate-limiting
const response = await fetch(url, { headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36', 'Referer': 'https://torrentz9.org/', 'Accept-Language': 'en-US,en;q=0.9', 'Connection': 'keep-alive', 'DNT': '1', 'Upgrade-Insecure-Requests': '1' } });
if (!response.ok) { console.error(`β Failed to fetch page (${response.status}):`, url); return []; }
const html = await response.text(); const dom = new JSDOM(html);
// β
Extract all magnet links const magnetLinks = Array.from( dom.window.document.querySelectorAll('a[href^="magnet:?xt=urn:btih:"]') ).map(link => link.href);
return [...new Set(magnetLinks)]; // β
Return unique magnet links } catch (error) { console.error('β Error fetching magnet links:', error); return []; }}
// β
Main function to search torrentz9 and extract torrent dataasync function fetchRSSFeeds(searchQuery) { console.log(`π Searching Torrentz9 for: "${searchQuery}"`);
try { await new Promise(resolve => setTimeout(resolve, 2000)); // Prevents bot detection
const url = `https://torrentz9.org/search?keyword=${encodeURIComponent(searchQuery)}`; const response = await fetch(url, { headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36', 'Referer': 'https://torrentz9.org/', 'Accept-Language': 'en-US,en;q=0.9', 'Connection': 'keep-alive', 'DNT': '1', 'Upgrade-Insecure-Requests': '1' } });
if (!response.ok) { throw new Error(`Failed to fetch Torrentz9 search page: ${response.status}`); }
const html = await response.text(); const dom = new JSDOM(html); const torrents = [];
// β
Extract torrent data from table rows dom.window.document.querySelectorAll('tr').forEach(row => { const titleElement = row.querySelector('td:first-child a'); const sizeElement = row.querySelector('td:nth-child(2)'); const seedersElement = row.querySelector('td:nth-child(3) .text-seeders'); const leechersElement = row.querySelector('td:nth-child(4) .text-danger');
if (!titleElement || !sizeElement) return;
const title = titleElement.textContent.trim(); const detailUrl = 'https://torrentz9.org' + titleElement.getAttribute('href'); const quality = extractQuality(title); const size = sizeElement.textContent.trim(); const seeders = seedersElement ? parseInt(seedersElement.textContent.trim()) || 0 : 0; const leechers = leechersElement ? parseInt(leechersElement.textContent.trim()) || 0 : 0;
// β
Extract hash from detail URL to generate magnet link const hash = extractHash(detailUrl); const magnetLink = hash ? createMagnetLink(hash) : null;
if (magnetLink) { torrents.push({ title, detailUrl, quality, size, seeders, leechers, magnetLink }); } });
// β
If magnet links are missing, try to fetch from detail pages await Promise.all(torrents.map(async (torrent) => { if (!torrent.magnetLink && torrent.detailUrl) { const magnetLinks = await extractMagnetLinksFromPage(torrent.detailUrl); if (magnetLinks.length > 0) { torrent.magnetLink = magnetLinks[0]; // β
Assign first valid magnet link } } }));
// β
Ensure only torrents with magnet links are returned const validTorrents = torrents.filter(torrent => torrent.magnetLink);
console.log(`β
Found ${validTorrents.length} torrents with magnet links.`); return validTorrents;
} catch (error) { console.error('β Error fetching from Torrentz9:', error); return []; }}
export { fetchRSSFeeds };torr9.jsAdded+140