Download tp2.js from mikmc5/sow: direct link, hf CLI and curl.
- Browser
- Download file 3.88 kB
-
https://huggingface.co/spaces/mikmc5/sow/resolve/main/tp2.js
- Command line
-
hf download hf://spaces/mikmc5/sow/tp2.js
-
curl -L -o tp2.js https://huggingface.co/spaces/mikmc5/sow/resolve/main/tp2.js
3.88 kB
| import fetch from 'node-fetch'; | |
| import xml2js from 'xml2js'; | |
| const parser = new xml2js.Parser({ explicitArray: false, ignoreAttrs: false }); | |
| function extractQuality(title) { | |
| if (!title) return ''; | |
| const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i); | |
| return qualityMatch ? qualityMatch[1].toLowerCase() : ''; | |
| } | |
| function formatSize(bytes) { | |
| const sizes = ['B', 'KB', 'MB', 'GB', 'TB']; | |
| if (bytes === 0) return '0 B'; | |
| const i = Math.floor(Math.log(bytes) / Math.log(1024)); | |
| return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${sizes[i]}`; | |
| } | |
| async function fetchRSSFeeds(searchQuery) { | |
| console.log(`🔄 Fetching torrents for: ${searchQuery}`); | |
| const rssUrl = `https://torrentdownloads.proxyninja.org/rss.xml?type=search&search=${encodeURIComponent(searchQuery)}`; | |
| try { | |
| const response = await fetch(rssUrl, { | |
| headers: { 'User-Agent': 'Mozilla/5.0' } | |
| }); | |
| if (!response.ok) throw new Error(`Failed to fetch RSS: ${response.statusText}`); | |
| const rssData = await response.text(); | |
| const result = await parser.parseStringPromise(rssData); | |
| if (!result?.rss?.channel?.item) { | |
| console.log('⚠️ No torrents found.'); | |
| return []; | |
| } | |
| const items = Array.isArray(result.rss.channel.item) | |
| ? result.rss.channel.item | |
| : [result.rss.channel.item]; | |
| console.log(`✅ Found ${items.length} torrents`); | |
| const torrentResults = await Promise.all( | |
| items.map(async (item) => { | |
| if (!item.info_hash) return null; | |
| // Create a magnet link directly from the info hash | |
| const infoHash = item.info_hash; | |
| const encodedTitle = encodeURIComponent(item.title); | |
| const magnetLink = `magnet:?xt=urn:btih:${infoHash}&dn=${encodedTitle}`; | |
| let websiteTitle = item.title; | |
| if (websiteTitle.includes(' - ')) { | |
| websiteTitle = websiteTitle.split(' - ')[0]; // Take only the first part of the title | |
| } | |
| // Size is directly provided in bytes | |
| const sizeInBytes = parseInt(item.size, 10) || 0; | |
| const size = formatSize(sizeInBytes); | |
| return { | |
| magnetLink, | |
| websiteTitle, | |
| quality: extractQuality(item.title), | |
| size, | |
| source: 'torrentdownloads', // Keeping the original source name as in your code | |
| infoHash, | |
| mainFileSize: sizeInBytes, | |
| pubDate: item.pubDate, | |
| categories: item.category ? [item.category] : [] | |
| }; | |
| }) | |
| ); | |
| // Filter out any null values | |
| const validStreams = torrentResults.filter(Boolean); | |
| // Sort streams by quality and size | |
| validStreams.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(`✅ Processed ${validStreams.length} valid streams`); | |
| if (validStreams.length > 0) { | |
| console.log('Sample stream:', { | |
| title: validStreams[0].websiteTitle, | |
| quality: validStreams[0].quality, | |
| size: validStreams[0].size, | |
| source: validStreams[0].source | |
| }); | |
| } | |
| return validStreams; | |
| } catch (error) { | |
| console.error('❌ Error fetching RSS:', error); | |
| return []; | |
| } | |
| } | |
| export { fetchRSSFeeds }; |