hankchin commited on
Commit
ef7ffe1
Β·
verified Β·
1 Parent(s): 343a2f1

Update rarbg.js

Browse files
Files changed (1) hide show
  1. rarbg.js +57 -65
rarbg.js CHANGED
@@ -1,9 +1,11 @@
 
1
  import { XMLParser } from 'fast-xml-parser';
 
2
 
3
- // Single fetchRSSFeeds export for searching torrents on RARBG
4
  async function fetchRSSFeeds(searchQuery) {
5
  const searchUrl = `https://rarbg.to/torrents.php?search=${encodeURIComponent(searchQuery)}&category=movies`;
6
-
7
  console.log('\nπŸ”„ Fetching RARBG search results for:', searchQuery);
8
 
9
  try {
@@ -20,7 +22,9 @@ async function fetchRSSFeeds(searchQuery) {
20
  }
21
 
22
  const pageHtml = await response.text();
23
- const torrentLinks = $(pageHtml).find('a[href^="/torrent/"]'); // Finding torrent links on the page
 
 
24
 
25
  if (torrentLinks.length === 0) {
26
  console.log('❌ No torrents found for:', searchQuery);
@@ -29,75 +33,64 @@ async function fetchRSSFeeds(searchQuery) {
29
 
30
  console.log(`Found ${torrentLinks.length} torrent links`);
31
 
32
- const torrent_results = await Promise.all(
33
- torrentLinks.map(async (i, link) => {
34
- const href = $(link).attr('href');
35
- const id = href.split('/')[2]; // Extract torrent ID
36
- const target = $(link).attr('title') || $(link).html(); // Extract the title or text
37
- const ddhref = `http://rarbg.to/download.php?id=${id}&f=${target}-[rarbg.com].torrent`; // Direct download link
38
-
39
- const torrentPageUrl = `https://rarbg.to${href}`;
40
- const magnetLink = await fetchMagnetLink(torrentPageUrl); // Fetch magnet link
41
-
42
- if (!magnetLink) return null;
43
-
44
- const sizeMatch = $(link).parent().next().text().match(/Size:\s*([\d.]+)\s*(MB|GB|TB)/i); // Extract size info
45
- const size = sizeMatch ? `${sizeMatch[1]} ${sizeMatch[2]}` : 'Unknown';
46
-
47
- let sizeInBytes = 0;
48
- if (sizeMatch) {
49
- const [, value, unit] = sizeMatch;
50
- const multiplier =
51
- unit.toUpperCase() === 'TB'
52
- ? 1024 * 1024 * 1024 * 1024
53
- : unit.toUpperCase() === 'GB'
54
- ? 1024 * 1024 * 1024
55
- : 1024 * 1024;
56
- sizeInBytes = parseFloat(value) * multiplier;
57
- }
58
-
59
- const infoHashMatch = magnetLink.match(/urn:btih:([A-Za-z0-9]+)/i);
60
- const infoHash = infoHashMatch ? infoHashMatch[1] : null;
61
-
62
- let websiteTitle = target;
63
- if (websiteTitle.includes(' - ')) {
64
- websiteTitle = websiteTitle.split(' - ')[0]; // Take only the first part of the title
65
- }
66
-
67
- return {
68
- magnetLink,
69
- websiteTitle,
70
- quality: extractQuality(target),
71
- size,
72
- source: 'RARBG',
73
- infoHash,
74
- mainFileSize: sizeInBytes,
75
- categories: ['Movies'], // Assuming a default category, can be adjusted
76
- };
77
- })
78
- );
79
-
80
- const validStreams = torrent_results.filter(Boolean);
81
-
82
- validStreams.sort((a, b) => {
83
  const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };
84
  const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
85
- if (qualityDiff === 0) {
86
- return b.mainFileSize - a.mainFileSize;
87
- }
88
- return qualityDiff;
89
  });
90
 
91
- console.log(`βœ… Processed ${validStreams.length} valid streams`);
92
-
93
- return validStreams;
94
  } catch (error) {
95
  console.error('❌ Error fetching from RARBG:', error);
96
  return [];
97
  }
98
  }
99
 
100
- // Function to fetch the magnet link from a RARBG torrent page
101
  async function fetchMagnetLink(pageUrl) {
102
  try {
103
  console.log(`πŸ”„ Fetching magnet link from: ${pageUrl}`);
@@ -114,12 +107,11 @@ async function fetchMagnetLink(pageUrl) {
114
  }
115
  }
116
 
 
117
  function extractQuality(title) {
118
  if (!title) return '';
119
-
120
- // Match quality in title
121
  let qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd|hdr|dv|blu[- ]?ray|webrip|brrip|dvdrip|hdtv)\b/i);
122
  return qualityMatch ? qualityMatch[1].toLowerCase().replace(/[- ]/g, '') : '';
123
  }
124
 
125
- export { fetchRSSFeeds };
 
1
+ import fetch from 'node-fetch';
2
  import { XMLParser } from 'fast-xml-parser';
3
+ import * as cheerio from 'cheerio';
4
 
5
+ // Function to fetch RSS feeds and search torrents on RARBG
6
  async function fetchRSSFeeds(searchQuery) {
7
  const searchUrl = `https://rarbg.to/torrents.php?search=${encodeURIComponent(searchQuery)}&category=movies`;
8
+
9
  console.log('\nπŸ”„ Fetching RARBG search results for:', searchQuery);
10
 
11
  try {
 
22
  }
23
 
24
  const pageHtml = await response.text();
25
+ const $ = cheerio.load(pageHtml);
26
+
27
+ const torrentLinks = $('a[href^="/torrent/"]').toArray(); // Convert to real array
28
 
29
  if (torrentLinks.length === 0) {
30
  console.log('❌ No torrents found for:', searchQuery);
 
33
 
34
  console.log(`Found ${torrentLinks.length} torrent links`);
35
 
36
+ // Process torrents sequentially to avoid overwhelming requests
37
+ const torrent_results = [];
38
+ for (const link of torrentLinks) {
39
+ const href = $(link).attr('href');
40
+ const id = href.split('/')[2];
41
+ const target = $(link).attr('title') || $(link).text();
42
+ const ddhref = `http://rarbg.to/download.php?id=${id}&f=${target}-[rarbg.com].torrent`;
43
+
44
+ const torrentPageUrl = `https://rarbg.to${href}`;
45
+ const magnetLink = await fetchMagnetLink(torrentPageUrl); // Sequential request
46
+
47
+ if (!magnetLink) continue;
48
+
49
+ const sizeMatch = $(link).parent().next().text().match(/Size:\s*([\d.]+)\s*(MB|GB|TB)/i);
50
+ const size = sizeMatch ? `${sizeMatch[1]} ${sizeMatch[2]}` : 'Unknown';
51
+
52
+ let sizeInBytes = 0;
53
+ if (sizeMatch) {
54
+ const [, value, unit] = sizeMatch;
55
+ const multiplier = unit.toUpperCase() === 'TB' ? 1024 * 1024 * 1024 * 1024 :
56
+ unit.toUpperCase() === 'GB' ? 1024 * 1024 * 1024 :
57
+ 1024 * 1024;
58
+ sizeInBytes = parseFloat(value) * multiplier;
59
+ }
60
+
61
+ const infoHashMatch = magnetLink.match(/urn:btih:([A-Za-z0-9]+)/i);
62
+ const infoHash = infoHashMatch ? infoHashMatch[1] : null;
63
+
64
+ let websiteTitle = target.split(' - ')[0];
65
+
66
+ torrent_results.push({
67
+ magnetLink,
68
+ websiteTitle,
69
+ quality: extractQuality(target),
70
+ size,
71
+ source: 'RARBG',
72
+ infoHash,
73
+ mainFileSize: sizeInBytes,
74
+ categories: ['Movies'],
75
+ });
76
+ }
77
+
78
+ // Sort results based on quality and size
79
+ torrent_results.sort((a, b) => {
 
 
 
 
 
 
 
80
  const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };
81
  const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
82
+ return qualityDiff === 0 ? b.mainFileSize - a.mainFileSize : qualityDiff;
 
 
 
83
  });
84
 
85
+ console.log(`βœ… Processed ${torrent_results.length} valid streams`);
86
+ return torrent_results;
 
87
  } catch (error) {
88
  console.error('❌ Error fetching from RARBG:', error);
89
  return [];
90
  }
91
  }
92
 
93
+ // Function to fetch the magnet link from a RARBG torrent page (Sequential)
94
  async function fetchMagnetLink(pageUrl) {
95
  try {
96
  console.log(`πŸ”„ Fetching magnet link from: ${pageUrl}`);
 
107
  }
108
  }
109
 
110
+ // Function to extract quality from title
111
  function extractQuality(title) {
112
  if (!title) return '';
 
 
113
  let qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd|hdr|dv|blu[- ]?ray|webrip|brrip|dvdrip|hdtv)\b/i);
114
  return qualityMatch ? qualityMatch[1].toLowerCase().replace(/[- ]/g, '') : '';
115
  }
116
 
117
+ export { fetchRSSFeeds };