Wednesday, November 17, 2010

Scrobbles

Please give a warm LHdlMR welcome to the newest version of my Blogger Last Child widget, now featuring fifteen songs, copiable text, fine-tuned colors that change from day to day, and a handy link to a well-formed XML file.

All so that y'all can have access to the songs I'm listening to on my iTunes.

Last.FM had thoughtfully provided a customizable widget for me to use, but I was never happy with that 'coz 1) it was an image, not a text file, and 2) the titles that were long enough to run past the edge of the image were simply chopped off rather than returned to be completed on a new line. So you most recently had entries like "They Might Be Giants - Sapphire Bull"

Not that there's anything wrong with Sapphire Bull, mind you.

Anyway, I decided to tgry and write something that would get it right, a PHP script that would parse the XML file my scrobbler generates, and once I had worked around the difficulties it had with Blöödhag, and had something I was happy with, I then--and only then--realized that Blogger won't allow you to post PHP scripts!

Ack, all that time wasted . . . but no, at the last I realized I could host the PHP file on my site, and run it here in an iframe.

Yippee!




Here's some code if anyone's interested:






<?php

$date = date("j");
if ($date %7 == 0) {$todayscolor = "#A47FCC";}
if ($date %7 == 1) {$todayscolor = "#0D3F04";}
if ($date %7 == 2) {$todayscolor = "#F7646A";}
if ($date %7 == 3) {$todayscolor = "#26388A";}
if ($date %7 == 4) {$todayscolor = "#EABF15";}
if ($date %7 == 5) {$todayscolor = "#000000";}
if ($date %7 == 6) {$todayscolor = "#408080";}

$i = 1 ;
$spacer="";
$backgroundcolor=$todayscolor;
$fontcolor="#FFFFFF";
$dashflag=1;

echo "<body style=\"margin:0px;padding:0px\">";
echo"<table cellpadding=3 style=\"font-size:12px;width:218\">";
echo "<caption style=\"border:3px double black;color:#666666;font-size:18px;font-weight:700\">Last 15 Tracks Scrobbled at
Last.FM</caption>";
ini_set('allow_url_fopen', 1);


$file = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=rastronomicals&limit=14&api_key=XXXXXXXXXXXXXXXX';



$NAMEflag=0;
$ARTISTflag=0;

echo "\r\n<tr

style=\"color:".$fontcolor.";background-color:".$backgroundcolor."\">";


function contents($parser, $data){
global $spacer;
global $todayscolor;
global $fontcolor;
global $backgroundcolor;
global $i;
global $NAMEflag;
global $ARTISTflag;
global $dashflag;

if ($i % 2==0) {$backgroundcolor=$todayscolor;
$fontcolor="#FFFFFF";} else
{$backgroundcolor="#FFFFFF";
$fontcolor=$todayscolor;}

if ($ARTISTflag==1) {
$NAMEflag=0;

echo $data;
}



if ($NAMEflag==1) {

$ARTISTflag=0;

if ($dashflag==1) {echo " - ";}
echo $data;
$dashflag=0;


}

}

function startTag($parser, $data){

global $i;
global $spacer;
global $NAMEflag;
global $ARTISTflag;



if ($data=="NAME") {$NAMEflag=1;
$ARTISTflag=0;
} else
{$NAMEflag=0;}

if ($data=="ARTIST") {
$ARTISTflag=1;
$NAMEflag=0;
}
else {$ARTISTflag=0;}

if ($NAMEflag==1) {
echo ""; }

if ($ARTISTflag==1 ) {
if ($i > 9) {$spacer="";} else {$spacer=" ";}

echo "<td style=\"vertical-align:top\"><b>".$spacer.$i.". </b><td><b>"; }


}

function endTag($parser, $data){

global $NAMEflag;
global $ARTISTflag;
global $fontcolor;
global $backgroundcolor;
global $i;
global $dashflag;

if ($data=="NAME" ) {
$NAMEflag=1;
$ARTISTflag=0;
}
else {
$NAMEflag=0;
}

if ($data=="ARTIST" ) {
$ARTISTflag=1;
$NAMEflag=0;
}
else {
$ARTISTflag=0;
}

if ($NAMEflag==1) {

echo "</td>\r\n<tr
style=\"color:".$fontcolor.";background-color:".$backgroundcolor."\">";
$i++;
$dashflag=1;
}

if ($ARTISTflag==1) {

echo "</b>"; }

$ARTISTflag=0;
$NAMEflag=0;

}

$xml_parser = xml_parser_create();




$y = xml_set_element_handler($xml_parser, "startTag", "endTag");


if ($y == "NAME") {
xml_set_character_data_handler($xml_parser, "contents"); }

// make sure the remote file is successfully opened before doing anything else
if ($fp = fopen($file, 'r')) {
$data = '';
// keep reading until there's nothing left
while ($line = fread($fp, 1024)) {
$data .= $line;
}


} else {

echo "uh-oh";
var_dump($http_response_headers);


// an error occured when trying to open the specified url
}




if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}

echo "</table>";
echo "<br><center><a style=\"color:#666666\" target=\"_blank\"

href=\"".$file."\">XML File Here</a></center>";

xml_parser_free($xml_parser);

fclose($fp);

?>


No comments: