Update rarbg2.js
Browse files
rarbg2.js
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
import fetch from 'node-fetch';
|
| 2 |
import * as cheerio from 'cheerio';
|
| 3 |
|
| 4 |
-
// Function to fetch
|
| 5 |
-
async function fetchRSSFeeds(searchQuery
|
| 6 |
console.log(`\n🔄 Fetching RARBG search results for: ${searchQuery}`);
|
| 7 |
-
|
| 8 |
-
// Remove year from the query to improve search results
|
| 9 |
-
searchQuery = searchQuery.replace(/\(\d{4}\)/, '').trim();
|
| 10 |
const encodedQuery = encodeURIComponent(searchQuery);
|
| 11 |
-
const searchUrl = `https://rarbg.
|
| 12 |
console.log(`Fetching: ${searchUrl}`);
|
| 13 |
|
| 14 |
try {
|
|
@@ -17,7 +15,7 @@ async function fetchRSSFeeds(searchQuery, type = 'movie') {
|
|
| 17 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0.4472.124 Safari/537.36',
|
| 18 |
'Accept': '*/*',
|
| 19 |
'Accept-Language': 'en-US,en;q=0.9',
|
| 20 |
-
'Referer': 'https://rarbg.
|
| 21 |
},
|
| 22 |
method: 'GET',
|
| 23 |
});
|
|
@@ -28,72 +26,58 @@ async function fetchRSSFeeds(searchQuery, type = 'movie') {
|
|
| 28 |
|
| 29 |
const pageHtml = await response.text();
|
| 30 |
const $ = cheerio.load(pageHtml);
|
| 31 |
-
const torrentResults =
|
| 32 |
|
| 33 |
-
$('tr.
|
| 34 |
-
const titleElement = $(element).find('td.
|
| 35 |
const websiteTitle = titleElement.text().trim();
|
| 36 |
const torrentPage = titleElement.attr('href');
|
| 37 |
-
const
|
| 38 |
-
const size = $(element).find('td.sizeCell').text().trim();
|
| 39 |
-
const sizeInBytes = parseSizeToBytes(size);
|
| 40 |
|
| 41 |
if (!torrentPage) return;
|
| 42 |
|
| 43 |
-
const torrentPageUrl = `https://rarbg.
|
| 44 |
-
const formattedTitle = formatTitle(websiteTitle);
|
| 45 |
const quality = extractQuality(websiteTitle);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
if (!torrentResults.has(key)) {
|
| 54 |
-
torrentResults.set(key, {
|
| 55 |
-
websiteTitle: formattedTitle,
|
| 56 |
-
torrentPageUrl,
|
| 57 |
-
category,
|
| 58 |
-
size,
|
| 59 |
-
sizeInBytes,
|
| 60 |
-
quality,
|
| 61 |
-
magnetLink,
|
| 62 |
-
source: 'RARBG',
|
| 63 |
-
});
|
| 64 |
-
}
|
| 65 |
-
}, delay);
|
| 66 |
});
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
return await fetchRSSFeeds(searchQuery.replace(/[^a-zA-Z0-9 ]/g, ''));
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
const sortedResults = Array.from(torrentResults.values()).sort((a, b) => {
|
| 74 |
const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };
|
| 75 |
const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
|
| 76 |
-
return qualityDiff === 0 ? b.
|
| 77 |
});
|
| 78 |
|
| 79 |
-
console.log(`✅ Processed ${
|
| 80 |
-
return
|
| 81 |
} catch (error) {
|
| 82 |
console.error('❌ Error fetching from RARBG:', error);
|
| 83 |
-
return [];
|
| 84 |
}
|
| 85 |
}
|
| 86 |
|
| 87 |
-
// Function to fetch
|
| 88 |
async function extractMagnetLink(torrentPageUrl) {
|
| 89 |
try {
|
| 90 |
await new Promise(resolve => setTimeout(resolve, Math.random() * 2000 + 1000)); // 1-3 sec delay
|
| 91 |
-
|
| 92 |
const response = await fetch(torrentPageUrl, {
|
| 93 |
headers: {
|
| 94 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0.4472.124 Safari/537.36',
|
| 95 |
'Accept': '*/*',
|
| 96 |
-
'Referer': 'https://rarbg.
|
| 97 |
},
|
| 98 |
method: 'GET',
|
| 99 |
});
|
|
@@ -111,6 +95,7 @@ async function extractMagnetLink(torrentPageUrl) {
|
|
| 111 |
}
|
| 112 |
}
|
| 113 |
|
|
|
|
| 114 |
function parseSizeToBytes(size) {
|
| 115 |
if (!size) return 0;
|
| 116 |
const sizeMatch = size.match(/([\d.]+)\s*(MB|GB|TB)/i);
|
|
@@ -123,14 +108,11 @@ function parseSizeToBytes(size) {
|
|
| 123 |
return parseFloat(value) * multiplier;
|
| 124 |
}
|
| 125 |
|
|
|
|
| 126 |
function extractQuality(title) {
|
| 127 |
if (!title) return '';
|
| 128 |
const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);
|
| 129 |
return qualityMatch ? qualityMatch[1].toLowerCase() : '';
|
| 130 |
}
|
| 131 |
|
| 132 |
-
function formatTitle(title) {
|
| 133 |
-
return title.replace(/\s+/g, ' ').trim();
|
| 134 |
-
}
|
| 135 |
-
|
| 136 |
export { fetchRSSFeeds };
|
|
|
|
| 1 |
import fetch from 'node-fetch';
|
| 2 |
import * as cheerio from 'cheerio';
|
| 3 |
|
| 4 |
+
// Function to fetch torrents from RARBG
|
| 5 |
+
async function fetchRSSFeeds(searchQuery) {
|
| 6 |
console.log(`\n🔄 Fetching RARBG search results for: ${searchQuery}`);
|
| 7 |
+
|
|
|
|
|
|
|
| 8 |
const encodedQuery = encodeURIComponent(searchQuery);
|
| 9 |
+
const searchUrl = `https://en.rarbg.gg/search/?search=${encodedQuery}&order=size&by=DESC`;
|
| 10 |
console.log(`Fetching: ${searchUrl}`);
|
| 11 |
|
| 12 |
try {
|
|
|
|
| 15 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0.4472.124 Safari/537.36',
|
| 16 |
'Accept': '*/*',
|
| 17 |
'Accept-Language': 'en-US,en;q=0.9',
|
| 18 |
+
'Referer': 'https://en.rarbg.gg/'
|
| 19 |
},
|
| 20 |
method: 'GET',
|
| 21 |
});
|
|
|
|
| 26 |
|
| 27 |
const pageHtml = await response.text();
|
| 28 |
const $ = cheerio.load(pageHtml);
|
| 29 |
+
const torrentResults = [];
|
| 30 |
|
| 31 |
+
$('tr.lista2').each((_, element) => {
|
| 32 |
+
const titleElement = $(element).find('td.lista a:first');
|
| 33 |
const websiteTitle = titleElement.text().trim();
|
| 34 |
const torrentPage = titleElement.attr('href');
|
| 35 |
+
const size = $(element).find('td.lista:nth-child(5)').text().trim();
|
|
|
|
|
|
|
| 36 |
|
| 37 |
if (!torrentPage) return;
|
| 38 |
|
| 39 |
+
const torrentPageUrl = `https://en.rarbg.gg${torrentPage}`;
|
|
|
|
| 40 |
const quality = extractQuality(websiteTitle);
|
| 41 |
+
const magnetLink = await extractMagnetLink(torrentPageUrl);
|
| 42 |
+
|
| 43 |
+
if (!magnetLink) {
|
| 44 |
+
console.log(`⚠️ Skipping ${websiteTitle} (No Magnet Link Found)`);
|
| 45 |
+
return;
|
| 46 |
+
}
|
| 47 |
|
| 48 |
+
torrentResults.push({
|
| 49 |
+
title: websiteTitle,
|
| 50 |
+
quality,
|
| 51 |
+
size,
|
| 52 |
+
magnetLink
|
| 53 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
});
|
| 55 |
|
| 56 |
+
// Sort results based on quality and size
|
| 57 |
+
torrentResults.sort((a, b) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };
|
| 59 |
const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
|
| 60 |
+
return qualityDiff === 0 ? parseSizeToBytes(b.size) - parseSizeToBytes(a.size) : qualityDiff;
|
| 61 |
});
|
| 62 |
|
| 63 |
+
console.log(`✅ Processed ${torrentResults.length} valid torrents`);
|
| 64 |
+
return { results: torrentResults };
|
| 65 |
} catch (error) {
|
| 66 |
console.error('❌ Error fetching from RARBG:', error);
|
| 67 |
+
return { results: [] };
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
| 71 |
+
// Function to fetch magnet link from a torrent page
|
| 72 |
async function extractMagnetLink(torrentPageUrl) {
|
| 73 |
try {
|
| 74 |
await new Promise(resolve => setTimeout(resolve, Math.random() * 2000 + 1000)); // 1-3 sec delay
|
| 75 |
+
|
| 76 |
const response = await fetch(torrentPageUrl, {
|
| 77 |
headers: {
|
| 78 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0.4472.124 Safari/537.36',
|
| 79 |
'Accept': '*/*',
|
| 80 |
+
'Referer': 'https://en.rarbg.gg/'
|
| 81 |
},
|
| 82 |
method: 'GET',
|
| 83 |
});
|
|
|
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
| 98 |
+
// Function to parse file size into bytes for sorting
|
| 99 |
function parseSizeToBytes(size) {
|
| 100 |
if (!size) return 0;
|
| 101 |
const sizeMatch = size.match(/([\d.]+)\s*(MB|GB|TB)/i);
|
|
|
|
| 108 |
return parseFloat(value) * multiplier;
|
| 109 |
}
|
| 110 |
|
| 111 |
+
// Function to extract quality from title
|
| 112 |
function extractQuality(title) {
|
| 113 |
if (!title) return '';
|
| 114 |
const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);
|
| 115 |
return qualityMatch ? qualityMatch[1].toLowerCase() : '';
|
| 116 |
}
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
export { fetchRSSFeeds };
|