From 31a55f3024009b8ad74f5c4e8a655fa51636de2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Thu, 26 Sep 2013 09:29:47 +0200 Subject: [PATCH] search: album and artist support --- lib/Search.js | 20 +++++-- src/search.cc | 88 +++++++++++++++++++++++++++++- test/test-020-search-02-process.js | 33 +++++++++++ 3 files changed, 136 insertions(+), 5 deletions(-) diff --git a/lib/Search.js b/lib/Search.js index bf8b52d..81f5bcf 100644 --- a/lib/Search.js +++ b/lib/Search.js @@ -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; @@ -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, @@ -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 = []; }; diff --git a/src/search.cc b/src/search.cc index 24614a8..4a2f73c 100644 --- a/src/search.cc +++ b/src/search.cc @@ -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 @@ -106,6 +106,36 @@ static Handle Search_Num_Tracks(const Arguments& args) { return scope.Close(Number::New(num)); } +/** + * JS search_num_albums implementation. + */ +static Handle Search_Num_Albums(const Arguments& args) { + HandleScope scope; + + assert(args.Length() == 1); + assert(args[0]->IsObject()); + + ObjectHandle* search = ObjectHandle::Unwrap(args[0]); + int num = sp_search_num_albums(search->pointer); + + return scope.Close(Number::New(num)); +} + +/** + * JS search_num_artists implementation. + */ +static Handle Search_Num_Artists(const Arguments& args) { + HandleScope scope; + + assert(args.Length() == 1); + assert(args[0]->IsObject()); + + ObjectHandle* search = ObjectHandle::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 */ @@ -132,8 +162,64 @@ static Handle 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 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* search = ObjectHandle::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* album = new ObjectHandle("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 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* search = ObjectHandle::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* artist = new ObjectHandle("sp_artist"); + artist->pointer = sp_search_artist(search->pointer, index); + + return scope.Close(artist->object); +} + void nsp::init_search(Handle 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); } diff --git a/test/test-020-search-02-process.js b/test/test-020-search-02-process.js index e5903bb..cab29b7 100644 --- a/test/test-020-search-02-process.js +++ b/test/test-020-search-02-process.js @@ -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(); + }); +};