-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
36 lines (34 loc) · 1.19 KB
/
index.js
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
const SpotifyWebApi = require("spotify-web-api-node");
const YouTube = require("ytsr");
const fs = require("fs");
const ytdl = require("ytdl-core");
const config = require("./config");
const spotifyApi = new SpotifyWebApi({
clientId: config.spotifyClientId,
});
spotifyApi.setAccessToken(config.spotifyAccessToken);
console.log(`Starting Download, This may take a while...`);
spotifyApi.getPlaylistTracks(config.spotifyPlaylistId).then((data) => {
data.body.items.map((item, index) =>
YouTube(item.track.name + config.additionalSearchTerm, {
limit: 1,
filter: "audioonly",
}).then((res) => {
const stream = ytdl(res.items[0].url);
stream.pipe(fs.createWriteStream(`${config.folderName}/${index}.mp4`));
stream.on("end", () => {
fs.rename(
`${config.folderName}/${index}.mp4`,
`${config.folderName}/${item.track.name.replace(
":",
"-"
)}.mp3` /* since ":" is not considered valid */,
() => {
console.log(`Successfully downloaded ${item.track.name}[${index}]`);
}
);
stream.on("error", (err) => console.error(`Encountered error: ${err}`));
});
})
);
});