Update server.js
Browse files
server.js
CHANGED
|
@@ -1,41 +1,44 @@
|
|
| 1 |
-
const express = require(
|
| 2 |
-
const
|
|
|
|
| 3 |
|
| 4 |
const app = express();
|
| 5 |
const PORT = 7860;
|
| 6 |
|
|
|
|
|
|
|
| 7 |
async function getDownloadLink(youtubeUrl) {
|
| 8 |
-
const browser = await
|
| 9 |
headless: true,
|
| 10 |
-
args: [
|
| 11 |
});
|
| 12 |
-
const page = await browser.newPage();
|
| 13 |
|
|
|
|
| 14 |
let downloadLink = null;
|
| 15 |
|
| 16 |
try {
|
| 17 |
// Open the website
|
| 18 |
-
await page.goto(
|
| 19 |
|
| 20 |
// Enter the YouTube link into the input field
|
| 21 |
-
await page.
|
| 22 |
|
| 23 |
-
//
|
| 24 |
-
page.on(
|
| 25 |
const url = response.url();
|
| 26 |
-
if (url.includes(
|
| 27 |
downloadLink = url;
|
| 28 |
}
|
| 29 |
});
|
| 30 |
|
| 31 |
// Click the convert/download button
|
| 32 |
-
await page.click(
|
| 33 |
|
| 34 |
-
// Wait for
|
| 35 |
-
await
|
| 36 |
|
| 37 |
} catch (error) {
|
| 38 |
-
console.error(
|
| 39 |
} finally {
|
| 40 |
await browser.close();
|
| 41 |
}
|
|
@@ -44,11 +47,11 @@ async function getDownloadLink(youtubeUrl) {
|
|
| 44 |
}
|
| 45 |
|
| 46 |
// API Endpoint
|
| 47 |
-
app.get(
|
| 48 |
const youtubeUrl = req.query.url;
|
| 49 |
|
| 50 |
if (!youtubeUrl) {
|
| 51 |
-
return res.status(400).json({ error:
|
| 52 |
}
|
| 53 |
|
| 54 |
console.log(`Processing: ${youtubeUrl}`);
|
|
@@ -59,11 +62,11 @@ app.get('/api/q', async (req, res) => {
|
|
| 59 |
if (downloadLink) {
|
| 60 |
res.json({ success: true, download_link: downloadLink });
|
| 61 |
} else {
|
| 62 |
-
res.status(404).json({ success: false, error:
|
| 63 |
}
|
| 64 |
} catch (error) {
|
| 65 |
-
console.error(
|
| 66 |
-
res.status(500).json({ success: false, error:
|
| 67 |
}
|
| 68 |
});
|
| 69 |
|
|
|
|
| 1 |
+
const express = require("express");
|
| 2 |
+
const { chromium } = require("playwright");
|
| 3 |
+
const cors = require("cors");
|
| 4 |
|
| 5 |
const app = express();
|
| 6 |
const PORT = 7860;
|
| 7 |
|
| 8 |
+
app.use(cors());
|
| 9 |
+
|
| 10 |
async function getDownloadLink(youtubeUrl) {
|
| 11 |
+
const browser = await chromium.launch({
|
| 12 |
headless: true,
|
| 13 |
+
args: ["--no-sandbox", "--disable-setuid-sandbox"],
|
| 14 |
});
|
|
|
|
| 15 |
|
| 16 |
+
const page = await browser.newPage();
|
| 17 |
let downloadLink = null;
|
| 18 |
|
| 19 |
try {
|
| 20 |
// Open the website
|
| 21 |
+
await page.goto("https://ogmp3.com", { waitUntil: "domcontentloaded" });
|
| 22 |
|
| 23 |
// Enter the YouTube link into the input field
|
| 24 |
+
await page.fill("#url", youtubeUrl);
|
| 25 |
|
| 26 |
+
// Listen for network responses to find the download URL
|
| 27 |
+
page.on("response", async (response) => {
|
| 28 |
const url = response.url();
|
| 29 |
+
if (url.includes("safestytmp3.cc") && url.includes("/download/")) {
|
| 30 |
downloadLink = url;
|
| 31 |
}
|
| 32 |
});
|
| 33 |
|
| 34 |
// Click the convert/download button
|
| 35 |
+
await page.click("#convert-button");
|
| 36 |
|
| 37 |
+
// Wait for the download link to appear
|
| 38 |
+
await page.waitForTimeout(15000);
|
| 39 |
|
| 40 |
} catch (error) {
|
| 41 |
+
console.error("Error:", error);
|
| 42 |
} finally {
|
| 43 |
await browser.close();
|
| 44 |
}
|
|
|
|
| 47 |
}
|
| 48 |
|
| 49 |
// API Endpoint
|
| 50 |
+
app.get("/api/q", async (req, res) => {
|
| 51 |
const youtubeUrl = req.query.url;
|
| 52 |
|
| 53 |
if (!youtubeUrl) {
|
| 54 |
+
return res.status(400).json({ error: "Missing YouTube URL" });
|
| 55 |
}
|
| 56 |
|
| 57 |
console.log(`Processing: ${youtubeUrl}`);
|
|
|
|
| 62 |
if (downloadLink) {
|
| 63 |
res.json({ success: true, download_link: downloadLink });
|
| 64 |
} else {
|
| 65 |
+
res.status(404).json({ success: false, error: "No download link found" });
|
| 66 |
}
|
| 67 |
} catch (error) {
|
| 68 |
+
console.error("Error processing the YouTube link:", error);
|
| 69 |
+
res.status(500).json({ success: false, error: "Internal Server Error" });
|
| 70 |
}
|
| 71 |
});
|
| 72 |
|