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

search: album and artist support #31

Open
wants to merge 1 commit 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
20 changes: 16 additions & 4 deletions lib/Search.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var b = require('bindings')('spotify.node');
var Session = require('./Session');
var Track = require('./Track');
var Album = require('./Album');
var Artist = require('./Artist');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var format = require('format').format;
Expand Down Expand Up @@ -45,7 +47,7 @@ Search.prototype.execute = function execute(cb) {
this._session._sp_session,
this._query,
this.trackOffset || 0,
this.trackCount || 10,
this.trackCount || 0,
this.albumOffset || 0,
this.albumCount || 0,
this.artistOffset || 0,
Expand Down Expand Up @@ -75,14 +77,24 @@ Search.prototype.execute = function execute(cb) {
};

Search.prototype._processResults = function _processResults(search) {
var i;

this.tracks = new Array(b.search_num_tracks(this._sp_search));
this.albums = new Array(b.search_num_albums(this._sp_search));
this.artists = new Array(b.search_num_artists(this._sp_search));

for (var i = 0; i < this.tracks.length; ++i) {
for (i = 0; i < this.tracks.length; ++i) {
this.tracks[i] = new Track(b.search_track(this._sp_search, i));
}

this.artists = [];
this.albums = [];
for (i = 0; i < this.albums.length; ++i) {
this.albums[i] = new Album(b.search_album(this._sp_search, i));
}

for (i = 0; i < this.artists.length; ++i) {
this.artists[i] = new Artist(b.search_artist(this._sp_search, i));
}

this.playlists = [];
};

Expand Down
88 changes: 87 additions & 1 deletion src/search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Filename: search.cc
*
* Description: bindings to the spotify search submodule
* Description: bindings to the spotify search submodule
*
* Version: 1.0
* Created: 23/12/2012 16:59:00
Expand Down Expand Up @@ -106,6 +106,36 @@ static Handle<Value> Search_Num_Tracks(const Arguments& args) {
return scope.Close(Number::New(num));
}

/**
* JS search_num_albums implementation.
*/
static Handle<Value> Search_Num_Albums(const Arguments& args) {
HandleScope scope;

assert(args.Length() == 1);
assert(args[0]->IsObject());

ObjectHandle<sp_search>* search = ObjectHandle<sp_search>::Unwrap(args[0]);
int num = sp_search_num_albums(search->pointer);

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

/**
* JS search_num_artists implementation.
*/
static Handle<Value> Search_Num_Artists(const Arguments& args) {
HandleScope scope;

assert(args.Length() == 1);
assert(args[0]->IsObject());

ObjectHandle<sp_search>* search = ObjectHandle<sp_search>::Unwrap(args[0]);
int num = sp_search_num_artists(search->pointer);

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

/**
* JS search_track implementation. gets a track a the given index in a search result
*/
Expand All @@ -132,8 +162,64 @@ static Handle<Value> Search_Track(const Arguments& args) {
return scope.Close(track->object);
}

/**
* JS search_album implementation. gets a album a the given index in a search result
*/
static Handle<Value> Search_Album(const Arguments& args) {
HandleScope scope;

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

// gets sp_search pointer from given object
ObjectHandle<sp_search>* search = ObjectHandle<sp_search>::Unwrap(args[0]);
int index = args[1]->ToNumber()->Int32Value();

// check index is within search results range
assert(index >= 0);
assert(index < sp_search_num_albums(search->pointer));

// create new handle for this album
ObjectHandle<sp_album>* album = new ObjectHandle<sp_album>("sp_album");
album->pointer = sp_search_album(search->pointer, index);

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

/**
* JS search_artist implementation. gets a artist a the given index in a search result
*/
static Handle<Value> Search_Artist(const Arguments& args) {
HandleScope scope;

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

// gets sp_search pointer from given object
ObjectHandle<sp_search>* search = ObjectHandle<sp_search>::Unwrap(args[0]);
int index = args[1]->ToNumber()->Int32Value();

// check index is within search results range
assert(index >= 0);
assert(index < sp_search_num_artists(search->pointer));

// create new handle for this artist
ObjectHandle<sp_artist>* artist = new ObjectHandle<sp_artist>("sp_artist");
artist->pointer = sp_search_artist(search->pointer, index);

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

void nsp::init_search(Handle<Object> target) {
NODE_SET_METHOD(target, "search_create", Search_Create);
NODE_SET_METHOD(target, "search_num_tracks", Search_Num_Tracks);
NODE_SET_METHOD(target, "search_num_albums", Search_Num_Albums);
NODE_SET_METHOD(target, "search_num_artists", Search_Num_Artists);
NODE_SET_METHOD(target, "search_track", Search_Track);
NODE_SET_METHOD(target, "search_album", Search_Album);
NODE_SET_METHOD(target, "search_artist", Search_Artist);
}
33 changes: 33 additions & 0 deletions test/test-020-search-02-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,36 @@ exports.testGetTrackFromSearchResult = function(test) {
test.done();
});
};

exports.testGetAlbumFromSearchResult = function(test) {
var search = new sp.Search('artist:"Hurts" album:"Exile"');
search.trackCount = 0;
search.albumCount = 1;
search.execute(function() {
test.doesNotThrow(function() {
test.ok(search.albums.length > 0, "the search should return at least one result");
var first = search.albums[0];
test.ok(first instanceof sp.Album, "the album results should be loaded album objects");
test.ok(first.isReady());
test.equal('Hurts', first.artist, "the album should be a Hurts album");
test.equal('Exile (Deluxe)', first.name, "the album should be Exile (Deluxe)");
});
test.done();
});
};

exports.testGetArtistFromSearchResult = function(test) {
var search = new sp.Search('artist:"Coldplay"');
search.trackCount = 0;
search.artistCount = 1;
search.execute(function() {
test.doesNotThrow(function() {
test.ok(search.artists.length > 0, "the search should return at least one result");
var first = search.artists[0];
test.ok(first instanceof sp.Artist, "the artist results should be loaded artist objects");
test.ok(first.isReady());
test.equal('Coldplay', first.name, "the artist should be Coldplay");
});
test.done();
});
};