mikmc5 commited on
Commit
f265ebb
·
verified ·
1 Parent(s): 64b682c

Update bt4g.js

Browse files
Files changed (1) hide show
  1. bt4g.js +101 -99
bt4g.js CHANGED
@@ -10,115 +10,117 @@ const parser = new XMLParser({
10
  trimValues: true,
11
  });
12
 
13
- async function fetchPageResults(searchQuery, page) {
14
- const url = `https://stremio-rev-proxy.h-y.workers.dev/https://bt4gprx.com/search?q=${encodeURIComponent(searchQuery)}&page=${page}&rss`;
15
- console.log(`🔄 Fetching RSS feed for: ${searchQuery} (Page: ${page})`);
 
 
16
 
17
  try {
18
- const response = await Promise.race([
19
- fetch(url, {
20
- headers: {
21
- 'accept': '*/*',
22
- 'accept-language': 'en-US,en;q=0.9',
23
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
24
- },
25
- method: 'GET',
26
- }),
27
- timeout(5000),
28
- ]);
29
-
30
- if (!response || !response.ok) {
31
- throw new Error(`Failed to fetch from BT4G: ${response?.status || 'timeout'}`);
32
  }
33
 
34
- const rssData = await response.text();
35
- console.log("Raw RSS data from BT4G:", rssData);
36
-
37
- try {
38
- const parsedData = parser.parse(rssData);
39
- const items = parsedData?.rss?.channel?.item || [];
40
- console.log(`Found ${items.length} items on page ${page}`);
41
-
42
- const torrentResults = await Promise.all(
43
- items.map(async (item) => {
44
- if (!item.title || !item.description) return null;
45
-
46
- const magnetLink = item.link;
47
- if (!magnetLink) return null;
48
-
49
- const sizeMatch = item.description.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 =
56
- unit.toUpperCase() === 'TB'
57
- ? 1024 * 1024 * 1024 * 1024
58
- : unit.toUpperCase() === 'GB'
59
- ? 1024 * 1024 * 1024
60
- : 1024 * 1024;
61
- sizeInBytes = parseFloat(value) * multiplier;
62
- }
63
-
64
- const infoHashMatch = magnetLink.match(/urn:btih:([A-Za-z0-9]+)/i);
65
- const infoHash = infoHashMatch ? infoHashMatch[1] : null;
66
-
67
- let websiteTitle = item.title;
68
- if (websiteTitle.includes(' - ')) {
69
- websiteTitle = websiteTitle.split(' - ')[0];
70
- }
71
-
72
- return {
73
- magnetLink,
74
- websiteTitle,
75
- quality: extractQuality(item.title),
76
- size,
77
- source: 'BT4G',
78
- infoHash,
79
- mainFileSize: sizeInBytes,
80
- pubDate: item.pubDate,
81
- categories: Array.isArray(item.category) ? item.category : [item.category],
82
- };
83
- })
84
- );
85
-
86
- return torrentResults.filter(Boolean); // Filter out null values
87
- } catch (err) {
88
- console.error('❌ Error parsing RSS data:', err);
89
- return [];
90
  }
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  } catch (error) {
93
- console.error(`❌ Error fetching from BT4G (Page ${page}):`, error);
94
  return [];
95
  }
96
  }
97
- // Function to fetch RSS feeds for both pages (1 and 2)
98
- async function fetchRSSFeeds(searchQuery) {
99
- const allResults = [];
100
-
101
- // Fetch results for page 1
102
- const page1Results = await fetchPageResults(searchQuery, 1);
103
- allResults.push(...page1Results);
104
-
105
- // Fetch results for page 2
106
- const page2Results = await fetchPageResults(searchQuery, 2);
107
- allResults.push(...page2Results);
108
 
109
- console.log(`✅ Processed ${allResults.length} valid streams from pages 1 and 2`);
110
-
111
- // Sort by quality and size
112
- allResults.sort((a, b) => {
113
- const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };
114
- const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
115
- if (qualityDiff === 0) {
116
- return b.mainFileSize - a.mainFileSize;
117
- }
118
- return qualityDiff;
119
- });
120
-
121
- return allResults;
122
- }
123
 
124
  export { fetchRSSFeeds };
 
10
  trimValues: true,
11
  });
12
 
13
+ async function fetchRSSFeeds(searchQuery) {
14
+ const page1Url = `https://stremio-rev-proxy.h-y.workers.dev/https://bt4gprx.com/search?q=${encodeURIComponent(searchQuery)}&page=1&orderby=size&page=rss`;
15
+ const page2Url = `https://bt4gprx.com/search?q=${encodeURIComponent(searchQuery)}&page=2&orderby=size&page=rss`; // Direct URL for page 2
16
+
17
+ console.log('\n🔄 Fetching RSS feed for:', searchQuery);
18
 
19
  try {
20
+ // Fetch page 1 with proxy
21
+ const page1Response = await fetch(page1Url, {
22
+ headers: {
23
+ 'accept': '*/*',
24
+ 'accept-language': 'en-US,en;q=0.9',
25
+ },
26
+ method: 'GET',
27
+ });
28
+ if (!page1Response.ok) {
29
+ throw new Error(`Failed to fetch page 1: ${page1Response.status}`);
 
 
 
 
30
  }
31
 
32
+ const page1RssData = await page1Response.text();
33
+ const parsedPage1Data = await parser.parseStringPromise(page1RssData);
34
+ const page1Items = parsedPage1Data?.rss?.channel?.item || [];
35
+
36
+ console.log(`Found ${page1Items.length} items on page 1`);
37
+
38
+ // Fetch page 2 without proxy
39
+ const page2Response = await fetch(page2Url, {
40
+ headers: {
41
+ 'accept': '*/*',
42
+ 'accept-language': 'en-US,en;q=0.9',
43
+ },
44
+ method: 'GET',
45
+ });
46
+ if (!page2Response.ok) {
47
+ throw new Error(`Failed to fetch page 2: ${page2Response.status}`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  }
49
 
50
+ const page2RssData = await page2Response.text();
51
+ const parsedPage2Data = await parser.parseStringPromise(page2RssData);
52
+ const page2Items = parsedPage2Data?.rss?.channel?.item || [];
53
+
54
+ console.log(`Found ${page2Items.length} items on page 2`);
55
+
56
+ // Combine items from both pages
57
+ const allItems = [...page1Items, ...page2Items];
58
+
59
+ // Map each item to the desired structure
60
+ const torrentResults = await Promise.all(
61
+ allItems.map(async (item) => {
62
+ if (!item.title || !item.description) return null;
63
+
64
+ const magnetLink = item.link; // Use the "link" as Magnet URI
65
+ if (!magnetLink) return null;
66
+
67
+ const sizeMatch = item.description.match(/Size:\s*([\d.]+)\s*(MB|GB|TB)/i);
68
+ const size = sizeMatch ? `${sizeMatch[1]} ${sizeMatch[2]}` : 'Unknown';
69
+
70
+ let sizeInBytes = 0;
71
+ if (sizeMatch) {
72
+ const [, value, unit] = sizeMatch;
73
+ const multiplier =
74
+ unit.toUpperCase() === 'TB'
75
+ ? 1024 * 1024 * 1024 * 1024
76
+ : unit.toUpperCase() === 'GB'
77
+ ? 1024 * 1024 * 1024
78
+ : 1024 * 1024;
79
+ sizeInBytes = parseFloat(value) * multiplier;
80
+ }
81
+
82
+ const infoHashMatch = magnetLink.match(/urn:btih:([A-Za-z0-9]+)/i);
83
+ const infoHash = infoHashMatch ? infoHashMatch[1] : null;
84
+
85
+ let websiteTitle = item.title;
86
+ if (websiteTitle.includes(' - ')) {
87
+ websiteTitle = websiteTitle.split(' - ')[0];
88
+ }
89
+
90
+ return {
91
+ magnetLink,
92
+ websiteTitle,
93
+ quality: extractQuality(item.title),
94
+ size,
95
+ source: 'BT4G',
96
+ infoHash,
97
+ mainFileSize: sizeInBytes,
98
+ pubDate: item.pubDate,
99
+ categories: Array.isArray(item.category) ? item.category : [item.category],
100
+ };
101
+ })
102
+ );
103
+
104
+ const validStreams = torrentResults.filter(Boolean);
105
+
106
+ // Sort streams by quality and size (with higher quality and larger size first)
107
+ validStreams.sort((a, b) => {
108
+ const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };
109
+ const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
110
+ if (qualityDiff === 0) {
111
+ return b.mainFileSize - a.mainFileSize;
112
+ }
113
+ return qualityDiff;
114
+ });
115
+
116
+ console.log(✅ Processed ${validStreams.length} valid streams);
117
+
118
+ return validStreams;
119
  } catch (error) {
120
+ console.error('❌ Error fetching from BT4G:', error);
121
  return [];
122
  }
123
  }
 
 
 
 
 
 
 
 
 
 
 
124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
  export { fetchRSSFeeds };