Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env node
- /*
- Usage:
- $ /path/to/track.js "first song" "second song" "another search for a song"
- The songs gets queued.
- It first searches the spotify database
- for the search terms (the arguments),
- and then plays the tracks it finds.
- Tested with node.js v0.8.20
- Created by djazz, using Floby's example code:
- https://github.com/Floby/node-libspotify
- Install instructions (for the Raspberry Pi):
- * Install these packages (example for Arch Linux ARM):
- $ sudo pacman -S nodejs make git
- * Put this file in a directory and name the file track.js, and make it executable:
- * $ chmod +x track.js
- * Download the latest build of libspotify from https://developer.spotify.com/technologies/libspotify/
- * The version at the time of writing is "12.1.103 beta"
- * $ wget https://developer.spotify.com/download/libspotify/libspotify-12.1.103-Linux-armv6-bcm2708hardfp-release.tar.gz
- * $ tar -zxvf libspotify-12.1.103-Linux-armv6-bcm2708hardfp-release.tar.gz
- * $ cd libspotify-12.1.103-Linux-armv6-bcm2708hardfp-release
- * $ sudo make prefix=/usr install
- * $ cd ..
- * Optional (remove the downloaded libspotify files, not needed anymore):
- $ rm -rf libspotify-12.1.103-Linux-armv6-bcm2708hardfp-release*
- * $ mkdir node_modules && cd node_modules
- * $ git clone git://github.com/Floby/node-libspotify.git
- * $ mv node-libspotify libspotify
- * $ cd libspotify
- * $ nano -w src/session.cc
- * Remove ", sp_error error" from line 244 starting with "static void call_scrobble_error_callback(..."
- * Replace ", bool is_private" with ", sp_error error" on line 251 starting with "static void call_private_session_mode_changed_callback(..."
- * Press "Ctrl+X Y Enter" to save
- * Download your spotify_appkey.key and put it in the current (node_modules/libspotify) directory.
- URL: https://developer.spotify.com/technologies/libspotify/keys/
- * Compile the nodejs libspotify module:
- $ npm install
- (lots of dependencies. Just wait for it to finish downloading/compiling/installing everything)
- * Now to make the file containing your Spotify login details:
- $ cd .. && mkdir spotify_cred && nano -w spotify_cred/index.js
- * Put this content in this file:
- module.exports.login = "YOUR USERNAME";
- module.exports.password = "YOUR PASSWORD";
- * $ cd ..
- * Install the Speaker node.js module, so you can hear the music you play ;)
- $ npm install speaker
- * $ ./track.js "the living tombstone" "nyan cat"
- Enjoy!
- */
- 'use strict';
- console.log("Loading modules...");
- var sp = require('libspotify');
- var Speaker = require('speaker');
- var cred = require('spotify_cred');
- function prettyPopularity(popularity, width) {
- var output = "";
- var fill = "#";
- var unfill = "-";
- var ratio = popularity/100;
- for (var i = 0; i < width; i++) {
- if (i < ratio*width) {
- output += fill;
- } else {
- output += unfill;
- }
- }
- return output;
- }
- console.log("Logging in as "+cred.login+"...");
- var session = new sp.Session({
- applicationKey: __dirname + '/node_modules/libspotify/spotify_appkey.key'
- });
- session.login(cred.login, cred.password);
- session.once('login', function (err) {
- if (err) {
- console.log(err.toString());
- session.close();
- return;
- }
- var player = session.getPlayer();
- var speaker = new Speaker();
- player.pipe(speaker);
- var searchTerms = process.argv.slice(2);
- var foundTracks = [];
- searchNext();
- function searchNext() {
- var searchTerm = searchTerms.shift();
- if (!searchTerm) {
- playLoop();
- return;
- }
- var search = new sp.Search(searchTerm);
- search.trackCount = 1;
- search.execute();
- search.once('ready', function (search) {
- if (!search.tracks.length) {
- console.log('No search result for "'+searchTerm+'"');
- } else {
- var track = search.tracks[0];
- foundTracks.push(track);
- console.log("Added "+track.title+" by "+track.artist+" to queue ("+searchTerm+")");
- }
- searchNext();
- });
- }
- function playLoop() {
- if (foundTracks.length === 0) {
- player.stop();
- session.logout();
- session.close();
- return;
- }
- var track = foundTracks.shift();
- player.load(track);
- player.play();
- var st = Date.now();
- console.log(' > '+track.title+' by '+track.artist+' ('+track.album+', '+track.album.year+') '+track.humanDuration+' ['+prettyPopularity(track.popularity, 10)+'] '+track.getHttpUrl());
- player.once('track-end', function() {
- //console.log('Track finished buffering, '+Math.round(((Date.now()-st)/track.duration)*100)+"% into the song");
- player.stop();
- if (foundTracks.length === 0) {
- session.logout();
- session.close();
- }
- });
- if (foundTracks.length > 0) {
- setTimeout(function () {
- playLoop();
- }, track.duration);
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement