Update server.js
Browse files
server.js
CHANGED
|
@@ -6,9 +6,9 @@ const axios = require("axios");
|
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
const PORT = 7860;
|
|
|
|
| 9 |
const DOWNLOADS_DIR = path.join(__dirname, "downloads");
|
| 10 |
|
| 11 |
-
// Ensure the downloads directory exists
|
| 12 |
if (!fs.existsSync(DOWNLOADS_DIR)) {
|
| 13 |
fs.mkdirSync(DOWNLOADS_DIR, { recursive: true });
|
| 14 |
}
|
|
@@ -16,20 +16,16 @@ if (!fs.existsSync(DOWNLOADS_DIR)) {
|
|
| 16 |
async function getDownloadLink(youtubeUrl) {
|
| 17 |
const browser = await chromium.launch({
|
| 18 |
headless: true,
|
| 19 |
-
executablePath: "/usr/bin/chromium",
|
| 20 |
args: ["--no-sandbox", "--disable-setuid-sandbox"]
|
| 21 |
});
|
| 22 |
const page = await browser.newPage();
|
| 23 |
let downloadLink = null;
|
| 24 |
|
| 25 |
try {
|
| 26 |
-
// Open the conversion website
|
| 27 |
await page.goto("https://ogmp3.com", { waitUntil: "domcontentloaded" });
|
| 28 |
-
|
| 29 |
-
// Enter the YouTube link
|
| 30 |
await page.fill("#url", youtubeUrl);
|
| 31 |
|
| 32 |
-
// Intercept network responses to find the download URL
|
| 33 |
page.on("response", async (response) => {
|
| 34 |
const url = response.url();
|
| 35 |
if (url.includes("safestytmp3.cc") && url.includes("/download/")) {
|
|
@@ -37,12 +33,8 @@ async function getDownloadLink(youtubeUrl) {
|
|
| 37 |
}
|
| 38 |
});
|
| 39 |
|
| 40 |
-
// Click the convert button
|
| 41 |
await page.click("#convert-button");
|
| 42 |
-
|
| 43 |
-
// Wait up to 15 seconds for the link to appear
|
| 44 |
await page.waitForTimeout(15000);
|
| 45 |
-
|
| 46 |
} catch (error) {
|
| 47 |
console.error("Error extracting download link:", error);
|
| 48 |
} finally {
|
|
@@ -60,10 +52,7 @@ async function downloadMP3(url) {
|
|
| 60 |
responseType: "stream",
|
| 61 |
});
|
| 62 |
|
| 63 |
-
// Default filename
|
| 64 |
let fileName = `download_${Date.now()}.mp3`;
|
| 65 |
-
|
| 66 |
-
// Extract filename from headers
|
| 67 |
const contentDisposition = response.headers["content-disposition"];
|
| 68 |
if (contentDisposition) {
|
| 69 |
const match = contentDisposition.match(/filename="?(.+?)"?$/);
|
|
@@ -71,29 +60,33 @@ async function downloadMP3(url) {
|
|
| 71 |
fileName = match[1];
|
| 72 |
}
|
| 73 |
}
|
| 74 |
-
|
| 75 |
-
// Ensure it ends with .mp3
|
| 76 |
if (!fileName.endsWith(".mp3")) {
|
| 77 |
fileName += ".mp3";
|
| 78 |
}
|
| 79 |
|
| 80 |
const filePath = path.join(DOWNLOADS_DIR, fileName);
|
| 81 |
const writer = fs.createWriteStream(filePath);
|
| 82 |
-
|
| 83 |
response.data.pipe(writer);
|
| 84 |
|
| 85 |
return new Promise((resolve, reject) => {
|
| 86 |
-
writer.on("finish", () =>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
writer.on("error", reject);
|
| 88 |
});
|
| 89 |
-
|
| 90 |
} catch (error) {
|
| 91 |
console.error("Error downloading MP3:", error.message);
|
| 92 |
return null;
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
-
// API Endpoint
|
| 97 |
app.get("/api/q", async (req, res) => {
|
| 98 |
const youtubeUrl = req.query.url;
|
| 99 |
|
|
@@ -108,9 +101,8 @@ app.get("/api/q", async (req, res) => {
|
|
| 108 |
|
| 109 |
if (downloadLink) {
|
| 110 |
const fileName = await downloadMP3(downloadLink);
|
| 111 |
-
|
| 112 |
if (fileName) {
|
| 113 |
-
res.json({ success: true, file: `/downloads/${fileName}` });
|
| 114 |
} else {
|
| 115 |
res.status(500).json({ success: false, error: "Failed to download MP3" });
|
| 116 |
}
|
|
@@ -123,10 +115,8 @@ app.get("/api/q", async (req, res) => {
|
|
| 123 |
}
|
| 124 |
});
|
| 125 |
|
| 126 |
-
// Serve downloaded MP3s
|
| 127 |
app.use("/downloads", express.static(DOWNLOADS_DIR));
|
| 128 |
|
| 129 |
-
// Start Server
|
| 130 |
app.listen(PORT, () => {
|
| 131 |
console.log(`Server running on http://localhost:${PORT}`);
|
| 132 |
});
|
|
|
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
const PORT = 7860;
|
| 9 |
+
const DOMAIN = "https://reikerxx-ytm.hf.space";
|
| 10 |
const DOWNLOADS_DIR = path.join(__dirname, "downloads");
|
| 11 |
|
|
|
|
| 12 |
if (!fs.existsSync(DOWNLOADS_DIR)) {
|
| 13 |
fs.mkdirSync(DOWNLOADS_DIR, { recursive: true });
|
| 14 |
}
|
|
|
|
| 16 |
async function getDownloadLink(youtubeUrl) {
|
| 17 |
const browser = await chromium.launch({
|
| 18 |
headless: true,
|
| 19 |
+
executablePath: "/usr/bin/chromium",
|
| 20 |
args: ["--no-sandbox", "--disable-setuid-sandbox"]
|
| 21 |
});
|
| 22 |
const page = await browser.newPage();
|
| 23 |
let downloadLink = null;
|
| 24 |
|
| 25 |
try {
|
|
|
|
| 26 |
await page.goto("https://ogmp3.com", { waitUntil: "domcontentloaded" });
|
|
|
|
|
|
|
| 27 |
await page.fill("#url", youtubeUrl);
|
| 28 |
|
|
|
|
| 29 |
page.on("response", async (response) => {
|
| 30 |
const url = response.url();
|
| 31 |
if (url.includes("safestytmp3.cc") && url.includes("/download/")) {
|
|
|
|
| 33 |
}
|
| 34 |
});
|
| 35 |
|
|
|
|
| 36 |
await page.click("#convert-button");
|
|
|
|
|
|
|
| 37 |
await page.waitForTimeout(15000);
|
|
|
|
| 38 |
} catch (error) {
|
| 39 |
console.error("Error extracting download link:", error);
|
| 40 |
} finally {
|
|
|
|
| 52 |
responseType: "stream",
|
| 53 |
});
|
| 54 |
|
|
|
|
| 55 |
let fileName = `download_${Date.now()}.mp3`;
|
|
|
|
|
|
|
| 56 |
const contentDisposition = response.headers["content-disposition"];
|
| 57 |
if (contentDisposition) {
|
| 58 |
const match = contentDisposition.match(/filename="?(.+?)"?$/);
|
|
|
|
| 60 |
fileName = match[1];
|
| 61 |
}
|
| 62 |
}
|
|
|
|
|
|
|
| 63 |
if (!fileName.endsWith(".mp3")) {
|
| 64 |
fileName += ".mp3";
|
| 65 |
}
|
| 66 |
|
| 67 |
const filePath = path.join(DOWNLOADS_DIR, fileName);
|
| 68 |
const writer = fs.createWriteStream(filePath);
|
|
|
|
| 69 |
response.data.pipe(writer);
|
| 70 |
|
| 71 |
return new Promise((resolve, reject) => {
|
| 72 |
+
writer.on("finish", () => {
|
| 73 |
+
setTimeout(() => {
|
| 74 |
+
fs.unlink(filePath, (err) => {
|
| 75 |
+
if (!err) {
|
| 76 |
+
console.log(`Deleted: ${fileName}`);
|
| 77 |
+
}
|
| 78 |
+
});
|
| 79 |
+
}, 300000);
|
| 80 |
+
resolve(fileName);
|
| 81 |
+
});
|
| 82 |
writer.on("error", reject);
|
| 83 |
});
|
|
|
|
| 84 |
} catch (error) {
|
| 85 |
console.error("Error downloading MP3:", error.message);
|
| 86 |
return null;
|
| 87 |
}
|
| 88 |
}
|
| 89 |
|
|
|
|
| 90 |
app.get("/api/q", async (req, res) => {
|
| 91 |
const youtubeUrl = req.query.url;
|
| 92 |
|
|
|
|
| 101 |
|
| 102 |
if (downloadLink) {
|
| 103 |
const fileName = await downloadMP3(downloadLink);
|
|
|
|
| 104 |
if (fileName) {
|
| 105 |
+
res.json({ success: true, file: `${DOMAIN}/downloads/${fileName}` });
|
| 106 |
} else {
|
| 107 |
res.status(500).json({ success: false, error: "Failed to download MP3" });
|
| 108 |
}
|
|
|
|
| 115 |
}
|
| 116 |
});
|
| 117 |
|
|
|
|
| 118 |
app.use("/downloads", express.static(DOWNLOADS_DIR));
|
| 119 |
|
|
|
|
| 120 |
app.listen(PORT, () => {
|
| 121 |
console.log(`Server running on http://localhost:${PORT}`);
|
| 122 |
});
|