Skip to content

Instantly share code, notes, and snippets.

@fjallstrom
Last active August 3, 2016 20:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjallstrom/4f238b15a4d54187f742 to your computer and use it in GitHub Desktop.
Save fjallstrom/4f238b15a4d54187f742 to your computer and use it in GitHub Desktop.
slacklastfmthingie
<?php
date_default_timezone_set("Europe/Stockholm");
require 'vendor/autoload.php';
use GuzzleHttp\Pool;
use GuzzleHttp\Client;
$client = new Client();
$users = array('suprape','musikmarskinen','dafeather','brunobrandstrom','Algoritm','mjelle','hjalle','johannagrip','tumde');
$requests = array();
foreach($users as $user){
$requests[] = $client->createRequest('GET', 'http://ws.audioscrobbler.com/2.0/', [
'query' => array(
'method' => 'user.getrecenttracks',
'limit' => '5',
'api_key' => 'GetThisFromLastFm',
'format' => 'json',
'nowplaying' => 'true',
'user' => $user
)
]);
}
$results = Pool::batch($client, $requests);
$nowplaying = array();
foreach ($results->getSuccessful() as $response) {
$recenttracks = json_decode($response->getBody()->getContents());
if(isset($recenttracks->recenttracks->track)){
foreach($recenttracks->recenttracks->track as $track){
if(isset($track->{"@attr"})){
$temp = new StdClass();
$temp->artist = $track->artist->{"#text"};
$temp->title = $track->name;
$temp->album = $track->album->{"#text"};
$temp->image = $track->image[3]->{"#text"};
$nowplaying[] = $temp;
unset($temp);
};
}
}
}
header('Content-type: text/plain;charset=utf-8');
echo "Verkar som att ".count($nowplaying)." låtar spelas på kontoret just nu:\n";
foreach($nowplaying as $track){
echo '- "'.$track->artist.' - '. $track->title.'"';
if($track->album){
echo ' från skivan "'.$track->album.'"';
}
echo "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment