Created
April 23, 2012 19:00
-
-
Save dancrew32/2473075 to your computer and use it in GitHub Desktop.
find and play a track by command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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