Last.fm widget in php : mostrare le canzoni ascoltate di recente in un sito web.
Prendendo spunto da una idea del sito mxmm.de e volendo cambiare il widget predefinito di last.fm in Flash per usare qualcosa di più flessibile, ne ho realizzato uno ad hoc.
E’possibile sfruttare l’output JSON delle ultime canzoni ascoltate, sfruttando le API di Last.fm, leggerlo, e sistemarlo come si vuole.
Le uniche cose da impostare sono il proprio username e la propria API key, inoltre si può cambiare il foglio CSS per adattarlo ai propri gusti.
Grazie a questo codice, ho potuto inserire in homepage una widget più larga e meno alta, rispetto a quella in Flash di last.fm
Inoltre, a differenza della widget ufficiale di last.fm, questa indica quante ore e minuti sono passati dall’ascolto di quella canzone (entro le 24 ore).
Passando con la freccia del mouse su una canzone ed aspettando un paio di secondi, apparirà l’orario e la data in cui è stata suonata quella canzone (funziona sia con Internet Explorer che con Firefox)

Esempio di widget per mostrare le canzoni da last.fm
Ecco il codice:
(update 10 Aprile 2010: Aggiunta la funzione “ADESSO”, se si sta ascoltando la canzone proprio in quel momento)
(update Giugno 2012: Codice pulito ed ottimizzato, ed aggiunta la funzione che prevede che il sito di Last.fm sia down)
(update Febbraio 2013: Passaggio dalle API v1 alle API v2 di Last.fm, con l’aggiunta di un file di cache)
<head> <style> #lastfm-title { width: 500px; background-color: #78a5d3; color: #dce8f4; font-size: 19px; font-weight: bold; padding-left: 5px; } .lastfm-homepage { width: 500px; text-align:left; background-color: #dee9ed; color: #000000; padding-left: 5px; } .lastfm-homepage a { text-decoration: none; color: #267dcd; } .lastfm-homepage a:hover { text-decoration: none; color: #00ffff; } </style> <meta content="text/html;charset=utf-8" http-equiv="Content-Type" /> </head> <? /* Last.fm PHP widget. It shows the last $songs played songs. You just need to insert your username, name of the cachefile, expiration time of the cachefile, and your API key. Source: https://www.flapane.com/blog/2009/03/lastfm-widget-in-php-mostrare-le-canzoni-ascoltate-di-recente-in-un-sito-web/ (c) Flapane.com - Sep-2014*/ $username = "your username"; //Last.fm Username $expiretime=2; // cache expire time in minutes $apikey="your api key"; //your api key $cachename="lastfm.cache"; //name of the cachefile $songs=4; //how many songs to show //create the cachefile if it doesn't exist if (!file_exists($cachename)) { $create = fopen($cachename, 'w'); chmod ("$cachename", 0644); //set chmod 644 fclose($create); } // Is the file older than $expiretime, or the file is new/empty? $FileAge = time() - filectime($cachename); // Calculate file age in seconds if ($FileAge > ($expiretime * 60) || 0 == filesize($cachename)) { $url = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=$username&api_key=$apikey&limit=$songs&extended=0&format=json"; $lastfm_json = file_get_contents($url); $handle = fopen($cachename, 'wb'); fwrite($handle, $lastfm_json); //refresh the cache file with new content } //cachefile is fresh enough... outputting data $parsed_json = json_decode(file_get_contents($cachename), true); if(!empty($parsed_json)){ for($i=0; $i<$songs; $i++){ $artist[$i] = $parsed_json['recenttracks']['track'][$i]['artist']['#text']; $title[$i] = $parsed_json['recenttracks']['track'][$i]['name']; $url[$i] = $parsed_json['recenttracks']['track'][$i]['url']; $timesong[$i] = $parsed_json['recenttracks']['track'][$i]['date']['uts']; $playedon[$i] = $parsed_json['recenttracks']['track'][$i]['date']['#text']; if(!empty($parsed_json['recenttracks']['track'][$i]['@attr'])){ $now_playing[$i] = $parsed_json['recenttracks']['track'][$i]['@attr']['nowplaying'];} $tempo = time() - $timesong[$i]; //seconds passed $minutes = (int)($tempo / 60); $hours = (int)($minutes / 60); $days = (int)($hours / 24); if (isset($now_playing) && $now_playing[$i]=='true') { echo "<div class='lastfm-homepage'><a title='Now playing' href=".$url[$i]." target='_blank'>$artist[$i] - $title[$i]</a> Now <img src=\"/img/lastfm_now.gif\" alt=\"lastfm listen icon\" /></div>"; } else if ($days >= 1) { echo "<div class='lastfm-homepage'><a title='$playedon[$i] GMT' href=".$url[$i]." target='_blank'>$artist[$i] - $title[$i]</a> " . $days . " day(s) ago</div>"; } else if ($hours >= 1) { echo "<div class='lastfm-homepage'><a title='$playedon[$i] GMT' href=".$url[$i]." target='_blank'>$artist[$i] - $title[$i]</a> " . $hours . " hours ago</div>"; } else if ($minutes > 1 && $minutes < 60) { echo "<div class='lastfm-homepage'><a title='$playedon[$i] GMT' href=".$url[$i]." target='_blank'>$artist[$i] - $title[$i]</a> " . $minutes . " minutes ago</div>"; } } } else echo "Sorry, Last.fm is down<br />"; ?>
Problemi o suggerimenti? Lasciate pure un commento qui sotto!