This repository was archived by the owner on Jun 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·82 lines (65 loc) · 1.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
#!/usr/bin/env node
var client = require('spotify-node-applescript');
var cli = require('cli').enable('version');
var request = require('request');
var pkg = require('./package.json');
cli.setApp(pkg.name, pkg.version);
cli.parse(null, ['play', 'pause', 'next', 'previous', 'status']);
function nowPlaying() {
client.getTrack(function(err, track) {
if (err || !track) cli.error(err);
cli.info(track.name + " - " + track.artist);
});
}
function parsePlayArg(arg, callback) {
if (arg.toLowerCase().substring(0,8) == 'spotify:') return callback(null, arg);
request('http://ws.spotify.com/search/1/track.json?q=' + arg, function (err, res, body) {
if (err) return callback(err);
if (res.statusCode == 200) {
callback(null, JSON.parse(body).tracks[0].href);
} else {
callback(res.statusCode);
}
});
}
cli.main(function (args, options) {
if (cli.command == 'play') {
if (cli.args.length) {
parsePlayArg(cli.args.join(' '), function (err, url) {
client.playTrack(url, function () {
nowPlaying();
});
});
} else {
client.play(function () {
nowPlaying();
});
}
}
if (cli.command == 'pause') {
client.pause(function () {
cli.ok('paused');
});
}
if (cli.command == 'next') {
client.next(function () {
nowPlaying();
});
}
if (cli.command == 'previous') {
client.previous(function () {
nowPlaying();
});
}
if (cli.command == 'status') {
client.getState(function (err, state) {
if (err) return cli.error(err);
cli.info(state.state);
client.getTrack(function(err, track) {
if (err) return cli.error(err);
cli.info(track.name + " - " + track.artist);
cli.progress((state.position * 1000) / track.duration);
});
});
}
});