Update bt4g.js
Browse files
bt4g.js
CHANGED
|
@@ -35,58 +35,63 @@ async function fetchPageResults(searchQuery, page) {
|
|
| 35 |
|
| 36 |
const rssData = await response.text();
|
| 37 |
|
| 38 |
-
//
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
} catch (error) {
|
| 91 |
console.error(`❌ Error fetching from BT4G (page ${page}):`, error);
|
| 92 |
return [];
|
|
|
|
| 35 |
|
| 36 |
const rssData = await response.text();
|
| 37 |
|
| 38 |
+
// Catch malformed XML errors and return empty result for that page
|
| 39 |
+
try {
|
| 40 |
+
const parsedData = await parser.parseStringPromise(rssData);
|
| 41 |
+
const items = parsedData?.rss?.channel?.item || [];
|
| 42 |
+
|
| 43 |
+
console.log(`Found ${items.length} items on page ${page}`);
|
| 44 |
+
|
| 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 (parseError) {
|
| 91 |
+
console.error(`❌ Error parsing RSS XML on page ${page}:`, parseError);
|
| 92 |
+
return [];
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
} catch (error) {
|
| 96 |
console.error(`❌ Error fetching from BT4G (page ${page}):`, error);
|
| 97 |
return [];
|