Skip to content

Instantly share code, notes, and snippets.

@dancrew32
Created April 23, 2012 19:00
Show Gist options
  • Save dancrew32/2473075 to your computer and use it in GitHub Desktop.
Save dancrew32/2473075 to your computer and use it in GitHub Desktop.
find and play a track by command line
#!/usr/bin/php
<? // just ./spotify.php
$api = array(
'context' => stream_context_create(array('http'=>array('method'=>'GET'))),
'ns' => 'spotify',
'url' => 'http://ws.spotify.com/search/1/track.json?q=',
);
fwrite(STDOUT, 'Pick a song: ');
$track = json_decode(
file_get_contents($api['url'] .
implode('%20', explode(' ', trim(fgets(STDIN)))),
false, $api['context']))->tracks[0];
$music = array(
'name' => $track->name,
'artist' => $track->artists[0]->name,
'url' => $track->href,
);
if (substr($music['url'], 0, strlen($api['ns'])) === $api['ns']) {
fwrite(STDOUT, "Now playing: ". $music['name'] ." by ". $music['artist'] ."\n\n");
exec('spotify '. $music['url']);
} else {
fwrite(STDOUT, "No track found...\n\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment