{"size": 1754, "ext": "js", "max_stars_repo_path": "server2/main.js", "max_stars_repo_name": "redfern314/uplink", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server2/main.js", "max_issues_repo_name": "redfern314/uplink", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server2/main.js", "max_forks_repo_name": "redfern314/uplink", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 32.4814814815, "max_line_length": 115, "alphanum_fraction": 0.5541619156}
var sp = require('libspotify');
// var cred = require('../spotify_key/passwd');
var spawn = require('child_process').spawn;
var fs = require('fs');
var dotenv = require('dotenv');

dotenv.load();

var session = new sp.Session({
    applicationKey: __dirname + '/spotify_appkey.key'
});
// session.login(cred.login, cred.password);
session.login("121181369",process.env.SPOTIFY_SECRET);
session.once('login', function(err) {
    if(err) this.emit('error', err);

    var search = new sp.Search('artist:"walk the moon" track:"shut up and dance"');
    search.trackCount = 1; // we're only interested in the first result;
    search.execute();
    search.once('ready', function() {
        if(!search.tracks.length) {
            console.error('there is no track to play :[');
            session.logout();
        }

        var track = search.tracks[0];
        var player = session.getPlayer();
        player.load(track);
        var f = fs.createWriteStream('audio.raw');
        player.pipe(f);
        player.play();

        // linux
        var play = spawn('aplay', ['-c', 2, '-f', 'S16_LE', '-r', '44100']);
        // osx with `brew install sox`
        var play = spawn('play', ['-r', 44100, '-b', 16, '-L', '-c', 2, '-e', 'signed-integer', '-t', 'raw', '-']);

        player.pipe(play.stdin);

        console.error('playing track. end in %s', track.humanDuration);
        player.on('data', function(buffer) {
            // console.log(buffer);
            // buffer.length
            // buffer.rate
            // buffer.channels
            // 16bit samples
        });
        player.once('track-end', function() {
            console.error('track ended');
            player.stop();
            session.close();
        });
    });
});