Create torr9.js
Browse files
torr9.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fetch from 'node-fetch';
|
| 2 |
+
import { JSDOM } from 'jsdom';
|
| 3 |
+
|
| 4 |
+
// Function to extract video quality from the title
|
| 5 |
+
function extractQuality(title) {
|
| 6 |
+
if (!title) return '';
|
| 7 |
+
const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);
|
| 8 |
+
return qualityMatch ? qualityMatch[1].toLowerCase() : '';
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
// Function to extract hash from torrentz9 detail URLs
|
| 12 |
+
function extractHash(detailUrl) {
|
| 13 |
+
const hashMatch = detailUrl.match(/\/([A-Fa-f0-9]{40})$/);
|
| 14 |
+
return hashMatch ? hashMatch[1] : '';
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
// Function to generate a direct magnet link from a hash
|
| 18 |
+
function createMagnetLink(hash) {
|
| 19 |
+
return `magnet:?xt=urn:btih:${hash}`;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
// β
Function to extract magnet links from torrentz9 detail pages
|
| 23 |
+
async function extractMagnetLinksFromPage(url) {
|
| 24 |
+
try {
|
| 25 |
+
await new Promise(resolve => setTimeout(resolve, 2000)); // Prevents rate-limiting
|
| 26 |
+
|
| 27 |
+
const response = await fetch(url, {
|
| 28 |
+
headers: {
|
| 29 |
+
'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',
|
| 30 |
+
'Referer': 'https://torrentz9.org/',
|
| 31 |
+
'Accept-Language': 'en-US,en;q=0.9',
|
| 32 |
+
'Connection': 'keep-alive',
|
| 33 |
+
'DNT': '1',
|
| 34 |
+
'Upgrade-Insecure-Requests': '1'
|
| 35 |
+
}
|
| 36 |
+
});
|
| 37 |
+
|
| 38 |
+
if (!response.ok) {
|
| 39 |
+
console.error(`β Failed to fetch page (${response.status}):`, url);
|
| 40 |
+
return [];
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
const html = await response.text();
|
| 44 |
+
const dom = new JSDOM(html);
|
| 45 |
+
|
| 46 |
+
// β
Extract all magnet links
|
| 47 |
+
const magnetLinks = Array.from(
|
| 48 |
+
dom.window.document.querySelectorAll('a[href^="magnet:?xt=urn:btih:"]')
|
| 49 |
+
).map(link => link.href);
|
| 50 |
+
|
| 51 |
+
return [...new Set(magnetLinks)]; // β
Return unique magnet links
|
| 52 |
+
} catch (error) {
|
| 53 |
+
console.error('β Error fetching magnet links:', error);
|
| 54 |
+
return [];
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
// β
Main function to search torrentz9 and extract torrent data
|
| 59 |
+
async function fetchRSSFeeds(searchQuery) {
|
| 60 |
+
console.log(`π Searching Torrentz9 for: "${searchQuery}"`);
|
| 61 |
+
|
| 62 |
+
try {
|
| 63 |
+
await new Promise(resolve => setTimeout(resolve, 2000)); // Prevents bot detection
|
| 64 |
+
|
| 65 |
+
const url = `https://torrentz9.org/search?keyword=${encodeURIComponent(searchQuery)}`;
|
| 66 |
+
const response = await fetch(url, {
|
| 67 |
+
headers: {
|
| 68 |
+
'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',
|
| 69 |
+
'Referer': 'https://torrentz9.org/',
|
| 70 |
+
'Accept-Language': 'en-US,en;q=0.9',
|
| 71 |
+
'Connection': 'keep-alive',
|
| 72 |
+
'DNT': '1',
|
| 73 |
+
'Upgrade-Insecure-Requests': '1'
|
| 74 |
+
}
|
| 75 |
+
});
|
| 76 |
+
|
| 77 |
+
if (!response.ok) {
|
| 78 |
+
throw new Error(`Failed to fetch Torrentz9 search page: ${response.status}`);
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
const html = await response.text();
|
| 82 |
+
const dom = new JSDOM(html);
|
| 83 |
+
const torrents = [];
|
| 84 |
+
|
| 85 |
+
// β
Extract torrent data from table rows
|
| 86 |
+
dom.window.document.querySelectorAll('tr').forEach(row => {
|
| 87 |
+
const titleElement = row.querySelector('td:first-child a');
|
| 88 |
+
const sizeElement = row.querySelector('td:nth-child(2)');
|
| 89 |
+
const seedersElement = row.querySelector('td:nth-child(3) .text-seeders');
|
| 90 |
+
const leechersElement = row.querySelector('td:nth-child(4) .text-danger');
|
| 91 |
+
|
| 92 |
+
if (!titleElement || !sizeElement) return;
|
| 93 |
+
|
| 94 |
+
const title = titleElement.textContent.trim();
|
| 95 |
+
const detailUrl = 'https://torrentz9.org' + titleElement.getAttribute('href');
|
| 96 |
+
const quality = extractQuality(title);
|
| 97 |
+
const size = sizeElement.textContent.trim();
|
| 98 |
+
const seeders = seedersElement ? parseInt(seedersElement.textContent.trim()) || 0 : 0;
|
| 99 |
+
const leechers = leechersElement ? parseInt(leechersElement.textContent.trim()) || 0 : 0;
|
| 100 |
+
|
| 101 |
+
// β
Extract hash from detail URL to generate magnet link
|
| 102 |
+
const hash = extractHash(detailUrl);
|
| 103 |
+
const magnetLink = hash ? createMagnetLink(hash) : null;
|
| 104 |
+
|
| 105 |
+
if (magnetLink) {
|
| 106 |
+
torrents.push({
|
| 107 |
+
title,
|
| 108 |
+
detailUrl,
|
| 109 |
+
quality,
|
| 110 |
+
size,
|
| 111 |
+
seeders,
|
| 112 |
+
leechers,
|
| 113 |
+
magnetLink
|
| 114 |
+
});
|
| 115 |
+
}
|
| 116 |
+
});
|
| 117 |
+
|
| 118 |
+
// β
If magnet links are missing, try to fetch from detail pages
|
| 119 |
+
await Promise.all(torrents.map(async (torrent) => {
|
| 120 |
+
if (!torrent.magnetLink && torrent.detailUrl) {
|
| 121 |
+
const magnetLinks = await extractMagnetLinksFromPage(torrent.detailUrl);
|
| 122 |
+
if (magnetLinks.length > 0) {
|
| 123 |
+
torrent.magnetLink = magnetLinks[0]; // β
Assign first valid magnet link
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
+
}));
|
| 127 |
+
|
| 128 |
+
// β
Ensure only torrents with magnet links are returned
|
| 129 |
+
const validTorrents = torrents.filter(torrent => torrent.magnetLink);
|
| 130 |
+
|
| 131 |
+
console.log(`β
Found ${validTorrents.length} torrents with magnet links.`);
|
| 132 |
+
return validTorrents;
|
| 133 |
+
|
| 134 |
+
} catch (error) {
|
| 135 |
+
console.error('β Error fetching from Torrentz9:', error);
|
| 136 |
+
return [];
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
export { fetchRSSFeeds };
|