onetune / spotify-web-helper

🎛 Control Spotify from node.js
62 stars 11 forks source link
node remote spotify

Please note that because of a recent Spotify update, the library is not working at the moment. Discussion here: https://github.com/onetune/spotify-web-helper/issues/41


Spotify Web Helper for node.js

This is a rewrite of the excellent node-spotify-webhelper, but with support for events, so you don't have to do getStatus() all the time. It also is faster, and starts SpotifyWebHelper on OS X, not just on Windows.
I am also trying to maintain the project and handle issues, at least every 2 months. Pull requests welcome!

Install

$ npm install spotify-web-helper --save

Example

const SpotifyWebHelper = require('spotify-web-helper');

const helper = SpotifyWebHelper();

helper.player.on('error', err => {
  if (error.message.match(/No user logged in/)) {
    // also fires when Spotify client quits
  } else {
    // other errors: /Cannot start Spotify/ and /Spotify is not installed/
  }
});
helper.player.on('ready', () => {

  // Playback events
  helper.player.on('play', () => { });
  helper.player.on('pause', () => { });
  helper.player.on('seek', newPosition => {});
  helper.player.on('end', () => { });
  helper.player.on('track-will-change', track => {});
  helper.player.on('status-will-change', status => {});

  // Playback control. These methods return promises
  helper.player.play('spotify:track:4uLU6hMCjMI75M1A2tKUQC');
  helper.player.pause();
  helper.player.seekTo(60); // 60 seconds

  // Get current playback status, including up to date playing position
  console.log(helper.status);
  // 'status': {
  //    'track': ...,
  //    'shuffle': ...,
  //    'playing_position': ...
  //  }

});

API

Class: SpotifyWebHelper

new SpotifyWebHelper([opts])

helper.player

helper.status

Class: PlayerEventEmitter

Inherits from EventEmitter.

Event: 'end'

Playback has ended.

Event: 'error'

An error has occurred. The listener callback receive the <Error> as first argument. An error occurs when Spotify cannot be started, is not installed, or quits. Refer to the example above to see how to distinguish errors.

Event: 'pause'

Playback has paused.

Event: 'play'

Playback has started.

Event: 'seek'

User has changed the current playing positon.

Event: 'ready'

This player object is ready to use.

Event: 'status-will-change'

Current status has changed. The listener callback receive a <SpotifyStatus> object as first argument.

helper.status will be changed by the new status after this event is emitted.

Event: 'track-will-change'

Current track has changed. The listener callback receive a <SpotifyTrack> object as first argument.

player.pause([unpause]);

player.play(spotifyUri);

Typedef: SpotifyStatus

status.version

status.client_version

status.playing

status.shuffle

status.repeat

status.play_enabled

status.prev_enabled

status.next_enabled

status.track

status.context

status.playing_position

status.server_time

status.volume

status.online

status.open_graph_state

status.running

Typedef: SpotifyTrack

track.track_resource

track.artist_resource

track.album_resource

track.length

track.track_type

Typedef: SpotifyResource

res.name

res.uri

res.location

Compatibility

Since 1.3.0 node >=4.0 is required. Use 1.2.0 for older node versions.