scraper / dl /spotifydl.js
R-Kentaren's picture
Rename spotifydl.js to dl/spotifydl.js
350edae verified
const axios = require('axios');
const cheerio = require('cheerio');
async function spotifydl(url) {
try {
if (!url.includes('open.spotify.com')) throw new Error('Invalid url.');
const rynn = await axios.get('https://spotdl.io/', {
headers: {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}
});
const $ = cheerio.load(uzip.data);
const api = axios.create({
baseURL: 'https://spotdl.io',
headers: {
cookie: uzip.headers['set-cookie'].join('; '),
'content-type': 'application/json',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'x-csrf-token': $('meta[name="csrf-token"]').attr('content')
}
});
const [{ data: meta }, { data: dl }] = await Promise.all([
api.post('/getTrackData', { spotify_url: url }),
api.post('/convert', { urls: url })
]);
return {
...meta,
download_url: dl.url
};
} catch (error) {
throw new Error(error.message);
}
}
// Usage:
const resp = await spotifydl('https://open.spotify.com/track/6yID3RbYKiwn2p2LPz0OkK');
console.log(resp);