Skip to content

Commit 2cc9ad0

Browse files
committed
support for repeating/shuffling states, add new set player position api
Renames existing set player position to movePlayerPosition
1 parent 30f6f17 commit 2cc9ad0

File tree

3 files changed

+85
-10
lines changed

3 files changed

+85
-10
lines changed

api.js

+70-6
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,17 @@ function getState() {
2323
if (state && state.position) {
2424
STATUS.playbackInfo.playbackPosition = state.position;
2525
STATUS.state = state;
26+
27+
spotify.isRepeating(function(err, repeating) {
28+
STATUS.state.isRepeating = repeating;
29+
30+
spotify.isShuffling(function(err, shuffling) {
31+
STATUS.state.isShuffling = shuffling;
32+
33+
updateClients();
34+
});
35+
});
2636
}
27-
28-
updateClients();
2937
return state;
3038
});
3139
}
@@ -67,7 +75,7 @@ function rampVolume(volume) {
6775
});
6876
}
6977

70-
function setPlayerPosition(seconds) {
78+
function movePlayerPosition(seconds) {
7179
let positionScript = `tell application "Spotify"
7280
set currentPosition to get player position
7381
set desiredPosition to (currentPosition + ${seconds})
@@ -81,6 +89,19 @@ function setPlayerPosition(seconds) {
8189
});
8290
}
8391

92+
function setPlayerPosition(seconds) {
93+
let positionScript = `tell application "Spotify"
94+
set desiredPosition to ${seconds}
95+
set player position to desiredPosition
96+
end tell`;
97+
98+
STATUS.playbackInfo.playerState = `Setting Player Position ${seconds} seconds`;
99+
100+
return osascript(positionScript).then(function(response) {
101+
return response;
102+
});
103+
}
104+
84105
module.exports = {
85106
start: function(port) {
86107
//starts the REST API
@@ -179,7 +200,22 @@ module.exports = {
179200
}
180201
});
181202

182-
server.get('/playerPosition/:seconds', function (req, res) {
203+
server.get('/movePlayerPosition/:seconds', function (req, res) {
204+
if (config.get('allowControl')) {
205+
try {
206+
movePlayerPosition(req.params.seconds);
207+
res.send({status: 'player-position-changed'});
208+
}
209+
catch(error) {
210+
res.send({error: error});
211+
}
212+
}
213+
else {
214+
res.send({status: 'not-allowed'});
215+
}
216+
});
217+
218+
server.get('/setPlayerPosition/:seconds', function (req, res) {
183219
if (config.get('allowControl')) {
184220
try {
185221
setPlayerPosition(req.params.seconds);
@@ -466,10 +502,26 @@ module.exports = {
466502
}
467503
});
468504

469-
socket.on('playerPosition', function (seconds) {
505+
socket.on('movePlayerPosition', function (seconds) {
506+
if (config.get('allowControl')) {
507+
try {
508+
movePlayerPosition(seconds);
509+
getState();
510+
}
511+
catch(error) {
512+
socket.emit('error', error);
513+
}
514+
}
515+
else {
516+
socket.emit('control_status', false);
517+
}
518+
});
519+
520+
socket.on('setPlayerPosition', function (seconds) {
470521
if (config.get('allowControl')) {
471522
try {
472523
setPlayerPosition(seconds);
524+
getState();
473525
}
474526
catch(error) {
475527
socket.emit('error', error);
@@ -540,6 +592,7 @@ module.exports = {
540592
if (config.get('allowControl')) {
541593
try {
542594
spotify.volumeUp();
595+
getState();
543596
}
544597
catch(error) {
545598
socket.emit('error', error);
@@ -554,6 +607,7 @@ module.exports = {
554607
if (config.get('allowControl')) {
555608
try {
556609
spotify.volumeDown();
610+
getState();
557611
}
558612
catch(error) {
559613
socket.emit('error', error);
@@ -568,6 +622,7 @@ module.exports = {
568622
if (config.get('allowControl')) {
569623
try {
570624
spotify.setVolume(volume);
625+
getState();
571626
}
572627
catch(error) {
573628
socket.emit('error', error);
@@ -582,6 +637,7 @@ module.exports = {
582637
if (config.get('allowControl')) {
583638
try {
584639
rampVolume(volume);
640+
getState();
585641
}
586642
catch(error) {
587643
socket.emit('error', error);
@@ -596,6 +652,7 @@ module.exports = {
596652
if (config.get('allowControl')) {
597653
try {
598654
spotify.muteVolume();
655+
getState();
599656
}
600657
catch(error) {
601658
socket.emit('error', error);
@@ -610,6 +667,7 @@ module.exports = {
610667
if (config.get('allowControl')) {
611668
try {
612669
spotify.unmuteVolume();
670+
getState();
613671
}
614672
catch(error) {
615673
socket.emit('error', error);
@@ -624,6 +682,7 @@ module.exports = {
624682
if (config.get('allowControl')) {
625683
try {
626684
spotify.setRepeating(true);
685+
getState();
627686
}
628687
catch(error) {
629688
socket.emit('error', error);
@@ -638,6 +697,7 @@ module.exports = {
638697
if (config.get('allowControl')) {
639698
try {
640699
spotify.setRepeating(false);
700+
getState();
641701
}
642702
catch(error) {
643703
socket.emit('error', error);
@@ -652,6 +712,7 @@ module.exports = {
652712
if (config.get('allowControl')) {
653713
try {
654714
spotify.toggleRepeating();
715+
getState();
655716
}
656717
catch(error) {
657718
socket.emit('error', error);
@@ -666,6 +727,7 @@ module.exports = {
666727
if (config.get('allowControl')) {
667728
try {
668729
spotify.setShuffling(true);
730+
getState();
669731
}
670732
catch(error) {
671733
socket.emit('error', error);
@@ -680,6 +742,7 @@ module.exports = {
680742
if (config.get('allowControl')) {
681743
try {
682744
spotify.setShuffling(false);
745+
getState();
683746
}
684747
catch(error) {
685748
socket.emit('error', error);
@@ -694,6 +757,7 @@ module.exports = {
694757
if (config.get('allowControl')) {
695758
try {
696759
spotify.toggleShuffling();
760+
getState();
697761
}
698762
catch(error) {
699763
socket.emit('error', error);
@@ -706,7 +770,7 @@ module.exports = {
706770
});
707771

708772
httpServer.listen(port);
709-
console.log('REST API server started on: ' + port);
773+
console.log('REST/Socket.io API server started on: ' + port);
710774
},
711775

712776
sendUpdates: function() {

api.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ All requests are HTTP GET.
7171

7272
* `/pause`:
7373

74-
Pausess playback.
74+
Pauses playback.
7575
```javascript
7676
{status: 'paused'}
7777
```
@@ -83,13 +83,20 @@ All requests are HTTP GET.
8383
{status: 'play-pause-toggled'}
8484
```
8585

86-
* `/playerPosition/[seconds]`:
86+
* `/movePlayerPosition/[seconds]`:
8787

8888
Moves the player position of the currently playing track forward or backward, in seconds.
8989
```javascript
9090
{status: 'player-position-changed'}
9191
```
9292

93+
* `/setPlayerPosition/[seconds]`:
94+
95+
Sets the player position of the currently playing track to the number of seconds specified.
96+
```javascript
97+
{status: 'player-position-changed'}
98+
```
99+
93100
* `/next`:
94101

95102
Goes to next track.
@@ -268,10 +275,14 @@ Upon connection, the server will emit the `control_status` event to let the clie
268275

269276
Toggles playback between play/pause.
270277

271-
* `playerPosition`:
278+
* `movePlayerPosition`:
272279

273280
Moves the player position of the currently playing track forward or backward based on the first argument, in seconds.
274281

282+
* `setPlayerPosition`:
283+
284+
Sets the player position of the currently playing track to the number of seconds specified in the first argument.
285+
275286
* `next`:
276287

277288
Goes to next track.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "spotify-controller",
33
"productName": "Spotify Controller",
4-
"version": "0.1.9",
4+
"version": "0.2.0",
55
"description": "Control Spotify on MacOS over the network",
66
"license": "MIT",
77
"repository": "josephdadams/spotify-controller",

0 commit comments

Comments
 (0)