jammus / lastfm-node

Read and write to Last.fm with node.js
http://return12.tumblr.com/tagged/lastfm-node
MIT License
203 stars 33 forks source link

lastfm-node

Read and write to users recent plays on Last.fm.

Installation

npm install lastfm

Usage

var LastFmNode = require('lastfm').LastFmNode;

var lastfm = new LastFmNode({
  api_key: 'apikey',    // sign-up for a key at http://www.last.fm/api
  secret: 'secret',
  useragent: 'appname/vX.X MyApp' // optional. defaults to lastfm-node.
});

Tests

Tests currently use a very old testing framework found at https://github.com/mynyml/ntest. You can get them running by cloning that repository and doing

$ ln -s /path/to/ntest/lib node_modules/ntest

Then run the tests

$ node tests/

Documentation

LastFmRequest

lastfm.request(method, options);

Returns a LastFmRequest instance.

Send request to Last.fm. Requests automatically include the API key and are signed and/or sent via POST as described in the Last.fm API documentation.

Methods:

Accepts any Last.fm API method name, eg "artist.getInfo".

Options:

All options are passed through to Last.fm with the exception of the following.

Events:

RecentTracksStream

lastfm.stream(username);

Returns: a RecentTracksStream instance

Methods:

Options:

Events:

LastFmSession

lastfm.session(options);

Returns: a LastFmSession instance.

If the user and session key are already known supply these in the options. Otherwise supply a token for authorisation. When a token is supplied the session will be authorised with Last.fm. If the user has not yet approved the token (desktop application flow) then authorisation will be automatically retried.

See the last.fm API documentation for more info on Last.fm authorisation flow.

Options:

Public properties:

Methods:

Events:

LastFmUpdate

lastfm.update(method, session, options);

Returns a LastFmUpdate instance.

Valid methods are 'nowplaying' and 'scrobble'.

An authorised LastFmSession instance is required to make a successful update.

If a scrobble request receives an 11 (service offline), 16 (temporarily unavailable) or 29 (rate limit exceeded) error code from Last.fm then the request is automatically retried until it is permanently rejected or accepted. The first retry attempt is made after 10 seconds with subsequent requests delayed by 30 seconds, 1 minute, 5 minutes, 15 minutes and then every 30 minutes.

Options:

Accepts all parameters used by track.updateNowPlaying and user.scrobble (see Last.Fm API) as well as:

Events:

LastFmInfo

lastfm.info(itemtype, [options]);

Returns: a LastFmInfo instance.

Gets extended info about specified item.

Public properties:

Options:

Special cases:

When requesting track info the track param can be either the track name or a track object as returned by RecentTracksStream.

Example

var LastFmNode = require('lastfm').LastFmNode;

var lastfm = new LastFmNode({
  api_key: 'abc',
  secret: 'secret'
});

var trackStream = lastfm.stream('username');

trackStream.on('lastPlayed', function(track) {
  console.log('Last played: ' + track.name);
});

trackStream.on('nowPlaying', function(track) {
  console.log('Now playing: ' + track.name);
});

trackStream.on('scrobbled', function(track) {
  console.log('Scrobbled: ' + track.name);
});

trackStream.on('stoppedPlaying', function(track) {
  console.log('Stopped playing: ' + track.name);
});

trackStream.on('error', function(error) {
  console.log('Error: '  + error.message);
});

trackStream.start();

var session = lastfm.session({
   token: token,
   handlers: {
      success: function(session) {
         lastfm.update('nowplaying', session, { track: track } );
         lastfm.update('scrobble', session, { track: track, timestamp: 12345678 });
      }
   }
});

var request = lastfm.request("artist.getInfo", {
    artist: "The Mae Shi",
    handlers: {
        success: function(data) {
            console.log("Success: " + data);
        },
        error: function(error) {
            console.log("Error: " + error.message);
        }
    }
});

Influences

Heavily drawn from technoweenie's twitter-node
http://github.com/technoweenie/twitter-node

Contributors