File size: 3,918 Bytes
828079c 0957d70 828079c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | 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}`);
}); |