FrontierPsychiatrist / node-spotify

A module for node.js to use libspotify.
MIT License
143 stars 20 forks source link

Searching / Callbacks with arguments #7

Closed simonexmachina closed 10 years ago

simonexmachina commented 10 years ago

Can you give me some quick pointers on how to perform searches? I'm not familiar with C++ so it'd hard to see how I'm supposed to do a search.

FrontierPsychiatrist commented 10 years ago

Currently the search is really basic, it's the newest feature and work in progress. I'll update the documentation soon.

spotify.search('the avalanches', function() {
    var tracks = this.getTracks();
});

This will search for 'the avalanches' and execute the callback with this as the search result. Currently it's hardcoded to search for maximum 20 tracks and you can only access the tracks, no playlists, artists or albums.

The API for the search may change to something like

var search = new spotify.search('the avalanches');
search.numTracks = 10;
search.trackOffset = 20;
search.execute(function() {
    this.getTracks();
});
FrontierPsychiatrist commented 10 years ago

Search is now more complete.

var search = new spotify.Search('the avalanches', 0 /* global offset, optional */, 10 /* global limit, optional */)
search.artistLimit = 2; //offset and limit available for track, album, artist, playlist
search.artistOffset = 5;
search.playlistLimit = 1;
search.execute(function() {
    var tracks = this.getTracks();
    var artists = this.getArtists();
    console.log("Total artists found " + this.totalArtists); //Also available for tracks, albums and playlists.
});

search.artistOffset = 10;
search.execute(function() {
    //artists are now at position ten.
});
simonexmachina commented 10 years ago

That’s great Moritz, thanks.

FWIW, I much prefer providing a parameter to the callback than mutating this. Furthermore, the common Node.js pattern would be:

search.execute(function(err, result) { if(err) console.log(err) || return; var tracks = result.getTracks(); ...

FrontierPsychiatrist commented 10 years ago

I hear you. Error handling will need some thinking because there are so many pieces of code (in different threads...) where an error can occur. The mutating this thing is from a time when I started learning how to write native node.js plugins. It should be fairly easy to change and I agree, would be better if done via arguments.

simonexmachina commented 10 years ago

Yeah cool, the libspotify side isn't something that I have any familiarity with, so I can't offer any opinion there. Glad you agree :)

FrontierPsychiatrist commented 10 years ago

You can try this out in 4bef5d58be7df10081185b456eada755baec3b7d. Error messages will be always undefined at the moment.

simonexmachina commented 10 years ago

Great, will do.