Download server.js from NexusV1/apl: direct link, hf CLI and curl.
- Browser
- Download file 3.92 kB
-
https://huggingface.co/spaces/NexusV1/apl/resolve/main/server.js
- Command line
-
hf download hf://spaces/NexusV1/apl/server.js
-
curl -L -o server.js https://huggingface.co/spaces/NexusV1/apl/resolve/main/server.js
3.92 kB
| require('dotenv').config(); | |
| const express = require('express'); | |
| const axios = require('axios'); | |
| const app = express(); | |
| const PORT = process.env.PORT || 7860; | |
| const clientId = process.env.SPOTIFY_CLIENT_ID; | |
| const clientSecret = process.env.SPOTIFY_CLIENT_SECRET; | |
| // Function to get Spotify API access token | |
| async function getAccessToken() { | |
| const authString = Buffer.from(`${clientId}:${clientSecret}`).toString('base64'); | |
| try { | |
| const response = await axios.post( | |
| 'https://accounts.spotify.com/api/token', | |
| 'grant_type=client_credentials', | |
| { | |
| headers: { | |
| 'Authorization': `Basic ${authString}`, | |
| 'Content-Type': 'application/x-www-form-urlencoded', | |
| } | |
| } | |
| ); | |
| return response.data.access_token; | |
| } catch (error) { | |
| console.error('Failed to get access token:', error.response?.data || error.message); | |
| throw error; | |
| } | |
| } | |
| // /search endpoint | |
| app.get('/search', async (req, res) => { | |
| const query = req.query.q; | |
| if (!query) { | |
| return res.status(400).json({ error: 'Missing query parameter q' }); | |
| } | |
| try { | |
| const token = await getAccessToken(); | |
| const response = await axios.get('https://api.spotify.com/v1/search', { | |
| params: { | |
| q: query, | |
| type: 'track', | |
| limit: 1, | |
| include_external: 'audio' | |
| }, | |
| headers: { | |
| 'Authorization': `Bearer ${token}` | |
| } | |
| }); | |
| const track = response.data.tracks.items[0]; | |
| if (!track) { | |
| return res.status(404).json({ error: 'No tracks found' }); | |
| } | |
| const result = { | |
| title: track.name, | |
| id: track.id, | |
| artists: track.artists.map(a => a.name), | |
| album: track.album.name, | |
| duration_seconds: Math.floor(track.duration_ms / 1000), | |
| popularity: track.popularity, | |
| release_date: track.album.release_date, | |
| spotify_url: track.external_urls.spotify, | |
| preview_available: Boolean(track.preview_url), | |
| explicit: track.explicit, | |
| album_type: track.album.album_type, | |
| total_tracks_in_album: track.album.total_tracks, | |
| track_number: track.track_number, | |
| isrc: track.external_ids.isrc, | |
| available_markets_count: track.available_markets.length | |
| }; | |
| res.json(result); | |
| } catch (error) { | |
| console.error('Error fetching track:', error.response?.data || error.message); | |
| res.status(500).json({ error: 'Failed to fetch track' }); | |
| } | |
| }); | |
| app.get('/download', async (req, res) => { | |
| const trackUrl = req.query.url; | |
| if (!trackUrl) { | |
| return res.status(400).json({ error: 'Missing track URL (param: url)' }); | |
| } | |
| try { | |
| const response = await axios.post( | |
| 'https://spotify.downloaderize.com/wp-json/spotify-downloader/v1/fetch', | |
| { | |
| type: 'song', | |
| url: trackUrl | |
| }, | |
| { | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Accept': '*/*', | |
| 'X-Requested-With': 'XMLHttpRequest', | |
| // Optional spoofed headers to bypass bot protection | |
| 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/114.0.0.0 Safari/537.36', | |
| 'Referer': 'https://spotify.downloaderize.com/', | |
| 'Origin': 'https://spotify.downloaderize.com' | |
| } | |
| } | |
| ); | |
| const result = response.data; | |
| if (!result.success) { | |
| return res.status(502).json({ error: 'Failed to retrieve download link' }); | |
| } | |
| res.json({ | |
| title: result.data.title, | |
| artist: result.data.artist, | |
| album: result.data.album, | |
| cover: result.data.cover, | |
| releaseDate: result.data.releaseDate, | |
| download: result.data.downloadLink | |
| }); | |
| } catch (err) { | |
| console.error('Download fetch error:', err.response?.data || err.message); | |
| res.status(500).json({ error: 'Internal server error while fetching download' }); | |
| } | |
| }); | |
| // Start server | |
| app.listen(PORT, () => { | |
| console.log(`Server running on port ${PORT}`); | |
| }); |