forked from ersel/spotify-cli-mac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·397 lines (366 loc) · 9.83 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
#! /usr/bin/env node
'use strict';
const program = require('commander');
const fs = require('fs');
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');
nconf.file(path.join(__dirname, '/config.json'));
// need client access token for genius
let lyricist = require('lyricist');
let SearchOptions;
// let rl = readline.createInterface({
// input: process.stdin,
// output: process.stdout
// });
let GENIUS_API_KEY = nconf.get('GeniusAPIClientKey');
let GENIUS_API_KEY_SET = GENIUS_API_KEY !== 'YOUR_CLIENT_ACCESS_TOKEN_HERE';
if(GENIUS_API_KEY_SET){
lyricist = lyricist(GENIUS_API_KEY);
}
let SPOTIFY_CLIENT_ID = nconf.get('spotifyClientID');
let SPOTIFY_CLIENT_ID_SET = SPOTIFY_CLIENT_ID !== 'YOUR_SPOTIFY_CLIENT_ID_HERE';
let SPOTIFY_CLIENT_SECRET = nconf.get('spotifyClientSecret');
let SPOTIFY_CLIENT_SECRET_SET = SPOTIFY_CLIENT_SECRET !== 'YOUR_SPOTIFY_CLIENT_SECRET_HERE';
let spotifyApi = null;
if(SPOTIFY_CLIENT_ID_SET && SPOTIFY_CLIENT_SECRET_SET) {
spotifyApi = new spotify({
clientId : SPOTIFY_CLIENT_ID,
clientSecret : SPOTIFY_CLIENT_SECRET
});
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'
}
};
}
else {
let clientId = readlineSync.question('What is your Spotify Client ID? \n');
let clientSecret = readlineSync.question('What is your Spotify Client Secret? \n')
nconf.set('spotifyClientID', clientId);
nconf.set('spotifyClientSecret', clientSecret);
nconf.save(function (err) {
fs.readFile(path.join(__dirname, '/config.json'), function (err, data) {
}, () => {
printer.printConfig();
process.exit(1);
});
});
}
program
.version('0.0.2')
.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) {
console.log('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('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 : -11 ;
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 tokens')
.action(() => {
let clientID = readlineSync.question('What is your Spotify Client ID? \n');
let clientSecret = readlineSync.question('What is your Spotify Client Secret? \n')
nconf.set('spotifyClientID', clientID);
nconf.set('spotifyClientSecret', clientSecret);
nconf.save(function (err) {
fs.readFile(path.join(__dirname, '/config.json'), function (err, data) {
});
});
})
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.json file with your credentials');
return;
}
lyricist.song({search: `${trackInfo.artist} ${trackInfo.track}`}, function (err, song) {
if(err){
console.log(`Could not find lyrics for track: ${trackInfo.track} - ${trackInfo.artist}`);
}
else {
console.log(`${trackInfo.track} - ${trackInfo.artist} Lyrics`);
console.log(song.lyrics);
}
});
});
});
program.parse(process.argv);