Update rarbg2.js
Browse files1234567891011121314151617181920214 unmodified lines2627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118import 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 {const response = await fetch(searchUrl, {headers: {'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',});if (!response.ok) {throw new Error(`Failed to fetch torrent page: ${response.status}`);}const pageHtml = await response.text();const magnetMatch = pageHtml.match(/href="(magnet:\?xt=urn:btih:[^"]+)"/);return magnetMatch ? magnetMatch[1] : null;} catch (error) {console.error('โ Error extracting magnet link:', error);return null;}}// 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);if (!sizeMatch) return 0;const [, value, unit] = sizeMatch;const multiplier = unit.toUpperCase() === 'TB' ? 1024 ** 4 :unit.toUpperCase() === 'GB' ? 1024 ** 3 :1024 ** 2;return 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 };1234567891011121314151617184 unmodified lines2324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117import fetch from 'node-fetch';import { XMLParser } from 'fast-xml-parser';import * as cheerio from 'cheerio';// Function to fetch RSS feeds and search torrents on RARBGasync function fetchRSSFeeds(searchQuery) {const searchUrl = `https://en.rarbg.gg/search/?search=${encodeURIComponent(searchQuery)}&category[]=movies`;console.log('\n๐ Fetching RARBG search results for:', searchQuery);try {const response = await fetch(searchUrl, {headers: {'accept': '*/*','accept-language': 'en-US,en;q=0.9',},method: 'GET',});4 unmodified linesconst pageHtml = await response.text();const $ = cheerio.load(pageHtml);const torrentLinks = $('a[href^="/torrent/"]').toArray(); // Convert to real arrayif (torrentLinks.length === 0) {console.log('โ No torrents found for:', searchQuery);return [];}console.log(`Found ${torrentLinks.length} torrent links`);// Process torrents sequentially to avoid overwhelming requestsconst torrent_results = [];for (const link of torrentLinks) {const href = $(link).attr('href');const id = href.split('/')[2];const target = $(link).attr('title') || $(link).text();const ddhref = `https://en.rarbg.gg/download.php?id=${id}&f=${target}-[rarbg.com].torrent`;const torrentPageUrl = `https://en.rarbg.gg${href}`;const magnetLink = await fetchMagnetLink(torrentPageUrl); // Sequential requestif (!magnetLink) continue;const sizeMatch = $(link).parent().next().text().match(/Size:\s*([\d.]+)\s*(MB|GB|TB)/i);const size = sizeMatch ? `${sizeMatch[1]} ${sizeMatch[2]}` : 'Unknown';let sizeInBytes = 0;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;}const infoHashMatch = magnetLink.match(/urn:btih:([A-Za-z0-9]+)/i);const infoHash = infoHashMatch ? infoHashMatch[1] : null;let websiteTitle = target.split(' - ')[0];torrent_results.push({magnetLink,websiteTitle,quality: extractQuality(target),size,source: 'RARBG',infoHash,mainFileSize: sizeInBytes,categories: ['Movies'],});}// Sort results based on quality and sizetorrent_results.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.mainFileSize - a.mainFileSize : qualityDiff;});console.log(`โ Processed ${torrent_results.length} valid streams`);return torrent_results;} catch (error) {console.error('โ Error fetching from RARBG:', error);return [];}}// Function to fetch the magnet link from a RARBG torrent page (Sequential)async function fetchMagnetLink(pageUrl) {try {console.log(`๐ Fetching magnet link from: ${pageUrl}`);const response = await fetch(pageUrl);if (!response.ok) {throw new Error(`Failed to fetch RARBG page: ${response.status}`);}const pageHtml = await response.text();const magnetMatch = pageHtml.match(/href="(magnet:\?xt=urn:btih:[^"]+)"/);return magnetMatch ? magnetMatch[1] : null;} catch (error) {console.error('โ Error fetching magnet link:', error);return null;}}// Function to extract quality from titlefunction extractQuality(title) {if (!title) return '';let qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd|hdr|dv|blu[- ]?ray|webrip|brrip|dvdrip|hdtv)\b/i);return qualityMatch ? qualityMatch[1].toLowerCase().replace(/[- ]/g, '') : '';}export { fetchRSSFeeds };
rarbg2.js+68-69