More Web Nerdery: Banish the crap last.fm flash widget

I’ve replaced the annoying Flash last.fm widget in the sidebar with straight HTML. Here’s how:

Last.fm provides XML feeds of the last 10 tracks you’ve listened to, and it’s easy to read and parse this feed in PHP:

<?php

$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://ws.audioscrobbler.com/1.0/user/namessuck/recenttracks.xml');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec ($ch);
curl_close($ch);

$dom = domxml_open_mem($file_contents);

echo $doc;

$tracks = $dom->get_elements_by_tagname("track");
foreach ($tracks as $track)
{
  $name = $track->get_elements_by_tagname("name");
  $artist = $track->get_elements_by_tagname("artist");
  $date = $track->get_elements_by_tagname("date");
  $url = $track->get_elements_by_tagname("url");
  if (count($name) > 0 && count($artist) > 0 && count($date) > 0)
  {
    $n = $name[0]->first_child();
    $a = $artist[0]->first_child();
    $d = $date[0];
    $when = intval($d->get_attribute(”uts”));
    $diff = time() - $when;
    $whenstr = “”;
    if ($diff <= 60)
      $whenstr = “just listened”;
    else if ($diff < 60 * 60)
      $whenstr = intval($diff / 60) . ” minutes ago”;
    else if ($diff < 24 * 60 * 60)
      $whenstr = intval($diff / (60 * 60)) . ” hours ago”;
    else
      $whenstr = intval($diff / (24 * 60 * 60)) . ” days ago”;
    echo “<span id=\”trackname\”>”;
    if (count($url) > 0)
      {
        $u = $url[0]->first_child();
        echo “<a href=\”" . $u->node_value() . “\”>”
          . $n->node_value() . “</a>”;
      }
    else
      {
        echo $n->node_value();
      }
    echo “</span><br/>”
      . “<span id=\”artistname\”>” . $a->node_value()
      . “</span><br/>”
      . “<span id=\”listendate\”>” . $whenstr
      . “</span><br/><br/>\n”;
  }
}

?>

And then, to avoid waiting for the last.fm feed while the main page is loading, we load this URL in the page via JavaScript, called in the onload handler:


var request2;

function lastfm_recenttracks_callback()
{
  if (request2.readyState == 4)
    {
      var newText = 'Failed to load recent tracks.';
      if (request2.status == 200)
        {
          try
            {
              newText = request2.responseText;
            }
          catch (e)
            {
              newText = 'Failed to load recent tracks: ' + e;
            }
        }
      var elem = document.getElementById('recenttracks');
      if (elem)
        elem.innerHTML = newText;
    }
}

function lastfm_recenttracks()
{
  try
    {
      if (window.XMLHttpRequest)
        {
          request2 = new XMLHttpRequest();
        }
      else
        {
          request2 = new ActiveXObject("Microsoft.XMLHTTP");
        }
      request2.onreadystatechange = lastfm_recenttracks_callback;
      request2.open('GET', 'http://metastatic.org/text/Concern/wp-content/themes/Barthelme.mine/wp-lastfm.php', true);
      request2.send(null);
    }
  catch (e)
    {
      var elem = document.getElementById('recenttracks');
      if (elem)
        {
          elem.innerHTML = "Failed to load recent tracks: " + e;
        }
    }
}