Last active
August 3, 2016 20:24
-
-
Save fjallstrom/4f238b15a4d54187f742 to your computer and use it in GitHub Desktop.
slacklastfmthingie
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
<?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