mikmc5 commited on
Commit
bfaa905
·
verified ·
1 Parent(s): d9f6d33

Update bt4g.js

Browse files
Files changed (1) hide show
  1. bt4g.js +66 -59
bt4g.js CHANGED
@@ -1,6 +1,5 @@
1
  import xml2js from 'xml2js';
2
 
3
- // Initialize xml2js parser with necessary options
4
  const parser = new xml2js.Parser({
5
  explicitArray: false,
6
  ignoreAttrs: true
@@ -16,23 +15,7 @@ function extractQuality(title) {
16
  // Timeout helper function
17
  const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
18
 
19
- // Function to fetch and parse the RSS feed based on the search query
20
- async function fetchRSSFeeds(searchQuery) {
21
- const allResults = [];
22
-
23
- // Fetch page 1
24
- const page1Results = await fetchPageResults(searchQuery, 1);
25
- allResults.push(...page1Results);
26
-
27
- // Fetch page 2
28
- const page2Results = await fetchPageResults(searchQuery, 2);
29
- allResults.push(...page2Results);
30
-
31
- console.log(`✅ Processed ${allResults.length} results from first 2 pages`);
32
- return allResults;
33
- }
34
-
35
- // Helper function to fetch results from a specific page
36
  async function fetchPageResults(searchQuery, page) {
37
  const url = `https://bt4gprx.com/search?q=${encodeURIComponent(searchQuery)}&page=${page}&orderby=size`;
38
  console.log(`🔄 Fetching page ${page} for:`, searchQuery);
@@ -52,54 +35,78 @@ async function fetchPageResults(searchQuery, page) {
52
 
53
  const rssData = await response.text();
54
 
55
- // Parse the RSS data using xml2js
56
  const parsedData = await parser.parseStringPromise(rssData);
57
  const items = parsedData?.rss?.channel?.item || [];
 
58
  console.log(`Found ${items.length} items on page ${page}`);
59
 
60
- // Process items into the desired format
61
- return items.map((item) => {
62
- if (!item.title || !item.description) return null;
63
-
64
- const magnetLink = item.link;
65
- const sizeMatch = item.description.match(/Size:\s*([\d.]+)\s*(MB|GB|TB)/i);
66
- const size = sizeMatch ? `${sizeMatch[1]} ${sizeMatch[2]}` : 'Unknown';
67
-
68
- let sizeInBytes = 0;
69
- if (sizeMatch) {
70
- const [, value, unit] = sizeMatch;
71
- const multiplier = unit.toUpperCase() === 'TB'
72
- ? 1024 * 1024 * 1024 * 1024
73
- : unit.toUpperCase() === 'GB'
74
- ? 1024 * 1024 * 1024
75
- : 1024 * 1024;
76
- sizeInBytes = parseFloat(value) * multiplier;
77
- }
78
-
79
- const infoHashMatch = magnetLink.match(/urn:btih:([A-Za-z0-9]+)/i);
80
- const infoHash = infoHashMatch ? infoHashMatch[1] : null;
81
-
82
- let websiteTitle = item.title;
83
- if (websiteTitle.includes(' - ')) {
84
- websiteTitle = websiteTitle.split(' - ')[0];
85
- }
86
-
87
- return {
88
- magnetLink,
89
- websiteTitle,
90
- quality: extractQuality(item.title),
91
- size,
92
- source: 'BT4G',
93
- infoHash,
94
- mainFileSize: sizeInBytes,
95
- pubDate: item.pubDate,
96
- categories: Array.isArray(item.category) ? item.category : [item.category],
97
- };
98
- }).filter(Boolean);
 
 
 
 
 
 
 
99
  } catch (error) {
100
- console.error('❌ Error fetching from BT4G:', error);
101
  return [];
102
  }
103
  }
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  export { fetchRSSFeeds };
 
1
  import xml2js from 'xml2js';
2
 
 
3
  const parser = new xml2js.Parser({
4
  explicitArray: false,
5
  ignoreAttrs: true
 
15
  // Timeout helper function
16
  const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
17
 
18
+ // Function to fetch results from a specific page
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  async function fetchPageResults(searchQuery, page) {
20
  const url = `https://bt4gprx.com/search?q=${encodeURIComponent(searchQuery)}&page=${page}&orderby=size`;
21
  console.log(`🔄 Fetching page ${page} for:`, searchQuery);
 
35
 
36
  const rssData = await response.text();
37
 
38
+ // Parsing the RSS XML response
39
  const parsedData = await parser.parseStringPromise(rssData);
40
  const items = parsedData?.rss?.channel?.item || [];
41
+
42
  console.log(`Found ${items.length} items on page ${page}`);
43
 
44
+ // Map each item to the desired structure
45
+ const torrentResults = await Promise.all(
46
+ items.map(async (item) => {
47
+ if (!item.title || !item.description) return null;
48
+
49
+ const magnetLink = item.link;
50
+ if (!magnetLink) return null;
51
+
52
+ const sizeMatch = item.description.match(/Size:\s*([\d.]+)\s*(MB|GB|TB)/i);
53
+ const size = sizeMatch ? `${sizeMatch[1]} ${sizeMatch[2]}` : 'Unknown';
54
+
55
+ let sizeInBytes = 0;
56
+ if (sizeMatch) {
57
+ const [, value, unit] = sizeMatch;
58
+ const multiplier =
59
+ unit.toUpperCase() === 'TB'
60
+ ? 1024 * 1024 * 1024 * 1024
61
+ : unit.toUpperCase() === 'GB'
62
+ ? 1024 * 1024 * 1024
63
+ : 1024 * 1024;
64
+ sizeInBytes = parseFloat(value) * multiplier;
65
+ }
66
+
67
+ const infoHashMatch = magnetLink.match(/urn:btih:([A-Za-z0-9]+)/i);
68
+ const infoHash = infoHashMatch ? infoHashMatch[1] : null;
69
+
70
+ let websiteTitle = item.title;
71
+ if (websiteTitle.includes(' - ')) {
72
+ websiteTitle = websiteTitle.split(' - ')[0];
73
+ }
74
+
75
+ return {
76
+ magnetLink,
77
+ websiteTitle,
78
+ quality: extractQuality(item.title),
79
+ size,
80
+ source: 'BT4G',
81
+ infoHash,
82
+ mainFileSize: sizeInBytes,
83
+ pubDate: item.pubDate,
84
+ categories: Array.isArray(item.category) ? item.category : [item.category],
85
+ };
86
+ })
87
+ );
88
+
89
+ return torrentResults.filter(Boolean); // Filter out null values
90
  } catch (error) {
91
+ console.error(`❌ Error fetching from BT4G (page ${page}):`, error);
92
  return [];
93
  }
94
  }
95
 
96
+ // Function to fetch RSS feeds for two pages
97
+ async function fetchRSSFeeds(searchQuery) {
98
+ const allResults = [];
99
+
100
+ // Fetch page 1
101
+ const page1Results = await fetchPageResults(searchQuery, 1);
102
+ allResults.push(...page1Results);
103
+
104
+ // Fetch page 2
105
+ const page2Results = await fetchPageResults(searchQuery, 2);
106
+ allResults.push(...page2Results);
107
+
108
+ console.log(`✅ Processed ${allResults.length} results from first 2 pages`);
109
+ return allResults;
110
+ }
111
+
112
  export { fetchRSSFeeds };