-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
539 lines (504 loc) · 17 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// all the dependencies
var yt = require('googleapis').youtube('v3');
var ytApi = "AIzaSyCb4WEODouAbNve1H0HhqrYfcnh7SGCsf8";
var fs = require("fs-extra");
var https = require("https");
var request = require("request");
var termList = require("./termList.json");
var colors = require('colors/safe');
var downloaded = require("./downloadedPlaylists.json");
// colors theme
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red',
link: ["underline", "green"]
});
var ffmetadata = require("ffmetadata");
var outputDir = __dirname;
String.prototype.contains = function(p) {
return (this.indexOf(p) > -1);
}
var spotify = new spotifyApi("f7fd010eb8204b7aabe4077d249ba905", "dfcc36a349bb4219baade50a51381b9c", spotifyCallback);
// get the spotify token from the user
var app = require('http').createServer(handler);
var io = require('socket.io')(app);
app.listen(8000);
function handler(req, res) {
fs.readFile(__dirname + "/index.html", function(err, data) {
if (err) {
res.writeHead(500);
return res.end(colors.error("error loading index.html"));
}
res.writeHead(200);
res.end(data);
});
}
io.on("connection", function (socket) {
socket.on("token", function(data) {
spotify.oauthCode = data.code;
spotify.getToken();
});
});
console.log(colors.help("click link to login to spotify"));
console.log(colors.link("https://accounts.spotify.com/authorize/?client_id="+spotify.client_id+"&response_type=code&redirect_uri=http://localhost:8000"));
// getting the spotify link
var args = process.argv;
var linkData = {playlist: [], album: [], artist: []};
// check the arguments given to the script
for (var i = 2; i < args.length; i++) {
if (args[i].contains("playlist")) {
var username = args[i].substring(args[i].indexOf("user")+5, args[i].length);
username = username.substring(0, username.indexOf("/"));
playlist = args[i].substring(args[i].indexOf("playlist")+9, args[i].length);
linkData.playlist.push({username: username, playlist: playlist});
}
if (args[i].contains("album")) {
var album = args[i].substring(args[i].indexOf("album")+6, args[i].length);
linkData.album.push({album: album});
}
if (args[i].contains("artist")) {
var artist = args[i].substring(args[i].indexOf("artist")+7, args[i].length);
linkData.artist.push({artist: artist});
}
if (args[i] === "--update" || args[i] === "-u") {
// updates the playlists. this will download any new songs in the downloaded playlists.
linkData["update"] = true;
}
if (args[i] === "-o") {
outputDir = args[i+1];
}
}
// checks if directory covers exists and if not it will make it
fs.existsSync(outputDir + "/covers") || fs.mkdirSync(outputDir + "/covers");
var spotifyData = [];
var downloadedData = [];
var totalSongs = 0;
var totalDownloaded = 0;
var songCount = 0; // for debugging
var youtubeSearches = 0; // for debugging
var artistCalls = 0; // for debugging
function spotifyCallback(res, code, err) {
if (code == "playlistTracks") {
if (res.next) {
var playlist = res.href.substring(res.href.indexOf("playlists")+10, res.href.length);
playlist = playlist.substring(0, playlist.indexOf("/"));
var username = res.href.substring(res.href.indexOf("users")+6, res.href.length);
username = username.substring(0, username.indexOf("/"));
spotify.getPlaylistTracks(playlist, username, {offset:res.offset+100, limit:100});
} else {
totalSongs += res.total;
}
for (var i = 0; i < res.items.length; i++) {
// if it finds the song, don't download it
if (downloaded.downloaded.find(x => x.artist === res.items[i].track.artists[0].name && x.song === res.items[i].track.name)) {
found = true;
totalSongs--;
checkProgress();
}
if (!found) {
songCount++;
spotifyData.push({
"artist": res.items[i].track.artists[0].name,
"song": res.items[i].track.name,
"album": res.items[i].track.album.name,
"cover": res.items[i].track.album.images[0].url,
"id": res.items[i].track.artists[0].id,
"track": null,
"date": null
});
downloadedData.push({"artist": res.items[i].track.artists[0].name, "song": res.items[i].track.name});
spotify.getArtist(res.items[i].track.artists[0].id);
}
}
} else if (code == "artistAlbums") {
for (var i = 0; i < res.items.length; i++) {
spotify.getAlbum(res.items[i].id);
}
} else if(code == "albumTracks") {
for (var i = 0; i < res.tracks.items.length; i++) {
var found = false;
for (var j = 0; j < spotifyData.length; j++) {
if (spotifyData[j].id == res.artists[0].id && spotifyData[j].song == res.tracks.items[i].name) {
found = true;
}
}
// if it finds the song, don't download it
if (downloaded.downloaded.find(x => x.artist === res.artists[0].name && x.song === res.tracks.items[i].name)) {
found = true;
checkProgress();
}
if (!found) {
spotifyData.push({
"artist": res.artists[0].name,
"song": res.tracks.items[i].name,
"track": res.tracks.items[i].track_number+"/"+res.tracks.total,
"album": res.name,
"date": res.release_date,
"cover": res.images[0].url,
"id": res.artists[0].id
});
downloadedData.push({"artist": res.items[i].track.artists[0].name, "song": res.items[i].track.name});
totalSongs++;
spotify.getArtist(res.artists[0].id);
}
}
} else if (code == "token") {
if (linkData.playlist[0]) {
for (var i = 0; i < linkData.playlist.length; i++) {
spotify.getPlaylistTracks(linkData.playlist[i]["playlist"], linkData.playlist[i]["username"]);
}
}
if(linkData.album[0]) {
for (var i = 0; i < linkData.album.length; i++) {
spotify.getAlbum(linkData.album[i]["album"]);
}
}
if (linkData.artist[0]) {
for (var i = 0; i < linkData.artist.length; i++) {
spotify.getArtistAlbums(linkData.artist[i]["artist"]);
}
}
if (linkData.update) {
// downloads all the playlists you have downloaded
for (var i = 0; i < downloaded.playlist.length; i++) {
spotify.getPlaylistTracks(downloaded.playlist[i].playlist, downloaded.playlist[i].username);
}
for (var i = 0; i < downloaded.album.length; i++) {
spotify.getAlbum(downloaded.album[i].album);
}
for (var i = 0; i < downloaded.artist.length; i++) {
spotify.getArtistAlbums(downloaded.artist[i].artist);
}
}
} else if (code == "artist") {
artistCalls++;
var spotData = spotifyData.find(x => x.id === res.id); // find the item
if (!spotData) {
console.log("skipped " + res.name);
return
}
spotifyData.splice(spotifyData.indexOf(spotData), 1); // remove it from the array
if (res.genres[0] != undefined) {
spotData["genre"] = res.genres[0];
} else {
spotData["genre"] = "";
}
// if it finds the song, don't download it
if (downloaded.downloaded.find(x => x.artist === spotData.artist && x.song === spotData.song)) {
totalSongs--;
checkProgress();
return
}
// do a yt search
(function(spotData) {
ytSearch(spotData.artist+" - "+spotData.song, 5, ytApi, result => {
youtubeSearches++;
loop1:
for (var j = 0; j < result.items.length; j++) {
if (result.items[j].id.kind === 'youtube#video') {
loop2:
for (var k = 0; k < termList.length; k++) {
if (result.items[j].snippet.title.toLowerCase().contains(termList[k]) || result.items[j].snippet.channelTitle.toLowerCase().contains(termList[k]) || result.items[j].snippet.channelTitle.toLowerCase().contains(spotData.artist)) {
var best = j;
break loop1;
}
}
}
}
if (!best) {
for (var j = 0; j < result.items.length; j++) {
if (result.items[j].id.kind === "youtube#video") {
var best = j;
break;
}
}
}
console.log("https://www.youtube.com/watch?v="+colors.verbose(result.items[best].id.videoId));
// download youtube mp3
var title = spotData.artist+" - "+spotData.song;
console.log(colors.verbose(title));
downloadMp3("https://www.youtube.com/watch?v="+result.items[best].id.videoId, outputDir+"/Music/"+title, (error, stdout, stderr, path)=> {
console.log(colors.info(title+" downloaded"));
//download cover
downloadImg(spotData.cover, outputDir+"/covers/"+title, (dir) => {
console.log(colors.info("downloaded cover"));
var options = {
"attachments": [dir]
};
var metadata = {
"artist": spotData.artist,
"album": spotData.album,
"title": spotData.song,
"genre": spotData.genre,
"track": spotData.track,
"date": spotData.date
}
ffmetadata.write(path, metadata, options, (err)=> {
if (err) {
console.log(colors.error("error writing metadata"));
} else {
console.log(colors.info("wrote metadata successfully"));
totalDownloaded++;
checkProgress();
}
});
});
});
}, i);
}(spotData));
}
}
function checkProgress() {
var percentage = totalDownloaded/totalSongs*100;
if (totalSongs === 0) {
percentage = 100;
}
io.emit("progress", percentage);
if (percentage == 100) {
console.log(colors.info("DONE downloading all files"));
if (linkData.update) {
console.log(colors.info("updated all the files"));
}
linkData.playlist.forEach(item => {
// if it doesn't exist, prevents doubles
if (!downloaded.playlist.find(x => x.playlist === item.playlist && x.username === item.playlist)) {
downloaded.playlist.push({"playlist": item.playlist, "username": item.username});
}
});
linkData.album.forEach(item => {
// if it doesn't exist, prevents doubles
if (!downloaded.album.find(x => x.album === item.album)) {
downloaded.album.push({album: item.album});
}
});
linkData.artist.forEach(item => {
// if it doesn't exist, prevents doubles
if (!downloaded.artist.find(x => x.artist === item.artist)) {
downloaded.artist.push({artist: item.artist});
}
});
downloadedData.forEach(item => {
downloaded.downloaded.push(item);
});
app.close();
fs.remove(outputDir+'/covers', (err)=>{
if (err) {
console.log(colors.error("couldn't delete cover folder"));
}
});
}
}
//path includes filename but excludes extension
function downloadMp3(ytLink, path, callback) {
var exec = require("child_process").exec;
var cmd = 'youtube-dl -x --audio-format mp3 -o "'+path+'.%(ext)s" '+ytLink;
exec(cmd, (error, stdout, stderr)=> {
if (!error && !stderr) {
callback(error, stdout, stderr, path+".mp3");
} else {
// brute force it
console.log(error);
console.log(stderr);
downloadMp3(ytLink, path, callback);
}
});
}
function ytSearch(searchTerm, maxResults, apiKey, callback) {
yt.search.list({
part: "snippet",
q: searchTerm,
maxResults: maxResults,
auth: apiKey
}, (err, result)=>{
if (err) {
console.log(err);
ytSearch(searchTerm, maxResults, apiKey, callback); // if error try again
} else {
callback(result);
return result;
}
});
}
// do not give dir an extension like .jpeg, gif or what ever, you can give it an name afterwards but no extension
function downloadImg(url, dir, callback){
request.head(url, function(err, res, body){
var ext = "."+res.headers['content-type'].substring("image/".length, res.headers['content-type'].length);
dir += ext;
request(url).pipe(fs.createWriteStream(dir)).on('close', function() {
callback(dir);
});
});
}
process.on("exit", () => {
fs.writeFileSync("downloadedPlaylists.json", JSON.stringify(downloaded));
console.log("wrote downloadedPlaylists.json file");
})
var apiErrors = 0;
// spotifyApi object
function spotifyApi(client_id, client_secret, callback) {
this.client_id = client_id;
this.client_secret = client_secret;
this.token;
this.oauthCode;
this.callback = callback;
this.timeoutTime = 10000; // if it gets an error wait this many seconds and try again.
}
spotifyApi.prototype.getToken = function() {
var data = {
'grant_type': "authorization_code",
'code': this.oauthCode,
'redirect_uri': "http://localhost:8000"
}
var options = {
url: "https://accounts.spotify.com/api/token",
method: "POST",
json: true,
form: data,
headers: {
'Authorization': "Basic "+encodeB64("f7fd010eb8204b7aabe4077d249ba905:dfcc36a349bb4219baade50a51381b9c")
}
};
request.post(options, (error, response, body) => {
if (!error && response.statusCode === 200) {
this.token = body.access_token;
this.callback(this.token, "token");
return this.token;
}
});
}
function encodeB64(s) {
return Buffer.from(s).toString("base64");
}
spotifyApi.prototype.getPlaylistTracks = function(playlistId, username, options) {
https.get({
host: "api.spotify.com",
path: "/v1/users/"+username+"/playlists/"+playlistId+"/tracks"+optionsToUriParams(options),
headers: {"Accept": "application/json", "Authorization": "Bearer "+this.token}}, (res)=>{
var pageData = "";
res.setEncoding("utf-8");
res.on('data', (chunk)=> {
pageData += chunk;
});
res.on('end', ()=> {
pageData = JSON.parse(pageData);
if (pageData.error && pageData.error.status === 429) {
console.log("error");
apiErrors++;
setTimeout(() => {
this.getPlaylistTracks(playlistId, username, options);
}, this.timeoutTime);
} else {
this.callback(pageData, "playlistTracks");
return pageData;
}
});
}
);
}
spotifyApi.prototype.getArtist = function(artistId, options) {
https.get({
host: "api.spotify.com",
path: "/v1/artists/"+artistId+optionsToUriParams(options),
headers: {"Accept": "application/json", "Authorization": "Bearer "+this.token}}, (res)=>{
var pageData = "";
res.setEncoding("utf-8");
res.on('data', (chunk)=> {
pageData += chunk;
});
res.on('end', ()=> {
pageData = JSON.parse(pageData);
if (pageData.error && pageData.error.status === 429) {
console.log("error");
apiErrors++;
setTimeout(() => {
this.getArtist(artistId, options);
}, this.timeoutTime);
} else {
this.callback(pageData, "artist");
return pageData;
}
});
}
);
}
spotifyApi.prototype.getArtistAlbums = function(artistId, options) {
https.get({
host: "api.spotify.com",
path: "https://api.spotify.com/v1/artists/"+artistId+"/albums"+optionsToUriParams(options),
headers: {"Accept": "application/json", "Authorization": "Bearer "+this.token}}, (res)=>{
var pageData = "";
res.setEncoding("utf-8");
res.on('data', (chunk)=> {
pageData += chunk;
});
res.on('end', ()=> {
pageData = JSON.parse(pageData);
if (pageData.error && pageData.error.status === 429) {
console.log("error");
apiErrors++;
setTimeout(() => {
this.getArtistAlbums(artistId, options);
}, this.timeoutTime);
} else {
this.callback(pageData, "artistAlbums");
return pageData;
}
});
}
);
}
spotifyApi.prototype.getAlbum = function(albumId, options) {
https.get({
host: "api.spotify.com",
path: "/v1/albums/"+albumId+optionsToUriParams(options),
headers: {"Accept": "application/json", "Authorization": "Bearer "+this.token}}, (res)=>{
var pageData = "";
res.setEncoding("utf-8");
res.on('data', (chunk)=> {
pageData += chunk;
});
res.on('end', ()=> {
pageData = JSON.parse(pageData);
if (pageData.error && pageData.error.status === 429) {
console.log("error");
apiErrors++;
setTimeout(() => {
this.getAlbum(albumId, options);
}, this.timeoutTime);
} else {
this.callback(pageData, "albumTracks");
return pageData;
}
});
}
);
}
// setInterval(() => {
// console.log(apiErrors, songCount, totalSongs, youtubeSearches, artistCalls);
// }, 10000);
function optionsToUriParams(options) {
if (!options) {
return ""
}
var uri = "?";
var counter = 0
for (var x in options) {
if (options.hasOwnProperty(x)) {
if (counter == 0) {
uri += x+"="+options[x];
} else {
uri += "&"+x+"="+options[x];
}
counter++;
}
}
return uri
}