forked from tuksik/spotify-downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (79 loc) · 2.79 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
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
var spotifyPlaylist = require('spotify-playlist');
var toUrl = require('./uri-url');
var download = require('./download');
var spotify = require('spotify-data');
var fs = require('fs');
var EasyZip = require('easy-zip').EasyZip;
// uriIN: Spotify playlist URI
// done: Callback when download to uriIN folder is done
// update: Called during download progress
module.exports = function(uriIN, done, update) {
var uri = uriIN;
console.log(uri);
var playlistId = Math.floor(Math.random() * 1000000000) + '';
spotifyPlaylist.playlistUri(uri, function(err, data){
console.log("Got playlist");
if (err == "Fail!"){
console.log(callback);
console.log(update);
update("Invalid playlist");
done("Invalid playlist");
return;
}
var tracks = data.playlist.tracks
fs.mkdir("./" + playlistId, function(err)
{
if (err == null){
downloadTracks(playlistId, tracks);
}else {
done("Directory creation failed");
}
})
});
function downloadTracks(playlistId, tracks)
{
var total = tracks.length
var done = 0
tracks.forEach((track, index)=>{
spotify.track(track.id, function(err, track2){
if (track2 && track2.track.album){
track.album = track2.track.album
}else {
console.log("No album found");
track.album = {}
}
toUrl(track, function(err, data){
if (data){
download(index, playlistId, track, data.id, function (err){
done++;
console.log(done + " out of " + total + " downloaded");
update(done + " out of " + total + " downloaded");
if (err){
console.log("Download failed for " + track.name);
}
if (done == total){
zip(playlistId)
}
})
}else {
console.log("Youtube search failed for " + track.name);
done++;
if (done == total){
zip(playlistId)
}
}
});
});
});
}
function zip(playlistId)
{
update("Sucesss!");
done(null);
// console.log("WE DONE!");
// var zip = new EasyZip();
// zip.zipFolder( './865008045',function(){
// zip.writeToFile( playlistId + '.zip');
// }, {rootFolder: 'easy-zip6'});
}
}