Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const puppeteer = require('puppeteer');
|
| 3 |
+
|
| 4 |
+
const app = express();
|
| 5 |
+
const PORT = 7860;
|
| 6 |
+
|
| 7 |
+
async function getDownloadLink(youtubeUrl) {
|
| 8 |
+
const browser = await puppeteer.launch({ headless: true });
|
| 9 |
+
const page = await browser.newPage();
|
| 10 |
+
|
| 11 |
+
let downloadLink = null;
|
| 12 |
+
|
| 13 |
+
try {
|
| 14 |
+
// Open the website
|
| 15 |
+
await page.goto('https://ogmp3.com', { waitUntil: 'domcontentloaded' });
|
| 16 |
+
|
| 17 |
+
// Enter the YouTube link into the input field
|
| 18 |
+
await page.type('#url', youtubeUrl);
|
| 19 |
+
|
| 20 |
+
// Intercept network responses to find the download URL
|
| 21 |
+
page.on('response', async (response) => {
|
| 22 |
+
const url = response.url();
|
| 23 |
+
if (url.includes('safestytmp3.cc') && url.includes('/download/')) {
|
| 24 |
+
downloadLink = url;
|
| 25 |
+
}
|
| 26 |
+
});
|
| 27 |
+
|
| 28 |
+
// Click the convert/download button
|
| 29 |
+
await page.click('#convert-button');
|
| 30 |
+
|
| 31 |
+
// Wait for 15 seconds max
|
| 32 |
+
await new Promise(resolve => setTimeout(resolve, 15000));
|
| 33 |
+
|
| 34 |
+
} catch (error) {
|
| 35 |
+
console.error('Error:', error);
|
| 36 |
+
} finally {
|
| 37 |
+
await browser.close();
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
return downloadLink;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
// API Endpoint
|
| 44 |
+
app.get('/api/q', async (req, res) => {
|
| 45 |
+
const youtubeUrl = req.query.url;
|
| 46 |
+
|
| 47 |
+
if (!youtubeUrl) {
|
| 48 |
+
return res.status(400).json({ error: 'Missing YouTube URL' });
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
console.log(`Processing: ${youtubeUrl}`);
|
| 52 |
+
|
| 53 |
+
const downloadLink = await getDownloadLink(youtubeUrl);
|
| 54 |
+
|
| 55 |
+
if (downloadLink) {
|
| 56 |
+
res.json({ success: true, download_link: downloadLink });
|
| 57 |
+
} else {
|
| 58 |
+
res.status(404).json({ success: false, error: 'No download link found' });
|
| 59 |
+
}
|
| 60 |
+
});
|
| 61 |
+
|
| 62 |
+
// Start Server
|
| 63 |
+
app.listen(PORT, () => {
|
| 64 |
+
console.log(`Server running on http://localhost:${PORT}`);
|
| 65 |
+
});
|