Create mpeer.js
Browse files
mpeer.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fetch from 'node-fetch';
|
| 2 |
+
import cheerio from 'cheerio';
|
| 3 |
+
|
| 4 |
+
/**
|
| 5 |
+
* Cleans the search query (removes years & special characters).
|
| 6 |
+
* @param {string} query - The search term.
|
| 7 |
+
* @returns {string} - Cleaned query.
|
| 8 |
+
*/
|
| 9 |
+
function cleanSearchQuery(query) {
|
| 10 |
+
if (!query) return '';
|
| 11 |
+
return query.replace(/\(?\b(19|20)\d{2}\b\)?/g, '').replace(/[^\w\s]/g, '').trim();
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
/**
|
| 15 |
+
* Fetches torrents from MegaPeer.vip.
|
| 16 |
+
* @param {string} searchQuery - The search term.
|
| 17 |
+
* @returns {Promise<Array>} - List of torrents with `.torrent` or magnet links.
|
| 18 |
+
*/
|
| 19 |
+
async function fetchMegaPeerTorrents(searchQuery) {
|
| 20 |
+
const cleanedQuery = cleanSearchQuery(searchQuery);
|
| 21 |
+
console.log(`🔄 Searching MegaPeer for: "${cleanedQuery}"`);
|
| 22 |
+
|
| 23 |
+
const SEARCH_URL = `https://megapeer.vip/browse.php?search=${encodeURIComponent(cleanedQuery)}&age=&cat=0&stype=0&sort=2&ascdesc=0`;
|
| 24 |
+
|
| 25 |
+
try {
|
| 26 |
+
const response = await fetch(SEARCH_URL, {
|
| 27 |
+
headers: { 'User-Agent': 'Mozilla/5.0' }
|
| 28 |
+
});
|
| 29 |
+
|
| 30 |
+
if (!response.ok) throw new Error(`❌ Failed to fetch search results: ${response.status}`);
|
| 31 |
+
|
| 32 |
+
const html = await response.text();
|
| 33 |
+
const $ = cheerio.load(html);
|
| 34 |
+
const torrents = [];
|
| 35 |
+
|
| 36 |
+
$('a[href^="/download/"]').each((_, el) => {
|
| 37 |
+
const href = $(el).attr('href');
|
| 38 |
+
const torrentLink = `https://megapeer.vip${href}`;
|
| 39 |
+
torrents.push({ torrentLink });
|
| 40 |
+
});
|
| 41 |
+
|
| 42 |
+
$('a.url').each((_, el) => {
|
| 43 |
+
const href = $(el).attr('href');
|
| 44 |
+
const title = $(el).text().trim();
|
| 45 |
+
const detailPage = `https://megapeer.vip${href}`;
|
| 46 |
+
torrents.push({ title, detailPage });
|
| 47 |
+
});
|
| 48 |
+
|
| 49 |
+
// ✅ Fetch magnet links for items that don't have a `.torrent`
|
| 50 |
+
await Promise.all(
|
| 51 |
+
torrents.map(async (torrent) => {
|
| 52 |
+
if (!torrent.torrentLink && torrent.detailPage) {
|
| 53 |
+
torrent.magnetLink = await fetchMagnetFromDetailPage(torrent.detailPage);
|
| 54 |
+
}
|
| 55 |
+
})
|
| 56 |
+
);
|
| 57 |
+
|
| 58 |
+
const validTorrents = torrents.filter(t => t.torrentLink || t.magnetLink);
|
| 59 |
+
console.log(`✅ Processed ${validTorrents.length} MegaPeer torrents.`);
|
| 60 |
+
return validTorrents;
|
| 61 |
+
} catch (error) {
|
| 62 |
+
console.error('❌ Error fetching from MegaPeer:', error);
|
| 63 |
+
return [];
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
/**
|
| 68 |
+
* Extracts the magnet link from a MegaPeer torrent detail page.
|
| 69 |
+
* @param {string} url - The torrent detail page URL.
|
| 70 |
+
* @returns {Promise<string|null>} - The extracted magnet link or null.
|
| 71 |
+
*/
|
| 72 |
+
async function fetchMagnetFromDetailPage(url) {
|
| 73 |
+
try {
|
| 74 |
+
const response = await fetch(url, { headers: { 'User-Agent': 'Mozilla/5.0' } });
|
| 75 |
+
if (!response.ok) return null;
|
| 76 |
+
|
| 77 |
+
const html = await response.text();
|
| 78 |
+
const $ = cheerio.load(html);
|
| 79 |
+
|
| 80 |
+
const magnetLink = $('a[href^="magnet:?xt=urn:btih:"]').attr('href');
|
| 81 |
+
return magnetLink || null;
|
| 82 |
+
} catch (error) {
|
| 83 |
+
console.error(`❌ Failed to fetch magnet from: ${url}`, error);
|
| 84 |
+
return null;
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
/**
|
| 89 |
+
* Main function for `server.js`.
|
| 90 |
+
* @param {string} searchQuery - The search term.
|
| 91 |
+
* @returns {Promise<Array>} - List of torrents with download/magnet links.
|
| 92 |
+
*/
|
| 93 |
+
async function fetchRSSFeeds(searchQuery) {
|
| 94 |
+
return await fetchMegaPeerTorrents(searchQuery);
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
// ✅ Export as a single function for easy use in `server.js`
|
| 98 |
+
export { fetchRSSFeeds };
|