Skip to content

Commit

Permalink
Merge pull request #4 from maddox/playlists
Browse files Browse the repository at this point in the history
Playlists
  • Loading branch information
maddox committed Sep 1, 2015
2 parents c574594 + 4d36818 commit 4784bdd
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
25 changes: 21 additions & 4 deletions README.md
Expand Up @@ -72,10 +72,25 @@ and what is playing.
"id": "AC4FFD2271422B47",
"name": "Forever",
"artist": "HAIM",
"album": "Days Are Gone (2013)"
"album": "Days Are Gone (2013)",
"playlist": "Summer Jams"
}
```

#### Playlist Resource

The Playlist resource returns all the information about a playlist in your library.

```json
{
"id": 88592,
"name": "Outkast: The '90s",
"loved": true,
"duration_in_seconds": 4544,
"time": "1:15:44"
},
```

#### AirPlayDevice Resource

The AirPlayDevice resource returns all the information about an available
Expand Down Expand Up @@ -115,16 +130,18 @@ These are the endpoints you can hit to do things.
GET /now_playing => NowPlayingResource
GET /artwork => JPEG Data (image/jpeg)

#### Playlist Control
#### Playlists
Use this endpoint to start a specific playlist.

POST /play_playlist?playlist="Party%20Time" => NowPlayingResource
GET /playlists => {"playlists": [PlaylistResource, PlaylistResource, ...]}
PUT /playlists/:id/play => NowPlayingResource
(DEPRECATED) POST /play_playlist?playlist="Party%20Time" => NowPlayingResource

#### AirPlay Control
Use these endpoints to query and set AirPlay devices. You can set multiple
AirPlay devices to be used at the same time.

GET /airplay_devices => {:airplay_devices => [AirPlayDevice, AirPlayDevice, ...]}
GET /airplay_devices => {"airplay_devices": [AirPlayDevice, AirPlayDevice, ...]}
PUT /airplay_devices/:id/on => AirPlayDevice
PUT /airplay_devices/:id/off => AirPlayDevice

Expand Down
53 changes: 50 additions & 3 deletions app.js
Expand Up @@ -17,17 +17,20 @@ app.use(morgan(logFormat))

function getCurrentState(){
itunes = Application('iTunes');
currentTrack = itunes.currentTrack;
playerState = itunes.playerState();
currentState = {};

currentState['player_state'] = playerState;

if (playerState != "stopped") {
currentTrack = itunes.currentTrack;
currentPlaylist = itunes.currentPlaylist;

currentState['id'] = currentTrack.persistentID();
currentState['name'] = currentTrack.name();
currentState['artist'] = currentTrack.artist();
currentState['album'] = currentTrack.album();
currentState['playlist'] = currentPlaylist.name();

if (currentTrack.year()) {
currentState['album'] += " (" + currentTrack.year() + ")";
Expand All @@ -53,13 +56,40 @@ function sendResponse(error, res){
}
}

function playPlaylist(name){
function playPlaylist(nameOrId){
itunes = Application('iTunes');
itunes.playlists.byName(name).play();

if ((nameOrId - 0) == nameOrId && ('' + nameOrId).trim().length > 0) {
id = parseInt(nameOrId);
itunes.playlists.byId(id).play();
}else{
itunes.playlists.byName(nameOrId).play();
}

return true;
}

function getPlaylists(){
itunes = Application('iTunes');
playlists = itunes.playlists();

playlistNames = [];

for (var i = 0; i < playlists.length; i++) {
playlist = playlists[i];

data = {};
data['id'] = playlist.id();
data['name'] = playlist.name();
data['loved'] = playlist.loved();
data['duration_in_seconds'] = playlist.duration();
data['time'] = playlist.time();
playlistNames.push(data);
}

return playlistNames;
}

app.put('/play', function(req, res){
iTunes.play(function (error){
sendResponse(error, res)
Expand Down Expand Up @@ -108,6 +138,23 @@ app.get('/artwork', function(req, res){
})
})

app.get('/playlists', function (req, res) {
osa(getPlaylists, function (error, data) {
if (error){
console.log(error)
res.sendStatus(500)
}else{
res.json({playlists: data})
}
})
})

app.put('/playlists/:id/play', function (req, res) {
osa(playPlaylist, req.params.id, function (error, data) {
sendResponse(error, res)
})
})

app.post('/play_playlist', function (req, res) {
osa(playPlaylist, req.body.playlist, function (error, data) {
sendResponse(error, res)
Expand Down

0 comments on commit 4784bdd

Please sign in to comment.