-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
604 lines (531 loc) · 18.1 KB
/
main.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//liberries
//loading the spotify wrapper
var SpotifyWebApi = require('spotify-web-api-node');
//file reading
var fs = require("fs");
//HTTP
const http = require("http");
const url = require("url")
const https = require("https");
//request
var req = require("request")
//async
var async = require("async")
//removeAccents
var removeAccents = require("remove-accents")
//string compare
var stringCompare = require("string-similarity")
//console input
var readline = require('readline-sync');
/*
██ ██ █████ ██████ ███████
██ ██ ██ ██ ██ ██ ██
██ ██ ███████ ██████ ███████
██ ██ ██ ██ ██ ██ ██
████ ██ ██ ██ ██ ███████
*/
//
// //variables
// var scopes = ['user-read-private', 'user-read-email'],
// redirectUri = 'http://localhost:8888/callback',
// clientId = '5ddacffa892542a08bd9927c226dad43',
// clientSecret = '45b90caa6aae43ddbd969bdc3982f5b4',
// state = 'some-state-of-my-choice',
// accessCode = '',
// refreshCode = '';
//
// //credentials for creation
// var credentials = {
// clientId : clientId,
// clientSecret : clientSecret,
// redirectUri : redirectUri
// }
//
// //temp code that was returned
// var code = ''
//
// // creating the wrapper app
// var spotifyApi = new SpotifyWebApi(credentials);
/*
███████ ██ ██ ███ ██ ██████ ████████ ██ ██████ ███ ██ ███████
██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ████ ██ ██
█████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██████ ██ ████ ██████ ██ ██ ██████ ██ ████ ███████
*/
//
// //checking if the data file for spotify cred info exists
// function checkSpotifyDataFile(callback) {
// //checking file exists
// fs.existsSync('spotifyData.json') ? loadSpotifyData(callback) : createSpotifyData()
// }
//
// //for when running for the first time
// //creating spotify data
// function createSpotifyData() {
//
// //get and print the url
// // Create the authorization URL
// var authorizeURL = spotifyApi.createAuthorizeURL(scopes, state);
//
// //console log url
// console.log("The authorization URL is: \n")
// console.log(authorizeURL)
// console.log("\n")
//
// //starting the server
// var server = http.createServer(function(request, response) {
// //responding on the page
// response.writeHead(200, {"Content-Type": "text/plain"});
// response.write("Thanks for authorizing, James!");
//
// //parsing the request url to get the code
// //getting the url from request
// //getting query from url
// var queryData = url.parse(request.url, true).query;
//
// //getting code data from queryData
// code = queryData.code
// // console.log("\n\n" + code + "\n\n" )
//
// //need to get access and refresh codes with our new code
// // Retrieve an access token and a refresh token
// spotifyApi.authorizationCodeGrant(code)
// .then(function(data) {
// // console.log('The token expires in ' + data.body['expires_in']);
// // console.log('The access token is ' + data.body['access_token']);
// // console.log('The refresh token is ' + data.body['refresh_token']);
//
// // Set the access token on the API object to use it in later calls
// spotifyApi.setAccessToken(data.body['access_token']);
// spotifyApi.setRefreshToken(data.body['refresh_token']);
//
// //storing access and refresh codes
// accessCode = data.body['access_token']
// refreshCode = data.body['refresh_token']
//
// //writing to file
// //storing in JSON
// var dataObject = JSON.stringify({accessCode : accessCode, refreshCode : refreshCode})
// fs.writeFile( "spotifyData.json", dataObject, "utf8", function(err) {
// if (!err) {
// console.log("\nSaved successfully\n")
//
// } else {
// console.log("\nNot saved successfully!\n")
// }
//
// //shutting down either way
// // Shutting down! run again
// server.shutdown(function() {
// console.log('Everything is cleanly shutdown.');
// console.log("Run again!")
// });
//
// } );
//
//
// }, function(err) {
// console.log('Something went wrong!', err);
// });
//
// response.end();
// //and close the server either way
// // server.close()
//
//
// })
//
// //for shutting down
// server = require('http-shutdown')(server);
//
// //listening server
// server.listen(8888)
//
// }
//
//
// //if it's already been run, load the data from the file
// function loadSpotifyData(callback) {
// // reading the saved spotify data
// var spotData = require("./spotifyData.json");
//
// //data is already parsed
// //read and store
// accessCode = spotData.accessCode
// refreshCode = spotData.refreshCode
//
// //set
// spotifyApi.setAccessToken(accessCode);
// spotifyApi.setRefreshToken(refreshCode);
//
// //refreshing codes
// // clientId, clientSecret and refreshToken has been set on the api object previous to this call.
// spotifyApi.refreshAccessToken()
// .then(function(data) {
// console.log('The access token has been refreshed!');
//
// // Save the access token so that it's used in future calls
// spotifyApi.setAccessToken(data.body['access_token']);
//
// //callback
// callback && callback()
// }, function(err) {
// console.log('Could not refresh access token', err);
// });
// }
//
//
// //this function handles paging through all of the spotify api returns with 100 songs each
// function getSongsHandler(tracksObj, user, id, index, max, callback, callbackFailure) {
// // Get tracks in a playlist from the index
// spotifyApi.getPlaylistTracks(user, id, { 'offset' : index, 'fields' : 'items' })
// .then(function(data) {
// //push array to tracksObj
// for (let song of data.body.items) {
// tracksObj.push(song)
// }
//
// //return through callback if index >= max
// if (index >= max) {
// callback && callback(tracksObj)
// } else {
// //if the index < max there's still songs
// //increment index and call function song
// index += 100
// //call this function again
// getSongsHandler(tracksObj, user, id, index, max, callback, callbackFailure)
//
// }
//
//
// }, function(err) {
// console.log('Something went wrong! in getSongsHandler', err);
//
// //callback failure
// //if no playlist edm for some reason
// //unncomment
// callbackFailure && callbackFailure(err)
// });
//
// }
//
// //processing converting all the song objects to the string to search deezer with
// function handleSongStrings(tracksObj) {
//
//
// //variables
// //array of songs + artists
// var songsArr = []
//
// for (var i = 0; i < tracksObj.length; i++) {
// // console.log(tracksObj[i])
// var tempSearchName = ""
// //getting the two artist names
// //storing temporarily
// var tempArtistName = "";
// //need to loop through them
// for (let iter of tracksObj[i].track.artists) {
// tempArtistName = tempArtistName.concat(iter.name + " ")
// }
//
// //temp edited name to remove unicode characters
// var tempEditedName = tracksObj[i].track.name + " " + tempArtistName
// tempEditedName = tempEditedName.replace(/["“‘”’’]/g, "'");
// tempEditedName = removeAccents.remove(tempEditedName)
// console.log(tempEditedName)
//
// //temp object that holds both normal song name and edited song name
// var tempObj = {rawSongName: tracksObj[i].track.name, editedSongName: tempEditedName}
//
// //pushing to songsArr
// songsArr.push(tempObj)
//
// }
//
// return songsArr;
//
// }
//
// //get playlist specified in argument from user
// function getPlaylist(user, playlistName, callback) {
//
// //variables for this function
// //id of the playlist
// var playlistID;
// //how many songs there are, to page through them
// var songCount;
// //the raw objects of individual tracks from the playlist
// var trackObjects = [];
//
// // Get my playlists user jolaroux
// spotifyApi.getUserPlaylists(user)
// .then(function(data) {
// // console.log('Retrieved playlists', data.body.items);
// // console.log(data.body.items.length)
//
// //get ID of playlist EDM
// for (let playlist of data.body.items) {
// //if it's the right playlist
// if (playlist.name == playlistName) {
// //have the id of playlist now
// playlistID = playlist.id
// //have song count too
// songCount = playlist.tracks.total
// console.log(songCount + " songs in playlist " + playlistName)
//
// }
// }
//
//
// //getting tracks in playlist
// //need to loop through songCount and call get100Songs until the playlist empty
// var index = 0;
// //storing the song objects strings of song + artist
// var songObjects = [];
// //tracksObj, user, id, index, max, callback, callbackFailure
// getSongsHandler(trackObjects, user, playlistID, index, songCount,
//
// //callback success
// function(trackObjects) {
//
// //formatting the strings to search deezer
// songObjects = handleSongStrings(trackObjects)
//
// //printing for now
// // for (let o of songObjects) {
// // console.log(o)
// // }
// writeTo(songObjects, './spotifySongs.json')
//
// //pass back through callback
// callback && callback(songObjects)
//
// },
//
// //callback failure
// function (err) {
// console.log(err)
// }
// )
//
//
// },function(err) {
// console.log('Something went wrong!', err);
//
// //callback failure
// //if no playlist edm for some reason
// //unncomment
// callbackFailure && callbackFailure()
//
// });
//
// }
//writes array to file sent
function writeTo(arr, file) {
fs.writeFile(file, JSON.stringify(arr), 'utf8', function(err) {
if (err) {
console.log(err)
}
})
}
//this searches deezer using an HTTP GET
function searchDeezer(searchString, callback) {
console.log("")
//url is
//https://api.deezer.com/search?q=eminem
var searchUrl = "https://api.deezer.com/search?q=";
//add in searchString
var newSearchUrl = searchUrl + searchString.editedSongName
//remove unicode stupid fucking quotes
newSearchUrl.replace(/["“‘”’]/g, "'");
newSearchUrl = removeAccents.remove(newSearchUrl)
//error or not
var err = false;
//no match or not
var noMatch = false;
//data from search
var data;
var title;
//full data of object
var fulldata;
//comparing two strings result
var sComparePercent
console.log(newSearchUrl)
//try and catch because fuck this shit
try {
//searching
req.get(newSearchUrl, (error, response, body) => {
//full data
fulldata = ""
//parsing
let json = JSON.parse(body);
//has data and total
//if there's no search result
if (json.total == 0) {
// console.log("No results for " + searchString)
data = searchString
noMatch = true
} else {
//storing data from track object
title = json.data[0].title
data = json.data[0].link
fulldata = json.data[0]
// console.log(fulldata)
//comparing strings
sComparePercent = stringCompare.compareTwoStrings(title, searchString.rawSongName)
// console.log("Comparing: ")
// console.log(title + "\nand\n" + searchString.rawSongName)
}
// console.log(data);
// let item = {index: index, json: json}
callback && callback(data, title, searchString.editedSongName, sComparePercent, noMatch, fulldata, err)
});
} catch (e) {
console.log("ERROR")
console.log(e)
callback && callback(data, fulldata, noMatch, e)
}
}
//this handles searching deezer for all the songs
function handleDeezer(songs) {
//TEMPORARY
//loading the songs from spotifySongs.txt
// var songs = require('./spotifySongs2.json')
//variables
var index = 0
//songs from deezer
var deezSongs = {}
var deezSongsArr = []
//songs with no match
var noMatches = {}
var noMatchesArr = []
//songs that errored
var errorSongs = {}
//async calling it
async.forEachOfSeries(songs,
//run with every item
function(value, key, callback) {
//call the searchDeezer function
searchDeezer(value, function(link, title, origTitle, comparePercent, noMatch, fulldata, err) {
//if it succeeded in searching
if (!err) {
//if there WAS a match
if (!noMatch) {
//setting
deezSongs[key] = {link: link, origTitle: origTitle, title: title, artist: fulldata.artist.name, percent: comparePercent}
deezSongsArr.push({link: link, origTitle: origTitle, title: title, artist: fulldata.artist.name, percent: comparePercent})
} else {
//if it did NOT have a match
// noMatches.push(title)
noMatches[key] = origTitle
noMatchesArr.push({origTitle: origTitle})
}
} else {
//if it ERRORED
// errorSongs.push(title)
errorSongs[key] = origTitle
}
//calling back so async knows
callback()
})
},
//called when everything is done or if there's an error
function() {
//going through all the songs that have matches one by one and choosing whether to
//download that one or not.
//need to print the given name and the return title + artist and a yes / no option
//var name = readline.question("What is your name?");
// syntax for reading input ^
//object to store the YES songs in
yesSongsToDownload = []
//object to store NO's in
noSongsToDownload = []
//figuring out whether to download each song or not!
for (let song of deezSongsArr) {
//get input for each song
// string to ask in the question
var promptString = "\n\nGiven: \n\t" + song.origTitle + "\n" + "Deezer Search: \n\t" + song.title + " " + song.artist +"\nEnter to download, anything else to skip:\n"
var toDownload = readline.question(promptString)
//seeing if we should download the song or not
if (toDownload.length == 0) {
//add to yesSongsToDownload
yesSongsToDownload.push(song)
} else {
noSongsToDownload.push(song)
}
}
//adding all the no matches to noSongsToDownload
noSongsToDownload = noSongsToDownload.concat(noMatches)
//can now export a file of links of songs to download
//need to make a string of all the links to write to the file
var largeString = ""
//filling the string
for (let song of yesSongsToDownload) {
largeString = largeString + song.link + "\n"
}
//writing to the file!
//
// you are the biggest retard on the planet
// you need to write the right file you fucking idiot
// like really did you need this explained to you
// i hate you so much
fs.writeFile("./SMLoadr/downloadLinks.txt", largeString, 'utf8', function(err) {
console.log(largeString)
if (err) {
console.log(err)
}
})
//now do the same with all the no matches
var largeNoMatchString = ""
//filling the string
for (let song of noSongsToDownload) {
largeNoMatchString = largeNoMatchString + song.origTitle + "\n"
}
//writing to the file!
fs.writeFile("DeezerNOMatches.txt", largeNoMatchString, 'utf8', function(err) {
if (err) {
console.log(err)
}
})
/*
//logging the matched songs
console.log("MATCHES FOR:")
console.log(deezSongs)
console.log("\n\n")
//logging errors
console.log("ERROR FOR:")
console.log(errorSongs)
console.log("\n\n")
//logging no matches
console.log("NO MATCHES FOR:")
console.log(noMatches)
*/
}
)
}
/*
███ ███ █████ ██ ███ ██
████ ████ ██ ██ ██ ████ ██
██ ████ ██ ███████ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ████
*/
//getting the process started of checking if the data exists or not
//if it doesn't then it's created and saved and if it does it's loaded
//call the next function with callback
//UNCOMMENT
//testing with spotify class
//importing
const HandleSpotify = require('./spotify.js')
const spotifyHandler = new HandleSpotify()
spotifyHandler.checkSpotifyDataFile(function() {
//getting the playlist xxx
spotifyHandler.getPlaylist('jolaroux', /*'rap'*/ 'rap', function(songsArr) {
//callback success with array of edited songs
handleDeezer(songsArr)
})
//deezer's api
// handleDeezer()
})
// handleDeezer()