Fuddland
Update: This version is no longer valid as the format of the feeds has changed. See the updated version.
A few weeks back Gordon mentioned a new web-based service called Audioscrobbler, which sounded pretty interesting to me.
Essentially it builds up a profile of your musical tastes — not by asking you questions about which genres or artists you prefer, but by logging exactly what songs you listen to on a day-to-day basis, using a plugin for your media player [a wide range of plugins have been developed]. This information is then compared to other users’, and that’s where the fun begins.
You can quickly find other people who are listening to the same things as you are, and then check out what other artists they also like, building up a network of “friends” whose tastes you respect; you can see which songs by a particular artist are most listened-to, and which users are listening to that artist or song; you can create or join groups dedicated to your favourite performers and join in with discussions about them.
One feature still in development is automatic recommendations — kind of like Amazon Recommendations, but because it would be based on what one actually listens to instead of purchases or on user-entered preferences, it should be more accurate.
They’re also planning to provide a variety of RSS feeds of collective and individuals’ data; currently the only one on offer is for a user’s last ten songs played. Inspired by Richard’s del.ic.ious feed PHP-based integrator, I’ve knocked together some code that produces a nicely marked-up list of the last ten songs in the form of a definition list:
<dl id="audioscrobbler10">
<dt>Song title</dt>
<dd>Artist name</dd>
<dd>[Album title]</dd>
.
.
.
</dl>
[If the first two items of data are missing, it displays “Unknown song title”/”Unknown artist”; if the album title is missing, it leaves out that line altogether.]
You can format this however you like using CSS — for example:
#audioscrobbler10 dl{
border: 2px solid #000;
background-color: #fff;
.
.
.
}
#audiosrobbler10 dl dt, #audiosrobbler10 dl dd{
margin-left: 1em;
.
.
.
}
Here’s the code itself, which you can paste into an existing PHP-enabled page or save as a separate file and include into other pages. You also need to upload the Magpie RSS Parser and change a few set-up details.
[You might find it easier to download/view the code as a separate file.]
<?php
// "Most recently listened-to" Audioscrobbler RSS feed parser
// Converts the feed into a definition list
// Written on 2004/04/27
// Originally posted at
// http://fuddland.org.uk/archives/2004/04/27/audioscrobbler.php
// Last modified 2005/09/04
// Licensed under Creative Commons Attribution-NonCommercial-ShareAlike 2.0
// http://creativecommons.org/licenses/by-nc-sa/2.0/
// ************************************************
// SETTINGS
// ************************************************
// Set your username
$username = username;
// Alter the next line to reflect the location of rss_fetch.inc
require_once "magpierss/rss_fetch.inc";
// Set the location of the cached feed [dir needs to be writable]
define('MAGPIE_CACHE_DIR', 'magpierss/cache');
// Cache the RSS feed for 15 minutes [900 seconds]
// Change to whatever you like
define('MAGPIE_CACHE_AGE', 900);
// Uncomment this next line to output debugging information
// define('MAGPIE_DEBUG',1);
// End of setup
// ************************************************
// *************************************************************
// *************************************************************
// DON'T EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU'RE DOING
// *************************************************************
// *************************************************************
// This function removes leading and trailing spaces and newlines
function cleanup($string){
$cleaned_text = preg_replace("/^\s/", "", $string);
$cleaned_text = preg_replace("/\s$/", "", $cleaned_text);
$cleaned_text = preg_replace("/\n$/s", "", $cleaned_text);
return $cleaned_text;
} // End of cleanup function
// Load Magpie's RSS fetching functions
define(MAGPIE_CACHE_FRESH_ONLY, false);
define(MAGPIE_DETECT_ENCODING, true);
define(MAGPIE_DEBUG, 0);
define(MAGPIE_FETCH_TIME_OUT, 15);
define(MAGPIE_USE_GZIP, true);
require_once $magpiepath . "/rss_fetch.inc";
// Turn on Magpie's caching function
define('MAGPIE_CACHE_ON',1);
if (!$feed = fetch_rss("http://ws.audioscrobbler.com/rdf/history/$username")) {
echo "<p>Sorry, I couldn't fetch the Audioscrobbler RSS feed. Please try again later.</p>";
}
else {
if (!$items = array_slice($feed->items, 0)) {
echo "<p>No songs played in the recent past.</p>";
}
else {
echo "<dl>\n";
foreach ($items as $item) {
$songlink = $item['link'];
$artist = $item['dc']['artist_creator_title'];
$title = $item['dc']['title'];
$album = $item['dc']['albumlist_album_title'];
$time = $item['dc']['date'];
// Faff around to get time in a form PHP can deal with
$time = str_replace("T", " ", $time);
$time = preg_match("/(.*)(\+|\-)(\d\d):\d\d$/", $time, $matches);
$time = $matches[1];
$time = strtotime($time);
$timezone_offset = intval($matches[3]);
if ($matches[2] == "-") {
$timezone_offset = -1*$timezone_offset;
}
$timezone_offset = $timezone_offset*3600;
$time = $time + $timezone_offset;
$time = strftime("%T", $time);
// Clean-up text
$artist = cleanup($artist);
$title = cleanup($title);
$album = cleanup($album);
if ($title != "") {
echo "\t\t<dt>$title</dt>\n";
} else {
echo "\t\t<dt>Unknown song title</dt>\n";
}
if ($artist != "") {
echo "\t<dd><strong>$artist</strong></dd>\n";
} else {
echo "\t<dd>Unknown artist</dd>\n";
}
if ($album != "") {
echo "\t\t<dd><em>[$album]</em></dd>\n";
}
} // foreach ($items as $item)
echo "</dl>\n";
}
} // if (!$feed) else
?>
Suggestions and improvements would be most welcome.
Update: A tidier version of the code has been produced and updated here, and a Creative Commons licence explicitly attached.
In: Indexed / GoogleAdsense & WWW
2004 / 04 / 27 – 11:33
Commenting Closed
Commenting on this post is closed. Thanks to all those who left comments. If you'd still like to say something about this entry, feel free to email me.