Spaces:
Paused
Paused
| const { exec } = require("child_process"); | |
| const path = require("path"); | |
| const fs = require("fs"); | |
| const COOKIES_PATH = "cookies/qq.txt"; | |
| const DOWNLOAD_DIR = path.join(__dirname, "tmp"); | |
| // Pastikan folder download ada | |
| if (!fs.existsSync(DOWNLOAD_DIR)) { | |
| fs.mkdirSync(DOWNLOAD_DIR, { recursive: true }); | |
| } | |
| // Fungsi untuk mendapatkan informasi lagu | |
| function getInfo(url) { | |
| return new Promise((resolve, reject) => { | |
| exec(`./yt-dlp -j --cookies ${COOKIES_PATH} "${url}"`, (error, stdout, stderr) => { | |
| if (error) { | |
| return reject(`Error: ${stderr || error.message}`); | |
| } | |
| try { | |
| const info = JSON.parse(stdout); | |
| resolve({ | |
| status: 200, | |
| title: info.title, | |
| artist: info.creators[0], | |
| duration: info.duration, | |
| thumbnail: info.thumbnai, | |
| formats: info.formats.map(f => ({ | |
| format_id: f.format_id, | |
| ext: f.ext, | |
| filesize: f.size, | |
| url: f.url | |
| })) | |
| }); | |
| } catch (e) { | |
| reject({ status: 401, message: e.message}); | |
| } | |
| }); | |
| }); | |
| } | |
| // Fungsi untuk mengunduh lagu | |
| function download(url) { | |
| return new Promise((resolve, reject) => { | |
| const command = `./yt-dlp -vU --cookies ${COOKIES_PATH} -f ba --extract-audio --audio-format mp3 -o "${DOWNLOAD_DIR}/%(title)s.%(ext)s" "${url}"`; | |
| exec(command, (error, stdout, stderr) => { | |
| if (error) { | |
| return reject(`Download Error: ${stderr || error.message}`); | |
| } | |
| resolve(`${DOWNLOAD_DIR}`); | |
| }); | |
| }); | |
| } | |
| module.exports = { | |
| getInfo, | |
| download | |
| } |