-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
522 lines (484 loc) · 13.7 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
#! /usr/bin/env node
'use strict';
const program = require('commander');
const spotify = require('spotify-web-api-node');
const prompt = require('prompt');
const parseSearchResults = require('./parsers/');
const printer = require('./printers/');
const spotifyClient = require('./osascripts/');
const nconf = require('nconf');
const path = require('path');
const readlineSync = require('readline-sync');
const fetchLyrics = require('./fetch_lyrics').fetchLyrics;
const version = require('./package.json').version;
const semver = require('semver');
const os = require('os');
const CONFIG_PATH = path.join(os.homedir(),'/.spotify-cli-config.json');
nconf.env().file(CONFIG_PATH);
// need client access token for genius
let lyricist = require('lyricist/node6');
let GENIUS_API_KEY = nconf.get('GeniusAPIClientKey');
let GENIUS_API_KEY_SET = GENIUS_API_KEY !== '' && GENIUS_API_KEY;
if(GENIUS_API_KEY_SET){
lyricist = new lyricist(GENIUS_API_KEY);
}
let SPOTIFY_CLIENT_ID = nconf.get('spotifyClientID');
let SPOTIFY_CLIENT_ID_SET = SPOTIFY_CLIENT_ID !== '' && SPOTIFY_CLIENT_ID;
let SPOTIFY_CLIENT_SECRET = nconf.get('spotifyClientSecret');
let SPOTIFY_CLIENT_SECRET_SET = SPOTIFY_CLIENT_SECRET !== '' && SPOTIFY_CLIENT_SECRET;
let SPOTIFY_USERNAME = nconf.get('spotifyUsername');
let SPOTIFY_USERNAME_SET = SPOTIFY_USERNAME !== '' && SPOTIFY_USERNAME;
let spotifyApi = null;
const initSpotifyApi = (client_id, client_secret) => {
return new spotify({
clientId : client_id,
clientSecret : client_secret
});
};
const setTokens = () => {
let clientId = readlineSync.question('What is your Spotify Client ID? \n', { hideEchoBack: true });
let clientSecret = readlineSync.question('What is your Spotify Client Secret? \n', { hideEchoBack: true });
nconf.set('spotifyClientID', clientId);
nconf.set('spotifyClientSecret', clientSecret);
nconf.save();
printer.printConfig();
};
if(SPOTIFY_CLIENT_ID_SET && SPOTIFY_CLIENT_SECRET_SET) {
spotifyApi = initSpotifyApi(SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET);
}
else {
if (process.argv[2] === 'reset') {
printer.warning('Config file is already in default state.');
process.exit(0);
}
setTokens();
let SPOTIFY_CLIENT_ID = nconf.get('spotifyClientID');
let SPOTIFY_CLIENT_SECRET = nconf.get('spotifyClientSecret');
spotifyApi = initSpotifyApi(SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET);
if (process.argv[2] === 'token' || process.argv[2] === 'reset') {
process.exit(0);
}
}
function setUsername(){
let clientUsername = readlineSync.question('What is your Spotify Username? \n');
nconf.set('spotifyUsername', clientUsername);
nconf.save();
printer.printConfig();
}
function getPlayListByUsername(username){
spotifyApi.clientCredentialsGrant().then(function(data) {
spotifyApi.setAccessToken(data.body['access_token']);
spotifyApi.getUserPlaylists(username)
.then(function(data) {
var results = parseSearchResults('playlistsByUser', data);
if (results.length > 0) {
printer.printSearchResults('playlists', results);
prompt.start();
prompt.get(['selection'], function (err, result) {
if (err) {
return process.stdout.write('\n');
}
if(results[result.selection-1]){
if (results[result.selection-1].noOfTracks > 0) {
var selectedSpotifyURI = results[result.selection-1].uri;
spotifyClient.play(selectedSpotifyURI).then(() => {
setTimeout(function(){
spotifyClient.status().then((result) => {
printer.printPlayerStatus(result);
});
}, 300);
});
}
else {
printer.error('This playlist has no songs, so it could not be played');
}
}
});
}
else {
printer.error('It looks like the user has any public playlists');
}
}, function(err) {
printer.error('Something went wrong!', err);
});
});
}
const SearchOptions = {
'track': {
'fn': spotifyApi.searchTracks.bind(spotifyApi),
'type': 'tracks'
},
't': {
'fn': spotifyApi.searchTracks.bind(spotifyApi),
'type': 'tracks'
},
'artist': {
'fn': spotifyApi.searchArtists.bind(spotifyApi),
'type': 'artists'
},
'ar': {
'fn': spotifyApi.searchArtists.bind(spotifyApi),
'type': 'artists'
},
'album': {
'fn': spotifyApi.searchAlbums.bind(spotifyApi),
'type': 'albums'
},
'al': {
'fn': spotifyApi.searchAlbums.bind(spotifyApi),
'type': 'albums'
},
'playlist': {
'fn': spotifyApi.searchPlaylists.bind(spotifyApi),
'type': 'playlists'
},
'p': {
'fn': spotifyApi.searchPlaylists.bind(spotifyApi),
'type': 'playlists'
}
};
program
.version(version)
.command('search <type> [query...]')
.description('Search for a <track (t) | artist (ar) | album (al) | playlist (p) > (searches tracks by default)')
.alias('s')
.action((type, query) => {
// search for tracks by default
var searchFn = SearchOptions['track'].fn;
var searchQry = `${type} ${query.join(' ')}`;
var resultType = SearchOptions['track'].type;
if (SearchOptions[type]){
searchFn = SearchOptions[type].fn;
searchQry = query.join(' ');
resultType = SearchOptions[type].type;
}
// Retrieve an access token.
spotifyApi.clientCredentialsGrant()
.then(function(data) {
spotifyApi.setAccessToken(data.body['access_token']);
searchFn(searchQry).then((data) => {
var results = parseSearchResults(resultType, data);
printer.printSearchResults(resultType, results);
prompt.start();
prompt.get(['selection'], function (err, result) {
if (err) {
return process.stdout.write('\n');
}
if(results[result.selection-1]){
var selectedSpotifyURI = results[result.selection-1].spotifyURI;
spotifyClient.play(selectedSpotifyURI).then(() => {
spotifyClient.status().then((result) => {
printer.printPlayerStatus(result);
});
});
}
});
});
}, function(err) {
printer.error('Something went wrong when retrieving an access token', err);
});
});
program
.command('info')
.alias('i')
.description('Display information about the current track along with player status')
.action(() => {
spotifyClient.status().then((result) => {
printer.printPlayerStatus(result);
});
});
program
.command('play [uri]')
.description('Continue playing current track or play the track with the provided URI')
.action((uri) => {
if(uri){
spotifyClient.play(uri).then(() => {
spotifyClient.status().then((result) => {
printer.printPlayerStatus(result);
});
});
}
else {
spotifyClient.play().then(() => {
spotifyClient.status().then((result) => {
printer.printPlayerStatus(result);
});
});
}
});
program
.command('playlist [username]')
.alias('pl')
.description('Get all playlist by user')
.action(username => {
if (username) {
getPlayListByUsername(username);
}
else {
if (SPOTIFY_USERNAME_SET) {
getPlayListByUsername(SPOTIFY_USERNAME);
}
else {
printer.warning('You will need to set your username first using spotify username.');
printer.warning('Run `spotify user`');
}
}
});
program
.command('pause')
.description('Pause the current track')
.action(() => {
spotifyClient.pause().then(() => {
spotifyClient.status().then((result) => {
printer.printPlayerStatus(result);
});
});
});
program
.command('next')
.alias('n')
.description('Play the next track in the queue')
.action(() => {
spotifyClient.next().then(() => {
spotifyClient.status().then((result) => {
printer.printNext(result);
});
});
});
program
.command('back')
.alias('b')
.description('Play the previous track')
.action(() => {
spotifyClient.previous().then(() => {
spotifyClient.status().then((result) => {
printer.printPrevious(result);
});
});
});
program
.command('mute')
.alias('m')
.description('Mute player')
.action(() => {
spotifyClient.mute().then(() => {
spotifyClient.getVolume().then((result) => {
printer.printMute(result);
});
});
});
program
.command('unmute')
.alias('u')
.description('Unmute player')
.action(() => {
spotifyClient.unmute().then(() => {
spotifyClient.getVolume().then((result) => {
printer.printUnmute(result);
});
});
});
program
.command('volume [newVolume]')
.alias('v')
.description('Display player volume')
.action((newVolume) => {
if(newVolume){
spotifyClient.setVolume(newVolume).then(() => {
spotifyClient.getVolume().then((result) => {
printer.printSetVolume(result);
});
});
}
else {
spotifyClient.getVolume().then(() => {
spotifyClient.getVolume().then((result) => {
printer.printVolume(result);
});
});
}
});
program
.command('+ [deltaVolume]')
.description('Turn the volume up by given amount (0-100), default:10')
.action((deltaVolume) => {
var changeInVolume = deltaVolume ? deltaVolume : 10;
spotifyClient.changeVolume(changeInVolume).then(() => {
spotifyClient.getVolume().then((result) => {
printer.printVolumeIncrease(changeInVolume, result);
});
});
});
program
.command('- [deltaVolume]')
.description('Turn the volume down by given amount (0-100), default:10')
.action((deltaVolume) => {
var changeInVolume = deltaVolume ? -deltaVolume : -10 ;
spotifyClient.changeVolume(changeInVolume).then(() => {
spotifyClient.getVolume().then((result) => {
printer.printVolumeDecrease(changeInVolume, result);
});
});
});
program
.command('p')
.description('Toggle play/pause')
.action(() => {
spotifyClient.togglePlayPause().then(() => {
spotifyClient.status().then((result) => {
printer.printPlayerStatus(result);
});
});
});
program
.command('replay')
.alias('r')
.description('Replay current track')
.action(() => {
spotifyClient.replay().then(() => {
spotifyClient.status().then((result) => {
printer.printPlayerStatus(result);
});
});
});
program
.command('position [newPosition]')
.alias('pos')
.description('Get or set player position [mm:ss], e.g: pos 1:23')
.action((newPosition) => {
if(newPosition){
spotifyClient.setPosition(newPosition).then(() => {
spotifyClient.status().then((result) => {
printer.printPlayerStatus(result);
});
});
}
else {
spotifyClient.getPosition().then(() => {
spotifyClient.status().then((result) => {
printer.printPlayerStatus(result);
});
});
}
});
program
.command('quit')
.alias('q')
.description('Quit Spotify :(')
.action(() =>{
spotifyClient.quit();
});
program
.command('open')
.alias('o')
.description('Open Spotify :)')
.action(() =>{
spotifyClient.start();
});
program
.command('shuffle')
.alias('ts')
.description('Toggle shuffle on/off')
.action(() => {
spotifyClient.shuffle().then((status) => {
printer.printToggleShuffle(status);
});
});
program
.command('repeat')
.alias('tr')
.description('Toggle repeat on/off')
.action(() => {
spotifyClient.repeat().then((status) => {
printer.printToggleRepeat(status);
});
});
program
.command('share [type]')
.alias('sh')
.description('Display share <uri|url> and copy value to clipboard')
.action((type) => {
spotifyClient.share(type);
});
program
.command('token')
.alias('tk')
.description('Change Client Spotify tokens')
.action(() => {
setTokens();
});
program
.command('user')
.alias('me')
.description('Change Client Spotify Username')
.action(() => {
setUsername();
});
program
.command('reset')
.description('reset config back to default settings')
.action(() => {
nconf.set('spotifyClientID', 'YOUR_SPOTIFY_CLIENT_ID_HERE');
nconf.set('spotifyClientSecret', 'YOUR_SPOTIFY_CLIENT_SECRET_HERE');
nconf.save();
printer.printConfig();
});
program
.command('lyrics')
.alias('ly')
.description('Display the lyrics of currently playing track')
.action(() => {
spotifyClient.status().then((trackInfo) => {
if(!GENIUS_API_KEY_SET){
console.log('You need to set the Client Access Token for Genius API.');
console.log('Sign up for API access here: https://genius.com/api-clients');
console.log('Update ' + CONFIG_PATH + ' file with your credentials');
return;
}
fetchLyrics(lyricist, trackInfo.artist, trackInfo.track);
});
});
program
.command('recommend')
.alias('rec')
.description('Recommend other songs based on the song currently playing.')
.action(() => {
spotifyApi.clientCredentialsGrant()
.then(function(data) {
spotifyApi.setAccessToken(data.body['access_token']);
spotifyClient.getCurrentSongId().then((data) => {
spotifyApi.getRecommendations({ seed_tracks: [data] }).then((response) => {
// Assuming Spotify won't start recommending albums, artists, or playlists
var results = parseSearchResults('tracks', response);
printer.printSearchResults('tracks', results);
prompt.start();
prompt.get(['selection'], function (err, result) {
if (err) {
return process.stdout.write('\n');
}
if(results[result.selection-1]){
var selectedSpotifyURI = results[result.selection-1].spotifyURI;
spotifyClient.play(selectedSpotifyURI).then(() => {
spotifyClient.status().then((result) => {
printer.printPlayerStatus(result);
});
});
}
});
});
});
}, function(err) {
printer.error('Something went wrong when retrieving an access token', err);
});
});
program.parse(process.argv);
/* CHECK Installed CLI version
* */
let publishedVersion = require('child_process').execSync(`npm info spotify-cli-mac version`);
publishedVersion = publishedVersion.toString().trim().replace(/^\n*/, '').replace(/\n*$/, '');
if(semver.lt(version, semver.clean(publishedVersion))){
console.log(`Your Spotify CLI is outdated. Latest version is ${publishedVersion}, you're on ${version}`);
console.log(`Reinstall by doing:`);
console.log(`npm uninstall -g spotify-cli-mac`);
console.log(`npm install spotify-cli-mac -g\n`);
}