Advertisement
djazz

nodejs libspotify player + instructions for Raspberry Pi

Feb 25th, 2013
6,611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env node
  2. /* 
  3.     Usage:
  4.     $ /path/to/track.js "first song" "second song" "another search for a song"
  5.  
  6.     The songs gets queued.
  7.     It first searches the spotify database
  8.     for the search terms (the arguments),
  9.     and then plays the tracks it finds.
  10.     Tested with node.js v0.8.20
  11.  
  12.     Created by djazz, using Floby's example code:
  13.     https://github.com/Floby/node-libspotify
  14.  
  15.     Install instructions (for the Raspberry Pi):
  16.     * Install these packages (example for Arch Linux ARM):
  17.       $ sudo pacman -S nodejs make git
  18.  
  19.  
  20.     * Put this file in a directory and name the file track.js, and make it executable:
  21.     * $ chmod +x track.js
  22.  
  23.  
  24.     * Download the latest build of libspotify from https://developer.spotify.com/technologies/libspotify/
  25.     * The version at the time of writing is "12.1.103 beta"
  26.  
  27.     * $ wget https://developer.spotify.com/download/libspotify/libspotify-12.1.103-Linux-armv6-bcm2708hardfp-release.tar.gz
  28.     * $ tar -zxvf libspotify-12.1.103-Linux-armv6-bcm2708hardfp-release.tar.gz
  29.     * $ cd libspotify-12.1.103-Linux-armv6-bcm2708hardfp-release
  30.     * $ sudo make prefix=/usr install
  31.     * $ cd ..
  32.     * Optional (remove the downloaded libspotify files, not needed anymore):
  33.         $ rm -rf libspotify-12.1.103-Linux-armv6-bcm2708hardfp-release*
  34.  
  35.  
  36.     * $ mkdir node_modules && cd node_modules
  37.     * $ git clone git://github.com/Floby/node-libspotify.git
  38.     * $ mv node-libspotify libspotify
  39.     * $ cd libspotify
  40.  
  41.     * $ nano -w src/session.cc
  42.     * Remove ", sp_error error" from line 244 starting with "static void call_scrobble_error_callback(..."
  43.     * Replace ", bool is_private" with ", sp_error error" on line 251 starting with "static void call_private_session_mode_changed_callback(..."
  44.     * Press "Ctrl+X Y Enter" to save
  45.  
  46.     * Download your spotify_appkey.key and put it in the current (node_modules/libspotify) directory.
  47.       URL: https://developer.spotify.com/technologies/libspotify/keys/
  48.    
  49.     * Compile the nodejs libspotify module:
  50.       $ npm install
  51.       (lots of dependencies. Just wait for it to finish downloading/compiling/installing everything)
  52.  
  53.  
  54.     * Now to make the file containing your Spotify login details:
  55.       $ cd .. && mkdir spotify_cred && nano -w spotify_cred/index.js
  56.     * Put this content in this file:
  57.         module.exports.login = "YOUR USERNAME";
  58.         module.exports.password = "YOUR PASSWORD";
  59.     * $ cd ..
  60.    
  61.    
  62.     * Install the Speaker node.js module, so you can hear the music you play ;)
  63.       $ npm install speaker
  64.  
  65.  
  66.     * $ ./track.js "the living tombstone" "nyan cat"
  67.  
  68.  
  69.     Enjoy!
  70. */
  71.  
  72.  
  73. 'use strict';
  74.  
  75. console.log("Loading modules...");
  76.  
  77. var sp = require('libspotify');
  78. var Speaker = require('speaker');
  79. var cred = require('spotify_cred');
  80.  
  81. function prettyPopularity(popularity, width) {
  82.     var output = "";
  83.     var fill = "#";
  84.     var unfill = "-";
  85.     var ratio = popularity/100;
  86.     for (var i = 0; i < width; i++) {
  87.         if (i < ratio*width) {
  88.             output += fill;
  89.         } else {
  90.             output += unfill;
  91.         }
  92.     }
  93.     return output;
  94. }
  95.  
  96. console.log("Logging in as "+cred.login+"...");
  97.  
  98. var session = new sp.Session({
  99.     applicationKey: __dirname + '/node_modules/libspotify/spotify_appkey.key'
  100. });
  101.  
  102. session.login(cred.login, cred.password);
  103. session.once('login', function (err) {
  104.     if (err) {
  105.         console.log(err.toString());
  106.         session.close();
  107.         return;
  108.     }
  109.  
  110.     var player = session.getPlayer();
  111.     var speaker = new Speaker();
  112.     player.pipe(speaker);
  113.    
  114.     var searchTerms = process.argv.slice(2);
  115.    
  116.     var foundTracks = [];
  117.  
  118.     searchNext();
  119.    
  120.     function searchNext() {
  121.         var searchTerm = searchTerms.shift();
  122.         if (!searchTerm) {
  123.             playLoop();
  124.             return;
  125.         }
  126.  
  127.         var search = new sp.Search(searchTerm);
  128.         search.trackCount = 1;
  129.         search.execute();
  130.         search.once('ready', function (search) {
  131.             if (!search.tracks.length) {
  132.                 console.log('No search result for "'+searchTerm+'"');
  133.             } else {
  134.                 var track = search.tracks[0];
  135.                 foundTracks.push(track);
  136.                 console.log("Added "+track.title+" by "+track.artist+" to queue ("+searchTerm+")");
  137.             }
  138.  
  139.            
  140.             searchNext();
  141.            
  142.         });
  143.     }
  144.    
  145.     function playLoop() {
  146.         if (foundTracks.length === 0) {
  147.             player.stop();
  148.             session.logout();
  149.             session.close();
  150.             return;
  151.         }
  152.        
  153.         var track = foundTracks.shift();
  154.         player.load(track);
  155.         player.play();
  156.         var st = Date.now();
  157.        
  158.         console.log(' > '+track.title+' by '+track.artist+' ('+track.album+', '+track.album.year+') '+track.humanDuration+' ['+prettyPopularity(track.popularity, 10)+'] '+track.getHttpUrl());
  159.        
  160.         player.once('track-end', function() {
  161.             //console.log('Track finished buffering, '+Math.round(((Date.now()-st)/track.duration)*100)+"% into the song");
  162.             player.stop();
  163.             if (foundTracks.length === 0) {
  164.                 session.logout();
  165.                 session.close();
  166.             }
  167.         });
  168.  
  169.         if (foundTracks.length > 0) {
  170.             setTimeout(function () {
  171.                 playLoop();
  172.             }, track.duration);
  173.         }
  174.     }
  175. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement