| const express = require("express"); |
| const { chromium } = require("playwright-core"); |
| const fs = require("fs"); |
| const path = require("path"); |
| const axios = require("axios"); |
|
|
| const app = express(); |
| const PORT = 7860; |
| const DOMAIN = "https://reikerxx-ytm.hf.space"; |
| const DOWNLOADS_DIR = path.join(__dirname, "downloads"); |
|
|
| if (!fs.existsSync(DOWNLOADS_DIR)) { |
| fs.mkdirSync(DOWNLOADS_DIR, { recursive: true }); |
| } |
|
|
| async function getDownloadLink(youtubeUrl) { |
| const browser = await chromium.launch({ |
| headless: true, |
| executablePath: "/usr/bin/chromium", |
| args: ["--no-sandbox", "--disable-setuid-sandbox"] |
| }); |
| const page = await browser.newPage(); |
| let downloadLink = null; |
|
|
| try { |
| await page.goto("https://ogmp3.com", { waitUntil: "domcontentloaded" }); |
| await page.fill("#url", youtubeUrl); |
|
|
| page.on("response", async (response) => { |
| const url = response.url(); |
| if (url.includes("safestytmp3.cc") && url.includes("/download/")) { |
| downloadLink = url; |
| } |
| }); |
|
|
| await page.click("#convert-button"); |
| await page.waitForTimeout(15000); |
| } catch (error) { |
| console.error("Error extracting download link:", error); |
| } finally { |
| await browser.close(); |
| } |
|
|
| return downloadLink; |
| } |
|
|
| async function downloadMP3(url) { |
| try { |
| const response = await axios({ |
| url, |
| method: "GET", |
| responseType: "stream", |
| }); |
|
|
| let fileName = `download_${Date.now()}.mp3`; |
| const contentDisposition = response.headers["content-disposition"]; |
| if (contentDisposition) { |
| const match = contentDisposition.match(/filename="?(.+?)"?$/); |
| if (match) { |
| fileName = match[1]; |
| } |
| } |
| if (!fileName.endsWith(".mp3")) { |
| fileName += ".mp3"; |
| } |
|
|
| const filePath = path.join(DOWNLOADS_DIR, fileName); |
| const writer = fs.createWriteStream(filePath); |
| response.data.pipe(writer); |
|
|
| return new Promise((resolve, reject) => { |
| writer.on("finish", () => { |
| setTimeout(() => { |
| fs.unlink(filePath, (err) => { |
| if (!err) { |
| console.log(`Deleted: ${fileName}`); |
| } |
| }); |
| }, 300000); |
| resolve(fileName); |
| }); |
| writer.on("error", reject); |
| }); |
| } catch (error) { |
| console.error("Error downloading MP3:", error.message); |
| return null; |
| } |
| } |
|
|
| app.get("/api/q", async (req, res) => { |
| const youtubeUrl = req.query.url; |
|
|
| if (!youtubeUrl) { |
| return res.status(400).json({ success: false, error: "Missing YouTube URL" }); |
| } |
|
|
| console.log(`Processing: ${youtubeUrl}`); |
|
|
| try { |
| const downloadLink = await getDownloadLink(youtubeUrl); |
|
|
| if (downloadLink) { |
| const fileName = await downloadMP3(downloadLink); |
| if (fileName) { |
| res.json({ success: true, file: `${DOMAIN}/downloads/${fileName}` }); |
| } else { |
| res.status(500).json({ success: false, error: "Failed to download MP3" }); |
| } |
| } else { |
| res.status(404).json({ success: false, error: "No MP3 link found" }); |
| } |
| } catch (error) { |
| console.error("Error processing YouTube link:", error); |
| res.status(500).json({ success: false, error: "Internal Server Error" }); |
| } |
| }); |
|
|
| app.use("/downloads", express.static(DOWNLOADS_DIR)); |
|
|
| app.listen(PORT, () => { |
| console.log(`Server running on http://localhost:${PORT}`); |
| }); |