spotify / index.js
Rianofc's picture
Update index.js
326cb11 verified
const express = require('express');
const axios = require('axios');
const NodeCache = require('node-cache');
const os = require('os');
const crypto = require('crypto');
const app = express();
const cache = new NodeCache({ stdTTL: 3600 }); // Cache 1 jam
const PORT = 7860;
app.use(express.json());
// Fungsi untuk mendapatkan informasi sistem
function getSystemInfo() {
return {
os: os.type(), // Jenis OS (Linux, Windows, dll.)
arch: os.arch(), // Arsitektur CPU (x64, arm, dll.)
cpu: os.cpus()[0].model, // Model CPU
cpuLoad: os.loadavg()[0].toFixed(2), // Load CPU (1 menit terakhir)
totalRAM: (os.totalmem() / 1024 / 1024).toFixed(2) + ' MB', // Total RAM
freeRAM: (os.freemem() / 1024 / 1024).toFixed(2) + ' MB', // RAM yang tersisa
uptime: (os.uptime() / 60).toFixed(2) + ' menit' // Waktu uptime sistem
};
}
// Endpoint utama untuk mendapatkan info sistem
app.get('/', (req, res) => {
res.json({
status: "Server berjalan",
systemInfo: getSystemInfo()
});
});
// Fungsi untuk mendapatkan file unduhan dari Spotymate
async function spotifydl(url) {
try {
const res = await axios.post('https://spotymate.com/api/download-track', { url });
return res.data.file_url;
} catch (error) {
throw new Error('Gagal mendapatkan URL unduhan');
}
}
// Fungsi untuk mendapatkan metadata lagu dari Spotymate
async function spotifyInfo(url) {
try {
const res = await axios.post('https://spotymate.com/api/get-metadata', { url });
return res.data.apiResponse.data;
} catch (error) {
throw new Error('Gagal mendapatkan metadata');
}
}
// Generate short token
function generateShortToken() {
return crypto.randomBytes(16).toString('hex');
}
// Endpoint GET untuk mengunduh lagu dari Spotify
app.get('/download', async (req, res) => {
const { url } = req.query;
if (!url) return res.status(400).json({ error: 'URL Spotify diperlukan' });
try {
const { data } = await axios.get(`https://backed-spotify.ex-project.my.id/download?url=${url}&signature=abcd5etyg2n89ewayyw`)
const metadata = await spotifyInfo(url);
const fileUrl = data.url
const shortToken = generateShortToken();
cache.set(shortToken, { fileUrl, metadata });
res.json({
shortToken,
downloadUrl: `https://rianofc-spotify.hf.space/spotify/result/${shortToken}`,
metadata
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Endpoint untuk mendapatkan buffer file dari short token
app.get('/spotify/result/:shortToken', async (req, res) => {
const { shortToken } = req.params;
const data = cache.get(shortToken);
if (!data) return res.status(404).json({ error: 'Short token tidak ditemukan atau kadaluarsa' });
try {
const fileBuffer = await axios.get(data.fileUrl, { responseType: 'arraybuffer' });
res.setHeader('Content-Type', 'audio/mpeg');
res.send(fileBuffer.data);
} catch (error) {
res.status(500).json({ error: 'Gagal mengambil file' });
}
});
app.listen(PORT, () => {
console.log(`Server berjalan di http://localhost:${PORT}`);
});