Update bt4g.js
Browse files1234569 unmodified lines161718192021222324252627282930313233343536373813 unmodified lines525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105import xml2js from 'xml2js';// Initialize xml2js parser with necessary optionsconst parser = new xml2js.Parser({explicitArray: false,ignoreAttrs: true9 unmodified lines// Timeout helper functionconst timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms));// Function to fetch and parse the RSS feed based on the search queryasync function fetchRSSFeeds(searchQuery) {const allResults = [];// Fetch page 1const page1Results = await fetchPageResults(searchQuery, 1);allResults.push(...page1Results);// Fetch page 2const page2Results = await fetchPageResults(searchQuery, 2);allResults.push(...page2Results);console.log(`✅ Processed ${allResults.length} results from first 2 pages`);return allResults;}// Helper function to fetch results from a specific pageasync function fetchPageResults(searchQuery, page) {const url = `https://bt4gprx.com/search?q=${encodeURIComponent(searchQuery)}&page=${page}&orderby=size`;console.log(`🔄 Fetching page ${page} for:`, searchQuery);13 unmodified linesconst rssData = await response.text();// Parse the RSS data using xml2jsconst parsedData = await parser.parseStringPromise(rssData);const items = parsedData?.rss?.channel?.item || [];console.log(`Found ${items.length} items on page ${page}`);// Process items into the desired formatreturn items.map((item) => {if (!item.title || !item.description) return null;const magnetLink = item.link;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],};}).filter(Boolean);} catch (error) {console.error('❌ Error fetching from BT4G:', error);return [];}}export { fetchRSSFeeds };123459 unmodified lines1516171819202113 unmodified lines3536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112import xml2js from 'xml2js';const parser = new xml2js.Parser({explicitArray: false,ignoreAttrs: true9 unmodified lines// Timeout helper functionconst timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms));// Function to fetch results from a specific pageasync function fetchPageResults(searchQuery, page) {const url = `https://bt4gprx.com/search?q=${encodeURIComponent(searchQuery)}&page=${page}&orderby=size`;console.log(`🔄 Fetching page ${page} for:`, searchQuery);13 unmodified linesconst rssData = await response.text();// Parsing the RSS XML responseconst parsedData = await parser.parseStringPromise(rssData);const items = parsedData?.rss?.channel?.item || [];console.log(`Found ${items.length} items on page ${page}`);// Map each item to the desired structureconst 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 (error) {console.error(`❌ Error fetching from BT4G (page ${page}):`, error);return [];}}// Function to fetch RSS feeds for two pagesasync function fetchRSSFeeds(searchQuery) {const allResults = [];// Fetch page 1const page1Results = await fetchPageResults(searchQuery, 1);allResults.push(...page1Results);// Fetch page 2const page2Results = await fetchPageResults(searchQuery, 2);allResults.push(...page2Results);console.log(`✅ Processed ${allResults.length} results from first 2 pages`);return allResults;}export { fetchRSSFeeds };
bt4g.js+66-59