Create mpeer.js
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798import fetch from 'node-fetch';import cheerio from 'cheerio';
/** * Cleans the search query (removes years & special characters). * @param {string} query - The search term. * @returns {string} - Cleaned query. */function cleanSearchQuery(query) { if (!query) return ''; return query.replace(/\(?\b(19|20)\d{2}\b\)?/g, '').replace(/[^\w\s]/g, '').trim();}
/** * Fetches torrents from MegaPeer.vip. * @param {string} searchQuery - The search term. * @returns {Promise<Array>} - List of torrents with `.torrent` or magnet links. */async function fetchMegaPeerTorrents(searchQuery) { const cleanedQuery = cleanSearchQuery(searchQuery); console.log(`🔄 Searching MegaPeer for: "${cleanedQuery}"`);
const SEARCH_URL = `https://megapeer.vip/browse.php?search=${encodeURIComponent(cleanedQuery)}&age=&cat=0&stype=0&sort=2&ascdesc=0`;
try { const response = await fetch(SEARCH_URL, { headers: { 'User-Agent': 'Mozilla/5.0' } });
if (!response.ok) throw new Error(`❌ Failed to fetch search results: ${response.status}`);
const html = await response.text(); const $ = cheerio.load(html); const torrents = [];
$('a[href^="/download/"]').each((_, el) => { const href = $(el).attr('href'); const torrentLink = `https://megapeer.vip${href}`; torrents.push({ torrentLink }); });
$('a.url').each((_, el) => { const href = $(el).attr('href'); const title = $(el).text().trim(); const detailPage = `https://megapeer.vip${href}`; torrents.push({ title, detailPage }); });
// ✅ Fetch magnet links for items that don't have a `.torrent` await Promise.all( torrents.map(async (torrent) => { if (!torrent.torrentLink && torrent.detailPage) { torrent.magnetLink = await fetchMagnetFromDetailPage(torrent.detailPage); } }) );
const validTorrents = torrents.filter(t => t.torrentLink || t.magnetLink); console.log(`✅ Processed ${validTorrents.length} MegaPeer torrents.`); return validTorrents; } catch (error) { console.error('❌ Error fetching from MegaPeer:', error); return []; }}
/** * Extracts the magnet link from a MegaPeer torrent detail page. * @param {string} url - The torrent detail page URL. * @returns {Promise<string|null>} - The extracted magnet link or null. */async function fetchMagnetFromDetailPage(url) { try { const response = await fetch(url, { headers: { 'User-Agent': 'Mozilla/5.0' } }); if (!response.ok) return null;
const html = await response.text(); const $ = cheerio.load(html);
const magnetLink = $('a[href^="magnet:?xt=urn:btih:"]').attr('href'); return magnetLink || null; } catch (error) { console.error(`❌ Failed to fetch magnet from: ${url}`, error); return null; }}
/** * Main function for `server.js`. * @param {string} searchQuery - The search term. * @returns {Promise<Array>} - List of torrents with download/magnet links. */async function fetchRSSFeeds(searchQuery) { return await fetchMegaPeerTorrents(searchQuery);}
// ✅ Export as a single function for easy use in `server.js`export { fetchRSSFeeds };mpeer.jsAdded+98