hankchin commited on
Commit
0743ff0
ยท
verified ยท
1 Parent(s): 8ff3e38

Update rarbg2.js

Browse files
Files changed (1) hide show
  1. rarbg2.js +68 -69
rarbg2.js CHANGED
@@ -1,21 +1,18 @@
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 {
13
  const response = await fetch(searchUrl, {
14
  headers: {
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,93 +23,95 @@ async function fetchRSSFeeds(searchQuery) {
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
- });
84
-
85
  if (!response.ok) {
86
- throw new Error(`Failed to fetch torrent page: ${response.status}`);
87
  }
88
-
89
  const pageHtml = await response.text();
90
  const magnetMatch = pageHtml.match(/href="(magnet:\?xt=urn:btih:[^"]+)"/);
91
  return magnetMatch ? magnetMatch[1] : null;
92
  } catch (error) {
93
- console.error('โŒ Error extracting magnet link:', error);
94
  return null;
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);
102
- if (!sizeMatch) return 0;
103
-
104
- const [, value, unit] = sizeMatch;
105
- const multiplier = unit.toUpperCase() === 'TB' ? 1024 ** 4 :
106
- unit.toUpperCase() === 'GB' ? 1024 ** 3 :
107
- 1024 ** 2;
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 };
 
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://en.rarbg.gg/search/?search=${encodeURIComponent(searchQuery)}&category[]=movies`;
8
+
9
+ console.log('\n๐Ÿ”„ Fetching RARBG search results for:', searchQuery);
 
 
10
 
11
  try {
12
  const response = await fetch(searchUrl, {
13
  headers: {
14
+ 'accept': '*/*',
15
+ 'accept-language': 'en-US,en;q=0.9',
 
 
16
  },
17
  method: 'GET',
18
  });
 
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);
31
+ return [];
32
+ }
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 = `https://en.rarbg.gg/download.php?id=${id}&f=${target}-[rarbg.com].torrent`;
43
+
44
+ const torrentPageUrl = `https://en.rarbg.gg${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}`);
97
+ const response = await fetch(pageUrl);
 
 
 
 
 
 
 
 
 
98
  if (!response.ok) {
99
+ throw new Error(`Failed to fetch RARBG page: ${response.status}`);
100
  }
 
101
  const pageHtml = await response.text();
102
  const magnetMatch = pageHtml.match(/href="(magnet:\?xt=urn:btih:[^"]+)"/);
103
  return magnetMatch ? magnetMatch[1] : null;
104
  } catch (error) {
105
+ console.error('โŒ Error fetching magnet link:', error);
106
  return null;
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 };