Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

album: getTracks #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"target_name": "libspotify",
"sources": [
"src/album.cc",
"src/albumbrowse.cc",
"src/artist.cc",
"src/audio.cc",
"src/binding.cc",
Expand Down
13 changes: 13 additions & 0 deletions lib/Album.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,17 @@ Album.prototype.smallCoverImage = function (cb) { this.coverImage(this.IMAGE_SIZ
Album.prototype.normalCoverImage = function (cb) { this.coverImage(this.IMAGE_SIZE_NORMAL, cb); }
Album.prototype.largeCoverImage = function (cb) { this.coverImage(this.IMAGE_SIZE_LARGE, cb); }

Album.prototype.getTracks = function (cb) {
var browser = b.albumbrowse_create(this.getSession()._sp_session, this._sp_object, function () {
tracks = new Array(b.albumbrowse_num_tracks(browser));

for(var i = 0; i<tracks.length; i++) {
tracks[i] = new sp.Track(b.albumbrowse_track(browser, i));
}

b.albumbrowse_release(browser);
cb(null, tracks);
});
}

module.exports = Album;
102 changes: 102 additions & 0 deletions src/albumbrowse.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* =====================================================================================
*
* Filename: albumbrowse.cc
*
* Description: bindings for the album subsystem
*
* Version: 1.0
* Revision: none
* Compiler: gcc
*
* Author: Linus Unnebäck, [email protected]
* Company: LinusU AB
*
* =====================================================================================
*/


#include "common.h"

using namespace v8;
using namespace nsp;

void cb_albumbrowse_complete (sp_albumbrowse *result, void *userdata) {
Persistent<Function> callback = static_cast<Function*>(userdata);

callback->Call(callback, 0, NULL);
callback.Dispose();
}

static Handle<Value> AlbumBrowse_Create(const Arguments& args) {
HandleScope scope;

// test arguments sanity
assert(args.Length() == 3);
assert(args[0]->IsObject()); // sp_session
assert(args[1]->IsObject()); // sp_album
assert(args[2]->IsFunction()); // callback

ObjectHandle<sp_session> *session = ObjectHandle<sp_session>::Unwrap(args[0]);
ObjectHandle<sp_album> *album = ObjectHandle<sp_album>::Unwrap(args[1]);
Handle<Function> callback = Persistent<Function>::New(Handle<Function>::Cast(args[2]));

ObjectHandle<sp_albumbrowse>* albumbrowse = new ObjectHandle<sp_albumbrowse>("sp_albumbrowse");
albumbrowse->pointer = sp_albumbrowse_create(session->pointer, album->pointer, cb_albumbrowse_complete, *callback);

return scope.Close(albumbrowse->object);
}

static Handle<Value> AlbumBrowse_Num_Tracks(const Arguments& args) {
HandleScope scope;

// test arguments sanity
assert(args.Length() == 1);
assert(args[0]->IsObject()); // sp_albumbrowse

ObjectHandle<sp_albumbrowse> *albumbrowse = ObjectHandle<sp_albumbrowse>::Unwrap(args[0]);
const int num = sp_albumbrowse_num_tracks(albumbrowse->pointer);

return scope.Close(Number::New(num));
}

static Handle<Value> AlbumBrowse_Track(const Arguments& args) {
HandleScope scope;

// test arguments sanity
assert(args.Length() == 2);
assert(args[0]->IsObject()); // sp_albumbrowse
assert(args[1]->IsNumber()); // index

// input
ObjectHandle<sp_albumbrowse> *albumbrowse = ObjectHandle<sp_albumbrowse>::Unwrap(args[0]);
int index = args[1]->ToNumber()->Int32Value();

// output
sp_track* sptrack = sp_albumbrowse_track(albumbrowse->pointer, index);
ObjectHandle<sp_track>* track = new ObjectHandle<sp_track>("sp_track");
track->pointer = sptrack;

return scope.Close(track->object);
}

static Handle<Value> AlbumBrowse_Release(const Arguments& args) {
HandleScope scope;

// test arguments sanity
assert(args.Length() == 1);
assert(args[0]->IsObject()); // sp_albumbrowse

ObjectHandle<sp_albumbrowse> *albumbrowse = ObjectHandle<sp_albumbrowse>::Unwrap(args[0]);
sp_error error = sp_albumbrowse_release(albumbrowse->pointer);
NSP_THROW_IF_ERROR(error);

return scope.Close(Undefined());
}

void nsp::init_albumbrowse(Handle<Object> target) {
NODE_SET_METHOD(target, "albumbrowse_create", AlbumBrowse_Create);
NODE_SET_METHOD(target, "albumbrowse_num_tracks", AlbumBrowse_Num_Tracks);
NODE_SET_METHOD(target, "albumbrowse_track", AlbumBrowse_Track);
NODE_SET_METHOD(target, "albumbrowse_release", AlbumBrowse_Release);
}
1 change: 1 addition & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern "C" {

// initializing all modules
nsp::init_album(target);
nsp::init_albumbrowse(target);
nsp::init_artist(target);
nsp::init_link(target);
nsp::init_player(target);
Expand Down
7 changes: 4 additions & 3 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ namespace nsp {
* init the album related functions to the target module exports
*/
void init_album(v8::Handle<v8::Object> target);
void init_albumbrowse(v8::Handle<v8::Object> target);
/**
* init the artist related functions to the target module exports
*/
Expand All @@ -156,7 +157,7 @@ namespace nsp {
* init the playlist related functions to the target module exports
*/
void init_playlist(v8::Handle<v8::Object> target);

/**
* This utility class allows to keep track of a C pointer that we attached
* to a JS object. It differs from node's ObjectWrap in the fact that it
Expand Down Expand Up @@ -191,7 +192,7 @@ namespace nsp {
* We do create this one
*/
v8::Persistent<v8::Object> object;

/**
* Get the name of the ObjectHandle that we gave it during instanciation
*/
Expand Down Expand Up @@ -229,7 +230,7 @@ namespace nsp {

object->SetPointerInInternalField(0, this);
}

template <typename T>
ObjectHandle<T>* ObjectHandle<T>::Unwrap(v8::Handle<v8::Value> obj) {
assert(obj->IsObject());
Expand Down
37 changes: 37 additions & 0 deletions test/test-032-albumbrowse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var sp = require('../lib/libspotify');
var testutil = require('./util');

var getAlbum = function(test, cb) {
var search = new sp.Search('artist:"Hurts" album:"Exile"');
search.trackCount = 1;
search.execute(function() {
test.ok(search.tracks.length > 0, 'the album was found');
test.ok(search.tracks[0] instanceof sp.Track, 'track is an track');
test.ok(search.tracks[0].album instanceof sp.Album, 'album is an album');
cb(search.tracks[0].album);
});
};

var session = null;

exports.albumbrowse = {
setUp: function(cb) {
testutil.getDefaultTestSession(function(s) {
session = s;
cb();
});
},
'get tracks from album': function(test) {
getAlbum(test, function(album) {
album.getTracks(function(err, tracks) {
test.ifError(err);
test.equal(tracks.length, 14, 'the album has 14 tracks');
test.equal(tracks.map(function(e) {return e instanceof sp.Track;}).indexOf(false), -1, 'It should only contain tracks');
test.equal(tracks.reduce(function(prev, current) {
return prev && current.isReady();
}, true), true, 'All tracks should be loaded');
test.done();
});
});
}
}