Download torr9.js from mikmc5/bitsa: direct link, hf CLI and curl.
- Browser
- Download file 7.05 kB
-
https://huggingface.co/spaces/mikmc5/bitsa/resolve/main/torr9.js
- Command line
-
hf download hf://spaces/mikmc5/bitsa/torr9.js
-
curl -L -o torr9.js https://huggingface.co/spaces/mikmc5/bitsa/resolve/main/torr9.js
7.05 kB
| import fetch from 'node-fetch'; | |
| import { JSDOM } from 'jsdom'; | |
| // Function to extract video quality from the title | |
| function 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 URLs | |
| function extractHash(detailUrl) { | |
| const hashMatch = detailUrl.match(/\/([A-Fa-f0-9]{40})$/); | |
| return hashMatch ? hashMatch[1] : ''; | |
| } | |
| // Function to generate a direct magnet link from a hash | |
| function createMagnetLink(hash) { | |
| return `magnet:?xt=urn:btih:${hash}`; | |
| } | |
| // β Function to extract magnet links from torrentz9 detail pages | |
| async 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 data | |
| async 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; | |
| // Convert size to bytes for sorting | |
| let sizeInBytes = 0; | |
| const sizeMatch = size.match(/([\d.]+)\s*(MB|GB|TB)/i); | |
| if (sizeMatch) { | |
| const [, value, unit] = sizeMatch; | |
| const multiplier = | |
| unit.toUpperCase() === 'TB' | |
| ? 1024 * 1024 * 1024 * 1024 | |
| : unit.toUpperCase() === 'GB' | |
| ? 1024 * 1024 * 1024 | |
| : 1024 * 1024; | |
| sizeInBytes = parseFloat(value) * multiplier; | |
| } | |
| // Extract info hash from magnet link | |
| const infoHashMatch = magnetLink ? magnetLink.match(/urn:btih:([A-Za-z0-9]+)/i) : null; | |
| const infoHash = infoHashMatch ? infoHashMatch[1] : hash; | |
| if (magnetLink) { | |
| torrents.push({ | |
| magnetLink, | |
| websiteTitle: title, | |
| quality, | |
| size, | |
| source: 'Torrentz9', | |
| infoHash, | |
| mainFileSize: sizeInBytes, | |
| seeders, | |
| leechers, | |
| detailUrl | |
| }); | |
| } | |
| }); | |
| // β 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 | |
| // Update infoHash if we got a new magnet link | |
| const infoHashMatch = torrent.magnetLink.match(/urn:btih:([A-Za-z0-9]+)/i); | |
| if (infoHashMatch) { | |
| torrent.infoHash = infoHashMatch[1]; | |
| } | |
| } | |
| } | |
| })); | |
| // β Ensure only torrents with magnet links are returned | |
| const validTorrents = torrents.filter(torrent => torrent.magnetLink); | |
| // Sort torrents by quality and size (higher quality and larger size first) | |
| validTorrents.sort((a, b) => { | |
| const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 }; | |
| const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0); | |
| if (qualityDiff === 0) { | |
| return b.mainFileSize - a.mainFileSize; | |
| } | |
| return qualityDiff; | |
| }); | |
| console.log(`β Found ${validTorrents.length} torrents with magnet links.`); | |
| return validTorrents; | |
| } catch (error) { | |
| console.error('β Error fetching from Torrentz9:', error); | |
| return []; | |
| } | |
| } | |
| export { fetchRSSFeeds }; |