Newfangled Package Modernizer

    spotify-castv2-client

    1.0.4 • Public • Published

    Spotify CastV2 client

    Now you can cast Spotify over a Chromecast.

    It doesn't require any OAuth client configured, you have to provide your Spotify credentials.

    WARNING: this is only a proof-of-concept hack, do not use in your commercial applications

    Installation

    npm i spotify-castv2-client
    

    Libraries

    This project depends on:

    • spotify-web-api-node: A Node.js wrapper for Spotify's Web API.
    • spotify-webplayer-accesstoken: Wrapper around Web Spotify Player to get an access token that simply works

    Sample

    const Client = require('castv2-client').Client;
    const Spotify = require('./Spotify');
    const mdns = require('mdns');
     
    const browser = mdns.createBrowser(mdns.tcp('googlecast'));
     
    browser.on('serviceUp', function(service) {
      console.log('found device "%s" at %s:%d', service.txtRecord.fn, service.addresses[0], service.port);
      if (service.txtRecord.fn === 'Lounge') {
        ondeviceup(service.addresses[0], service.txtRecord.fn);
        browser.stop();
      }
    });
     
    browser.start();
     
    function ondeviceup(host, device_name) {
      var client = new Client();
      client.connect(host, function() {
        console.log('connected, launching Spotify app on ' + device_name + '...');
        client.launch(Spotify, async(err, player) => {
     
          await player.authenticate({
            username: process.env.SPOTIFY_USERNAME, 
            password: process.env.SPOTIFY_PASSWORD, 
            device_name: process.env.SPOTIFY_DEVICE
          });
     
          console.log('Authentication OK');
     
          const artist = (await player.getAPI().searchArtists('Arcade Fire')).body.artists.items;
          await player.play({
            context_uri: artist[0].uri
          });
          console.log('Play OK');
     
          setTimeout(() => {
            player.pause();
            console.log('Pause OK');
          }, 1000);
        });
      });
     
      client.on('error', function(err) {
        console.log('Error: %s', err.message);
        client.close();
      });
    }

    Test

    Clone the repository, then:

    SPOTIFY_USERNAME={...} SPOTIFY_PASSWORD={...} SPOTIFY_DEVICE={...} node test.js
    

    SPOTIFY_DEVICE is the name of the Chromecast

    License

    MIT

    Install

    npm i spotify-castv2-client@1.0.4

    Version

    1.0.4

    License

    MIT

    Last publish

    Collaborators

    • kopiro