Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const { chromium } = require('playwright-chromium');
|
| 3 |
+
const cors = require('cors');
|
| 4 |
+
|
| 5 |
+
const app = express();
|
| 6 |
+
const PORT = 7860;
|
| 7 |
+
|
| 8 |
+
app.use(cors());
|
| 9 |
+
|
| 10 |
+
app.get('/download-links', async (req, res) => {
|
| 11 |
+
const { url } = req.query;
|
| 12 |
+
|
| 13 |
+
if (!url) {
|
| 14 |
+
return res.status(400).json({ error: "Missing 'url' query parameter" });
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
try {
|
| 18 |
+
console.log(`Scraping: ${url}`);
|
| 19 |
+
const downloadLinks = await getVideoLinks(url);
|
| 20 |
+
res.json({ downloadLinks });
|
| 21 |
+
} catch (error) {
|
| 22 |
+
res.status(500).json({ error: "Failed to fetch download links", details: error.message });
|
| 23 |
+
}
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
async function getVideoLinks(downloadPageUrl) {
|
| 27 |
+
console.log(`Opening Playwright: ${downloadPageUrl}`);
|
| 28 |
+
const browser = await chromium.launch({ headless: true });
|
| 29 |
+
const page = await browser.newPage();
|
| 30 |
+
|
| 31 |
+
try {
|
| 32 |
+
await page.goto(downloadPageUrl, { waitUntil: 'networkidle' });
|
| 33 |
+
|
| 34 |
+
// Wait for download buttons to appear
|
| 35 |
+
await page.waitForSelector('a[href*="ggredi.info/download.php"]', { timeout: 10000 });
|
| 36 |
+
|
| 37 |
+
// Extract links
|
| 38 |
+
const videoLinks = await page.evaluate(() => {
|
| 39 |
+
return Array.from(document.querySelectorAll('a[href*="ggredi.info/download.php"]')).map(link => ({
|
| 40 |
+
quality: link.innerText.trim(),
|
| 41 |
+
url: link.href
|
| 42 |
+
}));
|
| 43 |
+
});
|
| 44 |
+
|
| 45 |
+
await browser.close();
|
| 46 |
+
|
| 47 |
+
// Convert links to final MP4 links
|
| 48 |
+
const directDownloadLinks = {};
|
| 49 |
+
const promises = videoLinks
|
| 50 |
+
.filter(link => link.quality.includes('360') || link.quality.includes('720'))
|
| 51 |
+
.map(async link => {
|
| 52 |
+
const finalLink = await getFinalMp4Link(link.url);
|
| 53 |
+
if (finalLink) {
|
| 54 |
+
directDownloadLinks[link.quality.replace(/\D/g, '') + 'p'] = finalLink;
|
| 55 |
+
}
|
| 56 |
+
});
|
| 57 |
+
|
| 58 |
+
await Promise.all(promises);
|
| 59 |
+
return directDownloadLinks;
|
| 60 |
+
} catch (error) {
|
| 61 |
+
console.error("Error extracting video links:", error.message);
|
| 62 |
+
await browser.close();
|
| 63 |
+
return { "360p": "Not available", "720p": "Not available" };
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
async function getFinalMp4Link(redirectLink) {
|
| 68 |
+
try {
|
| 69 |
+
console.log(`Following redirect: ${redirectLink}`);
|
| 70 |
+
const response = await fetch(redirectLink, { redirect: "follow" });
|
| 71 |
+
const finalUrl = response.url;
|
| 72 |
+
|
| 73 |
+
if (finalUrl && finalUrl.includes(".mp4")) {
|
| 74 |
+
console.log(`Final MP4 Link: ${finalUrl}`);
|
| 75 |
+
return finalUrl;
|
| 76 |
+
} else {
|
| 77 |
+
console.log("No valid MP4 link found.");
|
| 78 |
+
return null;
|
| 79 |
+
}
|
| 80 |
+
} catch (error) {
|
| 81 |
+
console.error("Error fetching final MP4 link:", error.message);
|
| 82 |
+
return null;
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
// Start the Express server
|
| 87 |
+
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));
|