Spaces:
Paused
Paused
Create spotify.js
Browse files- spotify.js +98 -0
spotify.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const axios = require('axios');
|
| 3 |
+
const app = express();
|
| 4 |
+
const PORT = process.env.PORT || 7860;
|
| 5 |
+
|
| 6 |
+
const SPOTIFY_API = {
|
| 7 |
+
BASE: 'https://api.spotify.com/v1',
|
| 8 |
+
AUTH: 'https://accounts.spotify.com/api/token',
|
| 9 |
+
CLIENT_ID: 'b0cdfaef5b0b401299244ef88df29ffb',
|
| 10 |
+
CLIENT_SECRET: '3e5949b78a214aecb2558b861911c1a9'
|
| 11 |
+
};
|
| 12 |
+
|
| 13 |
+
const FAB_DL_API = 'https://api.fabdl.com';
|
| 14 |
+
|
| 15 |
+
let spotifyToken = null;
|
| 16 |
+
let tokenExpiry = null;
|
| 17 |
+
|
| 18 |
+
async function getSpotifyToken() {
|
| 19 |
+
if (spotifyToken && Date.now() < tokenExpiry) {
|
| 20 |
+
return spotifyToken;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
try {
|
| 24 |
+
const authString = Buffer.from(`${SPOTIFY_API.CLIENT_ID}:${SPOTIFY_API.CLIENT_SECRET}`).toString('base64');
|
| 25 |
+
const response = await axios.post(SPOTIFY_API.AUTH, 'grant_type=client_credentials', {
|
| 26 |
+
headers: {
|
| 27 |
+
'Content-Type': 'application/x-www-form-urlencoded',
|
| 28 |
+
'Authorization': `Basic ${authString}`
|
| 29 |
+
}
|
| 30 |
+
});
|
| 31 |
+
spotifyToken = response.data.access_token;
|
| 32 |
+
tokenExpiry = Date.now() + (response.data.expires_in * 1000);
|
| 33 |
+
return spotifyToken;
|
| 34 |
+
} catch (error) {
|
| 35 |
+
console.error(error);
|
| 36 |
+
throw new Error('Failed to get Spotify token');
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
function formatDuration(ms) {
|
| 41 |
+
const minutes = Math.floor(ms / 60000);
|
| 42 |
+
const seconds = Math.floor((ms % 60000) / 1000);
|
| 43 |
+
return `${minutes}:${seconds.toString().padStart(2, '0')}`;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
app.get('/api/spotify', async (req, res) => {
|
| 47 |
+
try {
|
| 48 |
+
const { url, search } = req.query;
|
| 49 |
+
|
| 50 |
+
if (url) {
|
| 51 |
+
const token = await getSpotifyToken();
|
| 52 |
+
const trackId = url.split('/').pop().split('?')[0];
|
| 53 |
+
const trackResponse = await axios.get(`${SPOTIFY_API.BASE}/tracks/${trackId}`, {
|
| 54 |
+
headers: { 'Authorization': `Bearer ${token}` }
|
| 55 |
+
});
|
| 56 |
+
|
| 57 |
+
const fabResponse = await axios.get(`${FAB_DL_API}/spotify/get?url=${url}`);
|
| 58 |
+
const downloadResponse = await axios.get(
|
| 59 |
+
`${FAB_DL_API}/spotify/mp3-convert-task/${fabResponse.data.result.gid}/${fabResponse.data.result.id}`
|
| 60 |
+
);
|
| 61 |
+
|
| 62 |
+
res.json({
|
| 63 |
+
title: trackResponse.data.name,
|
| 64 |
+
artist: trackResponse.data.artists.map(a => a.name).join(', '),
|
| 65 |
+
duration: formatDuration(trackResponse.data.duration_ms),
|
| 66 |
+
cover: trackResponse.data.album.images[0]?.url,
|
| 67 |
+
downloadUrl: `${FAB_DL_API}${downloadResponse.data.result.download_url}`
|
| 68 |
+
});
|
| 69 |
+
|
| 70 |
+
} else if (search) {
|
| 71 |
+
const token = await getSpotifyToken();
|
| 72 |
+
const searchResponse = await axios.get(
|
| 73 |
+
`${SPOTIFY_API.BASE}/search?q=${encodeURIComponent(search)}&type=track&limit=10`,
|
| 74 |
+
{ headers: { 'Authorization': `Bearer ${token}` } }
|
| 75 |
+
);
|
| 76 |
+
|
| 77 |
+
const tracks = searchResponse.data.tracks.items.map(track => ({
|
| 78 |
+
id: track.id,
|
| 79 |
+
title: track.name,
|
| 80 |
+
artist: track.artists.map(a => a.name).join(', '),
|
| 81 |
+
duration: formatDuration(track.duration_ms),
|
| 82 |
+
cover: track.album.images[0]?.url,
|
| 83 |
+
url: track.external_urls.spotify
|
| 84 |
+
}));
|
| 85 |
+
|
| 86 |
+
res.json(tracks);
|
| 87 |
+
} else {
|
| 88 |
+
res.status(400).json({ error: 'Missing url or search parameter' });
|
| 89 |
+
}
|
| 90 |
+
} catch (error) {
|
| 91 |
+
console.error(error);
|
| 92 |
+
res.status(500).json({ error: 'Internal server error' });
|
| 93 |
+
}
|
| 94 |
+
});
|
| 95 |
+
|
| 96 |
+
app.listen(PORT, () => {
|
| 97 |
+
console.log(`Server running on port ${PORT}`);
|
| 98 |
+
});
|