Update bt4g.js
Browse files9 unmodified lines1011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231249 unmodified linestrimValues: true,});async function fetchPageResults(searchQuery, page) {const url = `https://stremio-rev-proxy.h-y.workers.dev/https://bt4gprx.com/search?q=${encodeURIComponent(searchQuery)}&page=${page}&rss`;console.log(`🔄 Fetching RSS feed for: ${searchQuery} (Page: ${page})`);try {const response = await Promise.race([fetch(url, {headers: {'accept': '*/*','accept-language': 'en-US,en;q=0.9','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',},method: 'GET',}),timeout(5000),]);if (!response || !response.ok) {throw new Error(`Failed to fetch from BT4G: ${response?.status || 'timeout'}`);}const rssData = await response.text();console.log("Raw RSS data from BT4G:", rssData);try {const parsedData = parser.parse(rssData);const items = parsedData?.rss?.channel?.item || [];console.log(`Found ${items.length} items on page ${page}`);const torrentResults = await Promise.all(items.map(async (item) => {if (!item.title || !item.description) return null;const magnetLink = item.link;if (!magnetLink) return null;const sizeMatch = item.description.match(/Size:\s*([\d.]+)\s*(MB|GB|TB)/i);const size = sizeMatch ? `${sizeMatch[1]} ${sizeMatch[2]}` : 'Unknown';let sizeInBytes = 0;if (sizeMatch) {const [, value, unit] = sizeMatch;const multiplier =unit.toUpperCase() === 'TB'? 1024 * 1024 * 1024 * 1024: unit.toUpperCase() === 'GB'? 1024 * 1024 * 1024: 1024 * 1024;sizeInBytes = parseFloat(value) * multiplier;}const infoHashMatch = magnetLink.match(/urn:btih:([A-Za-z0-9]+)/i);const infoHash = infoHashMatch ? infoHashMatch[1] : null;let websiteTitle = item.title;if (websiteTitle.includes(' - ')) {websiteTitle = websiteTitle.split(' - ')[0];}return {magnetLink,websiteTitle,quality: extractQuality(item.title),size,source: 'BT4G',infoHash,mainFileSize: sizeInBytes,pubDate: item.pubDate,categories: Array.isArray(item.category) ? item.category : [item.category],};}));return torrentResults.filter(Boolean); // Filter out null values} catch (err) {console.error('❌ Error parsing RSS data:', err);return [];}} catch (error) {console.error(`❌ Error fetching from BT4G (Page ${page}):`, error);return [];}}// Function to fetch RSS feeds for both pages (1 and 2)async function fetchRSSFeeds(searchQuery) {const allResults = [];// Fetch results for page 1const page1Results = await fetchPageResults(searchQuery, 1);allResults.push(...page1Results);// Fetch results for page 2const page2Results = await fetchPageResults(searchQuery, 2);allResults.push(...page2Results);console.log(`✅ Processed ${allResults.length} valid streams from pages 1 and 2`);// Sort by quality and sizeallResults.sort((a, b) => {const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);if (qualityDiff === 0) {return b.mainFileSize - a.mainFileSize;}return qualityDiff;});return allResults;}export { fetchRSSFeeds };9 unmodified lines1011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251269 unmodified linestrimValues: true,});async function fetchRSSFeeds(searchQuery) {const page1Url = `https://stremio-rev-proxy.h-y.workers.dev/https://bt4gprx.com/search?q=${encodeURIComponent(searchQuery)}&page=1&orderby=size&page=rss`;const page2Url = `https://bt4gprx.com/search?q=${encodeURIComponent(searchQuery)}&page=2&orderby=size&page=rss`; // Direct URL for page 2console.log('\n🔄 Fetching RSS feed for:', searchQuery);try {// Fetch page 1 with proxyconst page1Response = await fetch(page1Url, {headers: {'accept': '*/*','accept-language': 'en-US,en;q=0.9',},method: 'GET',});if (!page1Response.ok) {throw new Error(`Failed to fetch page 1: ${page1Response.status}`);}const page1RssData = await page1Response.text();const parsedPage1Data = await parser.parseStringPromise(page1RssData);const page1Items = parsedPage1Data?.rss?.channel?.item || [];console.log(`Found ${page1Items.length} items on page 1`);// Fetch page 2 without proxyconst page2Response = await fetch(page2Url, {headers: {'accept': '*/*','accept-language': 'en-US,en;q=0.9',},method: 'GET',});if (!page2Response.ok) {throw new Error(`Failed to fetch page 2: ${page2Response.status}`);}const page2RssData = await page2Response.text();const parsedPage2Data = await parser.parseStringPromise(page2RssData);const page2Items = parsedPage2Data?.rss?.channel?.item || [];console.log(`Found ${page2Items.length} items on page 2`);// Combine items from both pagesconst allItems = [...page1Items, ...page2Items];// Map each item to the desired structureconst torrentResults = await Promise.all(allItems.map(async (item) => {if (!item.title || !item.description) return null;const magnetLink = item.link; // Use the "link" as Magnet URIif (!magnetLink) return null;const sizeMatch = item.description.match(/Size:\s*([\d.]+)\s*(MB|GB|TB)/i);const size = sizeMatch ? `${sizeMatch[1]} ${sizeMatch[2]}` : 'Unknown';let sizeInBytes = 0;if (sizeMatch) {const [, value, unit] = sizeMatch;const multiplier =unit.toUpperCase() === 'TB'? 1024 * 1024 * 1024 * 1024: unit.toUpperCase() === 'GB'? 1024 * 1024 * 1024: 1024 * 1024;sizeInBytes = parseFloat(value) * multiplier;}const infoHashMatch = magnetLink.match(/urn:btih:([A-Za-z0-9]+)/i);const infoHash = infoHashMatch ? infoHashMatch[1] : null;let websiteTitle = item.title;if (websiteTitle.includes(' - ')) {websiteTitle = websiteTitle.split(' - ')[0];}return {magnetLink,websiteTitle,quality: extractQuality(item.title),size,source: 'BT4G',infoHash,mainFileSize: sizeInBytes,pubDate: item.pubDate,categories: Array.isArray(item.category) ? item.category : [item.category],};}));const validStreams = torrentResults.filter(Boolean);// Sort streams by quality and size (with higher quality and larger size first)validStreams.sort((a, b) => {const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);if (qualityDiff === 0) {return b.mainFileSize - a.mainFileSize;}return qualityDiff;});console.log(✅ Processed ${validStreams.length} valid streams);return validStreams;} catch (error) {console.error('❌ Error fetching from BT4G:', error);return [];}}export { fetchRSSFeeds };
bt4g.js+101-99