Update bt4g.js
Browse files
bt4g.js
CHANGED
|
@@ -1,134 +1,128 @@
|
|
| 1 |
-
import xml2js from 'xml2js';
|
| 2 |
-
|
| 3 |
-
const parser = new xml2js.Parser({
|
| 4 |
-
explicitArray: false,
|
| 5 |
-
ignoreAttrs: true
|
| 6 |
-
});
|
| 7 |
-
|
| 8 |
-
function extractQuality(title) {
|
| 9 |
-
if (!title) return '';
|
| 10 |
-
const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);
|
| 11 |
-
return qualityMatch ? qualityMatch[1].toLowerCase() : '';
|
| 12 |
-
}
|
| 13 |
-
|
| 14 |
-
function formatSize(bytes) {
|
| 15 |
-
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
| 16 |
-
if (bytes === 0) return '0 B';
|
| 17 |
-
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
| 18 |
-
return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${sizes[i]}`;
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
-
function extractMagnetFromDescription(description) {
|
| 22 |
-
if (!description) return null;
|
| 23 |
-
const magnetMatch = description.match(/magnet:\?xt=urn:btih:[A-Za-z0-9]+[^"'\s]*/);
|
| 24 |
-
return magnetMatch ? magnetMatch[0] : null;
|
| 25 |
-
}
|
| 26 |
-
|
| 27 |
-
async function fetchRSSFeeds(searchQuery, type = 'movie') {
|
| 28 |
-
console.log('\n🔄 Fetching RSS feed for:', searchQuery);
|
| 29 |
-
|
| 30 |
-
try {
|
| 31 |
-
//
|
| 32 |
-
const
|
| 33 |
-
const
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
const
|
| 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 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
console.error('❌ Error fetching from BT4G:', error);
|
| 130 |
-
return [];
|
| 131 |
-
}
|
| 132 |
-
}
|
| 133 |
-
|
| 134 |
-
export { fetchRSSFeeds };
|
|
|
|
| 1 |
+
import xml2js from 'xml2js';
|
| 2 |
+
|
| 3 |
+
const parser = new xml2js.Parser({
|
| 4 |
+
explicitArray: false,
|
| 5 |
+
ignoreAttrs: true
|
| 6 |
+
});
|
| 7 |
+
|
| 8 |
+
function extractQuality(title) {
|
| 9 |
+
if (!title) return '';
|
| 10 |
+
const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);
|
| 11 |
+
return qualityMatch ? qualityMatch[1].toLowerCase() : '';
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
function formatSize(bytes) {
|
| 15 |
+
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
| 16 |
+
if (bytes === 0) return '0 B';
|
| 17 |
+
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
| 18 |
+
return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${sizes[i]}`;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
function extractMagnetFromDescription(description) {
|
| 22 |
+
if (!description) return null;
|
| 23 |
+
const magnetMatch = description.match(/magnet:\?xt=urn:btih:[A-Za-z0-9]+[^"'\s]*/);
|
| 24 |
+
return magnetMatch ? magnetMatch[0] : null;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
async function fetchRSSFeeds(searchQuery, type = 'movie') {
|
| 28 |
+
console.log('\n🔄 Fetching RSS feed for:', searchQuery);
|
| 29 |
+
|
| 30 |
+
try {
|
| 31 |
+
// Construct the BT4G RSS URL with the search query
|
| 32 |
+
const encodedSearch = encodeURIComponent(searchQuery);
|
| 33 |
+
const rssUrl = `https://bt4gprx.com/search?q=${encodedSearch}&p=1&orderby=size&page=rss`;
|
| 34 |
+
|
| 35 |
+
console.log('Request URL:', rssUrl);
|
| 36 |
+
|
| 37 |
+
const response = await fetch(rssUrl, {
|
| 38 |
+
headers: {
|
| 39 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
| 40 |
+
'Accept': 'application/rss+xml,application/xml,text/xml'
|
| 41 |
+
}
|
| 42 |
+
});
|
| 43 |
+
|
| 44 |
+
if (!response.ok) {
|
| 45 |
+
throw new Error(`Failed to fetch from BT4G: ${response.status}`);
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
const rssData = await response.text();
|
| 49 |
+
const result = await parser.parseStringPromise(rssData);
|
| 50 |
+
|
| 51 |
+
if (!result?.rss?.channel?.item) {
|
| 52 |
+
console.log('No items found');
|
| 53 |
+
return [];
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
const items = Array.isArray(result.rss.channel.item) ? result.rss.channel.item : [result.rss.channel.item];
|
| 57 |
+
|
| 58 |
+
console.log(`Found ${items.length} items`);
|
| 59 |
+
|
| 60 |
+
const streams = items.map(item => {
|
| 61 |
+
if (!item || !item.title || !item.description) return null;
|
| 62 |
+
|
| 63 |
+
// Extract magnet link from description
|
| 64 |
+
const magnetLink = extractMagnetFromDescription(item.description);
|
| 65 |
+
if (!magnetLink) return null;
|
| 66 |
+
|
| 67 |
+
// Extract size from description (if available)
|
| 68 |
+
const sizeMatch = item.description.match(/Size:\s*([\d.]+)\s*(MB|GB|TB)/i);
|
| 69 |
+
const size = sizeMatch ? `${sizeMatch[1]} ${sizeMatch[2]}` : 'Unknown';
|
| 70 |
+
|
| 71 |
+
// Convert size to bytes for sorting
|
| 72 |
+
let sizeInBytes = 0;
|
| 73 |
+
if (sizeMatch) {
|
| 74 |
+
const [, value, unit] = sizeMatch;
|
| 75 |
+
const multiplier = unit.toUpperCase() === 'TB' ? 1024 * 1024 * 1024 * 1024 :
|
| 76 |
+
unit.toUpperCase() === 'GB' ? 1024 * 1024 * 1024 :
|
| 77 |
+
1024 * 1024;
|
| 78 |
+
sizeInBytes = parseFloat(value) * multiplier;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
// Extract info hash from magnet link
|
| 82 |
+
const infoHashMatch = magnetLink.match(/urn:btih:([A-Za-z0-9]+)/i);
|
| 83 |
+
const infoHash = infoHashMatch ? infoHashMatch[1] : null;
|
| 84 |
+
|
| 85 |
+
return {
|
| 86 |
+
magnetLink,
|
| 87 |
+
websiteTitle: item.title, // Make sure it's pulled from the RSS title correctly
|
| 88 |
+
quality: extractQuality(item.title),
|
| 89 |
+
size,
|
| 90 |
+
source: item.author || 'Unknown',
|
| 91 |
+
infoHash,
|
| 92 |
+
mainFileSize: sizeInBytes,
|
| 93 |
+
pubDate: item.pubDate,
|
| 94 |
+
categories: Array.isArray(item.category) ? item.category : [item.category]
|
| 95 |
+
};
|
| 96 |
+
}).filter(Boolean);
|
| 97 |
+
|
| 98 |
+
// Sort by quality and size
|
| 99 |
+
streams.sort((a, b) => {
|
| 100 |
+
const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };
|
| 101 |
+
const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
|
| 102 |
+
if (qualityDiff === 0) {
|
| 103 |
+
return b.mainFileSize - a.mainFileSize;
|
| 104 |
+
}
|
| 105 |
+
return qualityDiff;
|
| 106 |
+
});
|
| 107 |
+
|
| 108 |
+
console.log(`✅ Processed ${streams.length} valid streams`);
|
| 109 |
+
|
| 110 |
+
if (streams.length > 0) {
|
| 111 |
+
console.log('Sample stream:', {
|
| 112 |
+
title: streams[0].websiteTitle,
|
| 113 |
+
quality: streams[0].quality,
|
| 114 |
+
size: streams[0].size,
|
| 115 |
+
source: streams[0].source
|
| 116 |
+
});
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
return streams;
|
| 120 |
+
|
| 121 |
+
} catch (error) {
|
| 122 |
+
console.error('❌ Error fetching from BT4G:', error);
|
| 123 |
+
return [];
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
export { fetchRSSFeeds };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|