Update server.js
Browse files
server.js
CHANGED
|
@@ -12,7 +12,7 @@ async function getDownloadLink(youtubeUrl) {
|
|
| 12 |
});
|
| 13 |
const page = await browser.newPage();
|
| 14 |
|
| 15 |
-
let
|
| 16 |
|
| 17 |
try {
|
| 18 |
// Open the website
|
|
@@ -21,19 +21,20 @@ async function getDownloadLink(youtubeUrl) {
|
|
| 21 |
// Enter the YouTube link into the input field
|
| 22 |
await page.fill("#url", youtubeUrl);
|
| 23 |
|
| 24 |
-
// Intercept
|
| 25 |
-
page.on("
|
| 26 |
-
const url =
|
| 27 |
-
if (url.includes("
|
| 28 |
-
|
|
|
|
| 29 |
}
|
| 30 |
});
|
| 31 |
|
| 32 |
// Click the convert/download button
|
| 33 |
await page.click("#convert-button");
|
| 34 |
|
| 35 |
-
// Wait
|
| 36 |
-
await
|
| 37 |
|
| 38 |
} catch (error) {
|
| 39 |
console.error("Error:", error);
|
|
@@ -41,7 +42,7 @@ async function getDownloadLink(youtubeUrl) {
|
|
| 41 |
await browser.close();
|
| 42 |
}
|
| 43 |
|
| 44 |
-
return
|
| 45 |
}
|
| 46 |
|
| 47 |
// API Endpoint
|
|
@@ -60,7 +61,7 @@ app.get("/api/q", async (req, res) => {
|
|
| 60 |
if (downloadLink) {
|
| 61 |
res.json({ success: true, download_link: downloadLink });
|
| 62 |
} else {
|
| 63 |
-
res.status(404).json({ success: false, error: "No
|
| 64 |
}
|
| 65 |
} catch (error) {
|
| 66 |
console.error("Error processing the YouTube link:", error);
|
|
|
|
| 12 |
});
|
| 13 |
const page = await browser.newPage();
|
| 14 |
|
| 15 |
+
let mp3DownloadLink = null;
|
| 16 |
|
| 17 |
try {
|
| 18 |
// Open the website
|
|
|
|
| 21 |
// Enter the YouTube link into the input field
|
| 22 |
await page.fill("#url", youtubeUrl);
|
| 23 |
|
| 24 |
+
// Intercept navigation events for the direct MP3 download
|
| 25 |
+
page.on("framenavigated", async (frame) => {
|
| 26 |
+
const url = frame.url();
|
| 27 |
+
if (url.includes(".mp3")) {
|
| 28 |
+
console.log("MP3 Detected:", url);
|
| 29 |
+
mp3DownloadLink = url;
|
| 30 |
}
|
| 31 |
});
|
| 32 |
|
| 33 |
// Click the convert/download button
|
| 34 |
await page.click("#convert-button");
|
| 35 |
|
| 36 |
+
// Wait up to 15 seconds for the MP3 link to appear
|
| 37 |
+
await page.waitForTimeout(15000);
|
| 38 |
|
| 39 |
} catch (error) {
|
| 40 |
console.error("Error:", error);
|
|
|
|
| 42 |
await browser.close();
|
| 43 |
}
|
| 44 |
|
| 45 |
+
return mp3DownloadLink;
|
| 46 |
}
|
| 47 |
|
| 48 |
// API Endpoint
|
|
|
|
| 61 |
if (downloadLink) {
|
| 62 |
res.json({ success: true, download_link: downloadLink });
|
| 63 |
} else {
|
| 64 |
+
res.status(404).json({ success: false, error: "No MP3 link found" });
|
| 65 |
}
|
| 66 |
} catch (error) {
|
| 67 |
console.error("Error processing the YouTube link:", error);
|