mikmc5 commited on
Commit
4dda0b3
·
verified ·
1 Parent(s): cea872d

Update bt4g.js

Browse files
Files changed (1) hide show
  1. bt4g.js +63 -66
bt4g.js CHANGED
@@ -1,59 +1,50 @@
1
- import xml2js from 'xml2js';
2
 
3
- const parser = new xml2js.Parser({
4
- explicitArray: false,
5
- ignoreAttrs: true
6
- });
7
-
8
- // This function extracts the quality from the title (e.g., 2160p, 1080p, etc.)
9
- function extractQuality(title) {
10
- if (!title) return '';
11
- const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);
12
- return qualityMatch ? qualityMatch[1].toLowerCase() : '';
13
- }
14
-
15
- // Timeout helper function
16
- const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
17
 
18
- // Function to fetch and parse the RSS feed for a given page
19
- async function fetchPageResults(searchQuery, page) {
20
- const url = `https://bt4gprx.com/search?q=${encodeURIComponent(searchQuery)}&page=${page}&rss`;
21
- console.log(`🔄 Fetching RSS feed for: ${searchQuery} (Page: ${page})`);
22
 
23
  try {
24
- const response = await Promise.race([
25
- fetch(url, {
26
- headers: {
27
- 'accept': '*/*',
28
- 'accept-language': 'en-US,en;q=0.9',
29
- },
30
- method: 'GET',
31
- }),
32
- timeout(5000), // Timeout after 5 seconds
33
- ]);
34
-
35
- if (!response || !response.ok) {
36
- throw new Error(`Failed to fetch from BT4G: ${response?.status || 'timeout'}`);
37
  }
38
 
39
  const rssData = await response.text();
40
 
41
  // Parsing the RSS XML response
42
- const parsedData = await parser.parseStringPromise(rssData);
43
- const items = parsedData?.rss?.channel?.item || [];
 
 
 
44
 
45
- console.log(`Found ${items.length} items on page ${page}`);
46
 
47
- const torrentResults = await Promise.all(
48
- items.map(async (item) => {
 
49
  if (!item.title || !item.description) return null;
50
 
 
51
  const magnetLink = item.link;
52
  if (!magnetLink) return null;
53
 
 
54
  const sizeMatch = item.description.match(/Size:\s*([\d.]+)\s*(MB|GB|TB)/i);
55
  const size = sizeMatch ? `${sizeMatch[1]} ${sizeMatch[2]}` : 'Unknown';
56
 
 
57
  let sizeInBytes = 0;
58
  if (sizeMatch) {
59
  const [, value, unit] = sizeMatch;
@@ -66,12 +57,14 @@ async function fetchPageResults(searchQuery, page) {
66
  sizeInBytes = parseFloat(value) * multiplier;
67
  }
68
 
 
69
  const infoHashMatch = magnetLink.match(/urn:btih:([A-Za-z0-9]+)/i);
70
  const infoHash = infoHashMatch ? infoHashMatch[1] : null;
71
 
 
72
  let websiteTitle = item.title;
73
  if (websiteTitle.includes(' - ')) {
74
- websiteTitle = websiteTitle.split(' - ')[0];
75
  }
76
 
77
  return {
@@ -79,7 +72,7 @@ async function fetchPageResults(searchQuery, page) {
79
  websiteTitle,
80
  quality: extractQuality(item.title),
81
  size,
82
- source: 'BT4G',
83
  infoHash,
84
  mainFileSize: sizeInBytes,
85
  pubDate: item.pubDate,
@@ -88,38 +81,42 @@ async function fetchPageResults(searchQuery, page) {
88
  })
89
  );
90
 
91
- return torrentResults.filter(Boolean); // Filter out null values
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  } catch (error) {
93
- console.error(`❌ Error fetching from BT4G (Page ${page}):`, error);
94
  return [];
95
  }
96
  }
97
 
98
- // Function to fetch RSS feeds for both pages (1 and 2)
99
- async function fetchRSSFeeds(searchQuery) {
100
- const allResults = [];
101
-
102
- // Fetch results for page 1
103
- const page1Results = await fetchPageResults(searchQuery, 1);
104
- allResults.push(...page1Results);
105
-
106
- // Fetch results for page 2
107
- const page2Results = await fetchPageResults(searchQuery, 2);
108
- allResults.push(...page2Results);
109
-
110
- console.log(`✅ Processed ${allResults.length} valid streams from pages 1 and 2`);
111
-
112
- // Sort by quality and size
113
- allResults.sort((a, b) => {
114
- const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };
115
- const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
116
- if (qualityDiff === 0) {
117
- return b.mainFileSize - a.mainFileSize;
118
- }
119
- return qualityDiff;
120
- });
121
-
122
- return allResults;
123
  }
124
 
125
  export { fetchRSSFeeds };
 
1
+ import { XMLParser } from 'fast-xml-parser';
2
 
3
+ async function fetchRSSFeeds(searchQuery) {
4
+ // The new URL you provided with encoding
5
+ const url = `https://bt4gprx.com/search?q=${encodeURIComponent(searchQuery)}&page=rss`;
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ console.log('\n🔄 Fetching RSS feed for:', searchQuery);
 
 
 
8
 
9
  try {
10
+ // Fetching the RSS data
11
+ const response = await fetch(url, {
12
+ headers: {
13
+ 'accept': '*/*',
14
+ 'accept-language': 'en-US,en;q=0.9',
15
+ },
16
+ method: 'GET',
17
+ });
18
+
19
+ if (!response.ok) {
20
+ throw new Error(`Failed to fetch from BT4G: ${response.status}`);
 
 
21
  }
22
 
23
  const rssData = await response.text();
24
 
25
  // Parsing the RSS XML response
26
+ const parser = new XMLParser();
27
+ let jObj = parser.parse(rssData);
28
+
29
+ // Ensure the structure is correct and check for items
30
+ const items = jObj?.rss?.channel?.item || [];
31
 
32
+ console.log(`Found ${items.length} items`);
33
 
34
+ // Process each item in the feed
35
+ const torrent_results = await Promise.all(
36
+ items.map((item) => {
37
  if (!item.title || !item.description) return null;
38
 
39
+ // Extract magnet link from the "link" property
40
  const magnetLink = item.link;
41
  if (!magnetLink) return null;
42
 
43
+ // Extract size from the description (regex used for size match)
44
  const sizeMatch = item.description.match(/Size:\s*([\d.]+)\s*(MB|GB|TB)/i);
45
  const size = sizeMatch ? `${sizeMatch[1]} ${sizeMatch[2]}` : 'Unknown';
46
 
47
+ // Convert size to bytes for sorting
48
  let sizeInBytes = 0;
49
  if (sizeMatch) {
50
  const [, value, unit] = sizeMatch;
 
57
  sizeInBytes = parseFloat(value) * multiplier;
58
  }
59
 
60
+ // Extract info hash from the magnet link
61
  const infoHashMatch = magnetLink.match(/urn:btih:([A-Za-z0-9]+)/i);
62
  const infoHash = infoHashMatch ? infoHashMatch[1] : null;
63
 
64
+ // Create the website title by splitting at " - " if present
65
  let websiteTitle = item.title;
66
  if (websiteTitle.includes(' - ')) {
67
+ websiteTitle = websiteTitle.split(' - ')[0]; // Take only the first part of the title
68
  }
69
 
70
  return {
 
72
  websiteTitle,
73
  quality: extractQuality(item.title),
74
  size,
75
+ source: 'BT4G', // Set the source as BT4G
76
  infoHash,
77
  mainFileSize: sizeInBytes,
78
  pubDate: item.pubDate,
 
81
  })
82
  );
83
 
84
+ // Filter out any null values (e.g., items without valid magnet links)
85
+ const validStreams = torrent_results.filter(Boolean);
86
+
87
+ // Sort streams by quality and size (higher quality and larger size first)
88
+ validStreams.sort((a, b) => {
89
+ const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };
90
+ const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
91
+ if (qualityDiff === 0) {
92
+ return b.mainFileSize - a.mainFileSize;
93
+ }
94
+ return qualityDiff;
95
+ });
96
+
97
+ console.log(`✅ Processed ${validStreams.length} valid streams`);
98
+
99
+ if (validStreams.length > 0) {
100
+ console.log('Sample stream:', {
101
+ title: validStreams[0].websiteTitle,
102
+ quality: validStreams[0].quality,
103
+ size: validStreams[0].size,
104
+ source: validStreams[0].source,
105
+ });
106
+ }
107
+
108
+ return validStreams;
109
  } catch (error) {
110
+ console.error('❌ Error fetching from BT4G:', error);
111
  return [];
112
  }
113
  }
114
 
115
+ // Helper function to extract quality (e.g., 2160p, 1080p, etc.) from title
116
+ function extractQuality(title) {
117
+ if (!title) return '';
118
+ const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);
119
+ return qualityMatch ? qualityMatch[1].toLowerCase() : '';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  }
121
 
122
  export { fetchRSSFeeds };