Update mpeer.js
Browse files
mpeer.js
CHANGED
|
@@ -1,6 +1,22 @@
|
|
| 1 |
import fetch from 'node-fetch';
|
| 2 |
import * as cheerio from 'cheerio';
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
// ==============================
|
| 5 |
// fetchMegapeerTorrents: Main Export Function
|
| 6 |
// ==============================
|
|
@@ -31,18 +47,19 @@ async function fetchMegapeerTorrents(searchQuery) {
|
|
| 31 |
|
| 32 |
$('tr.table_fon').each((_, element) => {
|
| 33 |
const titleElement = $(element).find('a.url');
|
| 34 |
-
const
|
|
|
|
|
|
|
| 35 |
const torrentPage = titleElement.attr('href');
|
| 36 |
const downloadLink = $(element).find('a[href^="/download/"]').attr('href');
|
| 37 |
const size = $(element).find('td[align="right"]').text().trim();
|
| 38 |
const dateElement = $(element).find('td').first();
|
| 39 |
const date = dateElement.text().trim();
|
| 40 |
-
|
| 41 |
-
// Find the seeds and leechers
|
| 42 |
const seedLeechCell = $(element).find('td[align="center"]');
|
| 43 |
const seedsMatch = seedLeechCell.html().match(/<font color="#008000"[^>]*>(\d+)<\/font>/i);
|
| 44 |
const leechesMatch = seedLeechCell.html().match(/<font color="#8b0000"[^>]*>(\d+)<\/font>/i);
|
| 45 |
-
|
| 46 |
const seeds = seedsMatch ? parseInt(seedsMatch[1]) : 0;
|
| 47 |
const leeches = leechesMatch ? parseInt(leechesMatch[1]) : 0;
|
| 48 |
|
|
@@ -50,10 +67,10 @@ async function fetchMegapeerTorrents(searchQuery) {
|
|
| 50 |
|
| 51 |
const torrentPageUrl = `https://megapeer.vip${torrentPage}`;
|
| 52 |
const directDownloadUrl = `https://megapeer.vip${downloadLink}`;
|
| 53 |
-
const quality = extractQuality(
|
| 54 |
|
| 55 |
torrents.push({
|
| 56 |
-
title:
|
| 57 |
quality,
|
| 58 |
size,
|
| 59 |
date,
|
|
@@ -64,13 +81,10 @@ async function fetchMegapeerTorrents(searchQuery) {
|
|
| 64 |
});
|
| 65 |
});
|
| 66 |
|
| 67 |
-
// Process and get magnet links (if available)
|
| 68 |
const completedTorrents = await Promise.all(
|
| 69 |
torrents.map(async (torrent) => {
|
| 70 |
try {
|
| 71 |
-
// For Megapeer, we might need to extract magnet links from each torrent page
|
| 72 |
const magnetLink = await extractMagnetLink(torrent.torrentPageUrl);
|
| 73 |
-
|
| 74 |
return {
|
| 75 |
websiteTitle: torrent.title,
|
| 76 |
quality: torrent.quality,
|
|
@@ -92,16 +106,12 @@ async function fetchMegapeerTorrents(searchQuery) {
|
|
| 92 |
|
| 93 |
const validTorrents = completedTorrents.filter(Boolean);
|
| 94 |
|
| 95 |
-
// Sorting: Highest Seeds, Quality & Size
|
| 96 |
validTorrents.sort((a, b) => {
|
| 97 |
-
// Primary sort by seeds
|
| 98 |
if (b.seeds !== a.seeds) return b.seeds - a.seeds;
|
| 99 |
-
|
| 100 |
-
// Secondary sort by quality
|
| 101 |
const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };
|
| 102 |
const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
|
| 103 |
-
|
| 104 |
-
// Tertiary sort by size
|
| 105 |
return qualityDiff === 0 ? b.mainFileSize - a.mainFileSize : qualityDiff;
|
| 106 |
});
|
| 107 |
|
|
@@ -118,7 +128,7 @@ async function fetchMegapeerTorrents(searchQuery) {
|
|
| 118 |
// ==============================
|
| 119 |
async function extractMagnetLink(torrentPageUrl) {
|
| 120 |
try {
|
| 121 |
-
await new Promise(resolve => setTimeout(resolve, Math.random() * 2000 + 1000));
|
| 122 |
|
| 123 |
const response = await fetch(torrentPageUrl, {
|
| 124 |
headers: {
|
|
@@ -169,4 +179,4 @@ function extractQuality(title) {
|
|
| 169 |
// ==============================
|
| 170 |
// Export for Server.js
|
| 171 |
// ==============================
|
| 172 |
-
export { fetchMegapeerTorrents };
|
|
|
|
| 1 |
import fetch from 'node-fetch';
|
| 2 |
import * as cheerio from 'cheerio';
|
| 3 |
|
| 4 |
+
// ==============================
|
| 5 |
+
// sanitizeTitle: Clean Cyrillic & Noise
|
| 6 |
+
// ==============================
|
| 7 |
+
function sanitizeTitle(rawTitle) {
|
| 8 |
+
return rawTitle
|
| 9 |
+
.replace(/[\u0400-\u04FF]+/g, '') // Remove Cyrillic
|
| 10 |
+
.replace(/[^\x00-\x7F]+/g, '') // Remove non-ASCII remnants
|
| 11 |
+
.replace(/\[\s*[\u0400-\u04FF\d]+\s*[\u0400-\u04FF]*\]/g, match => {
|
| 12 |
+
const digits = match.match(/\d+/);
|
| 13 |
+
return digits ? `[${digits[0]}]` : '';
|
| 14 |
+
})
|
| 15 |
+
.replace(/\s{2,}/g, ' ') // Collapse multiple spaces
|
| 16 |
+
.replace(/^\/\s*/, '') // Trim leading slashes
|
| 17 |
+
.trim();
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
// ==============================
|
| 21 |
// fetchMegapeerTorrents: Main Export Function
|
| 22 |
// ==============================
|
|
|
|
| 47 |
|
| 48 |
$('tr.table_fon').each((_, element) => {
|
| 49 |
const titleElement = $(element).find('a.url');
|
| 50 |
+
const rawTitle = titleElement.text().trim();
|
| 51 |
+
const sanitizedTitle = sanitizeTitle(rawTitle);
|
| 52 |
+
|
| 53 |
const torrentPage = titleElement.attr('href');
|
| 54 |
const downloadLink = $(element).find('a[href^="/download/"]').attr('href');
|
| 55 |
const size = $(element).find('td[align="right"]').text().trim();
|
| 56 |
const dateElement = $(element).find('td').first();
|
| 57 |
const date = dateElement.text().trim();
|
| 58 |
+
|
|
|
|
| 59 |
const seedLeechCell = $(element).find('td[align="center"]');
|
| 60 |
const seedsMatch = seedLeechCell.html().match(/<font color="#008000"[^>]*>(\d+)<\/font>/i);
|
| 61 |
const leechesMatch = seedLeechCell.html().match(/<font color="#8b0000"[^>]*>(\d+)<\/font>/i);
|
| 62 |
+
|
| 63 |
const seeds = seedsMatch ? parseInt(seedsMatch[1]) : 0;
|
| 64 |
const leeches = leechesMatch ? parseInt(leechesMatch[1]) : 0;
|
| 65 |
|
|
|
|
| 67 |
|
| 68 |
const torrentPageUrl = `https://megapeer.vip${torrentPage}`;
|
| 69 |
const directDownloadUrl = `https://megapeer.vip${downloadLink}`;
|
| 70 |
+
const quality = extractQuality(sanitizedTitle);
|
| 71 |
|
| 72 |
torrents.push({
|
| 73 |
+
title: sanitizedTitle,
|
| 74 |
quality,
|
| 75 |
size,
|
| 76 |
date,
|
|
|
|
| 81 |
});
|
| 82 |
});
|
| 83 |
|
|
|
|
| 84 |
const completedTorrents = await Promise.all(
|
| 85 |
torrents.map(async (torrent) => {
|
| 86 |
try {
|
|
|
|
| 87 |
const magnetLink = await extractMagnetLink(torrent.torrentPageUrl);
|
|
|
|
| 88 |
return {
|
| 89 |
websiteTitle: torrent.title,
|
| 90 |
quality: torrent.quality,
|
|
|
|
| 106 |
|
| 107 |
const validTorrents = completedTorrents.filter(Boolean);
|
| 108 |
|
|
|
|
| 109 |
validTorrents.sort((a, b) => {
|
|
|
|
| 110 |
if (b.seeds !== a.seeds) return b.seeds - a.seeds;
|
| 111 |
+
|
|
|
|
| 112 |
const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };
|
| 113 |
const qualityDiff = (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
|
| 114 |
+
|
|
|
|
| 115 |
return qualityDiff === 0 ? b.mainFileSize - a.mainFileSize : qualityDiff;
|
| 116 |
});
|
| 117 |
|
|
|
|
| 128 |
// ==============================
|
| 129 |
async function extractMagnetLink(torrentPageUrl) {
|
| 130 |
try {
|
| 131 |
+
await new Promise(resolve => setTimeout(resolve, Math.random() * 2000 + 1000));
|
| 132 |
|
| 133 |
const response = await fetch(torrentPageUrl, {
|
| 134 |
headers: {
|
|
|
|
| 179 |
// ==============================
|
| 180 |
// Export for Server.js
|
| 181 |
// ==============================
|
| 182 |
+
export { fetchMegapeerTorrents };
|