Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

last.fm song data displayer

Sator Canetti
Frustrated Catgirl
Join date: 20 Sep 2005
Posts: 130
06-06-2006 12:57
I've written this over the last couple of days while I was bored, to provide a simplistic way to show how to do some basic stuff through PHP and using llHTTPRequest.

The following is 1 php and 1 lsl script to interact with each other and last.fm's recent tracks list. This only works for users that are registered and use plugins/program to submit data to last.fm. (Registration is free and as far as I know, the software is clean)

PHP script: slscrobbler.php (last.fm absorbed audioscrobbler, the track stuff still uses the scrobbler domain)

CODE
<?php
header("Content-type: text/plain; charset= UTF-8"); // Sets the type of response provided, and the character set used

$recent = file( "" ); // The URL to your last.fm info. While logged in to last.fm, go to "http://www.last.fm/onyoursite/datafeeds.php" and click on Recent Tracks -> Text. The URL of the resultatn file is your url to enter here.

$user = ""; // Your desired display name

$lasttrack = rtrim ( substr( $recent[0], 11 ) ); // Gets the information after the timestamp of the last submission time, and gets rid of the newline at the end.
$parse = explode( " - ", $lasttrack, 2 ); // Splits this data into an array.
list( $artist, $title ) = $parse; // Takes the array and assigns the data to variables to be used later.

$when = substr( $recent[0], 0, 10 ); // Gets the timestamp.

$since = time( ) - $when; // Checks the difference between the last submission, and when this script is run.

if ( $since < 3600 ) // Checks to see if the time since the last track was played was under an hour.
{
$howlong = $since / 60;
$howlong = number_format( $howlong, 2 );
$howlong .= " minute(s) ago.";
}
elseif ( $since >= 3600 && $since < 86400 ) // Checks to see if the time since the last track was played was between an hour and a day, only if longer than an hour.
{
$howlong = $since / 3600;
$howlong = number_format($howlong, 2);
$howlong .= " hour(s) ago.";
}
elseif ( $since >= 86400 ) // Checks to see if the time is longer than 1 day, only if it's more than 24 hours worth.
{
$howlong = $since / 86400;
$howlong = number_format( $howlong, 2 );
$howlong .= " day(s) ago.";
}
else // Default statement to cover an error in the data.
{
echo $howlong = "Error: timestamp data conflicts. Please check the settings of your server.";
}

echo $user . " listened to the song [" . $title . "] by [". $artist . "] about " . $howlong; // displays the information.

?>


And the LSL script:

CODE
default
{
state_entry()
{
// Nothing to see here, move along.
}

touch_start(integer total_number)
{
llHTTPRequest("", [HTTP_METHOD, "GET"], ""); // Gets the data from the location you stored the php script, so you'll need that URL here.
}

http_response(key request_id, integer status, list metadata, string body)
{

// The following checks the status of the returned data. http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
if (status == 200)
{
llSay(0, body); // 200 OK (standard "it worked" response)
}

if (status == 404)
{
llSay(0, "Wrong URL or failed request."); // 404 Not Found (quoth the raven, "404")
}

if (status == 403)
{
llSay(0, "Improper permissions or restricted access."); // 403 Forbidden (generally an access issue)
}

if (status == 500)
{
llSay(0, "An error has occured, please check the server."); // 500 Internal Server Error (something has been misconfigured or broken)
}

llSleep(30); // don't want to pound the servers/exceed throttle.
}
}


If there is demand for it, I'll consider a second version that can do timechecking on the webserver to limit the load put on last.fm's server via flatfile or MySQL database stuff.
_____________________
"Have gone to commit suicide. Intend to return from grave Friday. Feed cat." -- A memo by Spider Jerusalem in Transmetropolitan

"Some people are like Slinkies; not really good for anything, but they still bring a smile to your face when you push them down a flight of stairs."

If you're reading this signature, I've probably just disagreed with you. Welcome to the club :D
Burke Prefect
Cafe Owner, Superhero
Join date: 29 Oct 2004
Posts: 2,785
06-06-2006 13:48
Oooh. I'm just wondering how I can corrupt this to my evil ways....
_____________________