-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
81 lines (71 loc) · 2.54 KB
/
player.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
const Genre = require('./genre');
const SpotifyWebApi = require('spotify-web-api-node');
function Player(handler) {
this.spotify = new SpotifyWebApi({
clientId: process.env.SPOTIFY_CLIENT_ID,
clientSecret: process.env.SPOTIFY_CLIENT_SECRET,
accessToken: handler.event.session.user.accessToken
});
this.deviceId = handler.attributes.spotifyDeviceMap && handler.attributes.spotifyDeviceMap[handler.event.context.System.device.deviceId];
}
function playlistIdFromUri(uri) {
const parts = uri.split(':');
return parts[parts.length - 1];
}
Player.prototype.playPlaylist = function(uri) {
const options = { context_uri: uri }
if(this.deviceId) {
options.device_id = this.deviceId;
return this.spotify.play(options).then(function() {
setTimeout(this.checkCurrentDeviceAndTransfer.bind(this), 2000);
}.bind(this));
} else {
return this.spotify.play(options);
}
};
Player.prototype.followPlaylist = function(uri) {
const id = playlistIdFromUri(uri);
return this.spotify.followPlaylist(id, { public: false });
};
Player.prototype.unfollowPlaylist = function(uri) {
const id = playlistIdFromUri(uri);
return this.spotify.unfollowPlaylist(id);
};
Player.prototype.getCurrentDevice = function() {
return this.spotify.getMyCurrentPlaybackState().then(function(data) {
if(!data.body.device) throw 'No device found.';
return data.body.device;
});
};
Player.prototype.getCurrentPlaybackState = function() {
return this.spotify.getMyCurrentPlaybackState().then(function(data) {
if(!data.body.item) throw 'No item found.';
return data.body;
});
};
Player.prototype.getCurrentGenre = function() {
return this.spotify.getMyCurrentPlaybackState().then(function(data) {
const genre = data.body && data.body.context && Genre.byUri[data.body.context.uri];
if(genre) {
const id = playlistIdFromUri(genre.uri);
return this.spotify.getPlaylist(id, { fields: "description" }).then(function(data) {
genre.similar = Genre.similarFromDescription(data.body.description);
return genre;
});
} else {
if(data.body && data.body.context) {
throw "no genre: for " + data.body.context.uri;
} else {
throw "no genre: missing playback context";
}
}
}.bind(this));
};
Player.prototype.checkCurrentDeviceAndTransfer = function() {
return this.getCurrentDevice().then(function(device) {
if(device.id != this.deviceId) {
this.spotify.transferMyPlayback({ deviceIds: [this.deviceId], play: true })
}
}.bind(this));
};
module.exports = Player;