Update rarbg2.js
Browse files12345678910111213142 unmodified lines171819202122234 unmodified lines28293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989911 unmodified lines1111121131141151166 unmodified lines123124125126127128129130131132133134135136import fetch from 'node-fetch';import * as cheerio from 'cheerio';// Function to fetch RSS feeds and search torrents on RARBGasync function fetchRSSFeeds(searchQuery, type = 'movie') {console.log(`\n🔄 Fetching RARBG search results for: ${searchQuery}`);// Remove year from the query to improve search resultssearchQuery = searchQuery.replace(/\(\d{4}\)/, '').trim();const encodedQuery = encodeURIComponent(searchQuery);const searchUrl = `https://rarbg.torrentsbay.org/get-posts/keywords:${encodedQuery}:category:Movies:category:TV:ncategory:XXX:order:-s/`;console.log(`Fetching: ${searchUrl}`);try {2 unmodified lines'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0.4472.124 Safari/537.36','Accept': '*/*','Accept-Language': 'en-US,en;q=0.9','Referer': 'https://rarbg.torrentsbay.org/'},method: 'GET',});4 unmodified linesconst pageHtml = await response.text();const $ = cheerio.load(pageHtml);const torrentResults = new Map();$('tr.list-entry').each((_, element) => {const titleElement = $(element).find('td.cellName a:first');const websiteTitle = titleElement.text().trim();const torrentPage = titleElement.attr('href');const category = $(element).find('td.hideCell a').text().trim();const size = $(element).find('td.sizeCell').text().trim();const sizeInBytes = parseSizeToBytes(size);if (!torrentPage) return;const torrentPageUrl = `https://rarbg.torrentsbay.org${torrentPage}`;const formattedTitle = formatTitle(websiteTitle);const quality = extractQuality(websiteTitle);// Introduce a small delay to prevent being blockedconst delay = Math.random() * 2000 + 1000; // 1-3 seconds delaysetTimeout(async () => {const magnetLink = await extractMagnetLink(torrentPageUrl);const key = `${formattedTitle}-${sizeInBytes}`;if (!torrentResults.has(key)) {torrentResults.set(key, {websiteTitle: formattedTitle,torrentPageUrl,category,size,sizeInBytes,quality,magnetLink,source: 'RARBG',});}}, delay);});if (torrentResults.size === 0) {console.log('⚠️ No results found. Trying alternative query without special characters.');return await fetchRSSFeeds(searchQuery.replace(/[^a-zA-Z0-9 ]/g, ''));}const sortedResults = Array.from(torrentResults.values()).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);return qualityDiff === 0 ? b.sizeInBytes - a.sizeInBytes : qualityDiff;});console.log(`✅ Processed ${sortedResults.length} valid torrents`);return sortedResults;} catch (error) {console.error('❌ Error fetching from RARBG:', error);return [];}}// Function to fetch the magnet link from the torrent pageasync function extractMagnetLink(torrentPageUrl) {try {await new Promise(resolve => setTimeout(resolve, Math.random() * 2000 + 1000)); // 1-3 sec delayconst response = await fetch(torrentPageUrl, {headers: {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0.4472.124 Safari/537.36','Accept': '*/*','Referer': 'https://rarbg.torrentsbay.org/'},method: 'GET',});11 unmodified lines}}function parseSizeToBytes(size) {if (!size) return 0;const sizeMatch = size.match(/([\d.]+)\s*(MB|GB|TB)/i);6 unmodified linesreturn parseFloat(value) * multiplier;}function extractQuality(title) {if (!title) return '';const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);return qualityMatch ? qualityMatch[1].toLowerCase() : '';}function formatTitle(title) {return title.replace(/\s+/g, ' ').trim();}export { fetchRSSFeeds };1234567891011122 unmodified lines151617181920214 unmodified lines2627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828311 unmodified lines95969798991001016 unmodified lines108109110111112113114115116117118import fetch from 'node-fetch';import * as cheerio from 'cheerio';// Function to fetch torrents from RARBGasync function fetchRSSFeeds(searchQuery) {console.log(`\n🔄 Fetching RARBG search results for: ${searchQuery}`);const encodedQuery = encodeURIComponent(searchQuery);const searchUrl = `https://en.rarbg.gg/search/?search=${encodedQuery}&order=size&by=DESC`;console.log(`Fetching: ${searchUrl}`);try {2 unmodified lines'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0.4472.124 Safari/537.36','Accept': '*/*','Accept-Language': 'en-US,en;q=0.9','Referer': 'https://en.rarbg.gg/'},method: 'GET',});4 unmodified linesconst pageHtml = await response.text();const $ = cheerio.load(pageHtml);const torrentResults = [];$('tr.lista2').each((_, element) => {const titleElement = $(element).find('td.lista a:first');const websiteTitle = titleElement.text().trim();const torrentPage = titleElement.attr('href');const size = $(element).find('td.lista:nth-child(5)').text().trim();if (!torrentPage) return;const torrentPageUrl = `https://en.rarbg.gg${torrentPage}`;const quality = extractQuality(websiteTitle);const magnetLink = await extractMagnetLink(torrentPageUrl);if (!magnetLink) {console.log(`⚠️ Skipping ${websiteTitle} (No Magnet Link Found)`);return;}torrentResults.push({title: websiteTitle,quality,size,magnetLink});});// Sort results based on quality and sizetorrentResults.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);return qualityDiff === 0 ? parseSizeToBytes(b.size) - parseSizeToBytes(a.size) : qualityDiff;});console.log(`✅ Processed ${torrentResults.length} valid torrents`);return { results: torrentResults };} catch (error) {console.error('❌ Error fetching from RARBG:', error);return { results: [] };}}// Function to fetch magnet link from a torrent pageasync function extractMagnetLink(torrentPageUrl) {try {await new Promise(resolve => setTimeout(resolve, Math.random() * 2000 + 1000)); // 1-3 sec delayconst response = await fetch(torrentPageUrl, {headers: {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0.4472.124 Safari/537.36','Accept': '*/*','Referer': 'https://en.rarbg.gg/'},method: 'GET',});11 unmodified lines}}// Function to parse file size into bytes for sortingfunction parseSizeToBytes(size) {if (!size) return 0;const sizeMatch = size.match(/([\d.]+)\s*(MB|GB|TB)/i);6 unmodified linesreturn parseFloat(value) * multiplier;}// Function to extract quality from titlefunction extractQuality(title) {if (!title) return '';const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);return qualityMatch ? qualityMatch[1].toLowerCase() : '';}export { fetchRSSFeeds };
rarbg2.js+33-51