John Tiernan

 
Navigation

Home
About
Computing Projects
Microcontroller Platforms
Art
Blog
Demos
Everything Else
Contact Me

Username:

Password:


Forgotten password? | Register!
Code Sample - Last.FM - User Taste Comparisons
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>johntiernan.co.uk Demo - last.fm</title>
</head>
<body>

<?php
// Include the API lib and config file

require 'lastfmapi/lastfmapi.php';
require 'config.inc.php';


//Get start time
$stTime = microtime();

//Create variables for use later
$method = "user.getfriends";
$user = "";
$page = "";


//Get the username to use, if none set, use creator's own
if(isset($_POST['user']))
{
	$user = strip_tags($_POST['user']);
}
else if(isset($_GET['user']))
{
	$user = strip_tags($_GET['user']);
}
else
{
	$user = "ananasjihad";
}

//Get page number. If none set, default to page 1
if(isset($_GET['page']))
{
	$page = strip_tags($_GET['page']);
}else{
	$page = 1;
}

echo "<h6>Friends of ".$user."</h6>";

//Construct URL for initial API query to get list of user's friends
$source = "http://ws.audioscrobbler.com/2.0/?method=".$method.
"&user=".$user."&api_key=".$publicAPI."&limit=20&page=".$page;

//Debug junk
//echo $source . "<hr />";

//Run query
$xml = simplexml_load_file($source);

//echo $xml->getName() . "<br />";

//For each element in API response, extract data and add to array
$pos = 0;	//Position in array. Could use array append function
foreach($xml->children() as $child)
{
  //echo $child->getName() . "<br />";
  echo "Total: " . $child["total"] . "<br />\n\n";	//Echo total number of friends to process
  foreach($child->children() as $spawn)
  {
     $data[$pos] = array("name" => $spawn->name, "url" => $spawn->url, "date" => $spawn->date);
     //echo $spawn->getName() . "-" . $spawn->body . "<br />";
     $pos++;
  }
  
}

//For each friend...
for($i=0;$i<count($data);$i++)
{
	$similarity = 0;
	
	//Construct comparison API request
	$compSource = "http://ws.audioscrobbler.com/2.0/?method=tasteometer.
				compare&type1=user&type2=user&value1=".$user.
				"&value2=".$data[$i]["name"]."&api_key=".$publicAPI."&limit=1";
	
	//echo $compSource;
	
	//Run query
	$res = simplexml_load_file($compSource);
	
	//Debug junk
	//print_r($res);
	
	//Extract value from response
	foreach($res->children() as $resc)
	{
		//print_r($resc->children());
		foreach($resc->children() as $moar)
		{
			if($moar->getName() == "result")
			{
				$similarity = $moar->score;
				
				//Debugging - output similarity score
				//echo "\t\t<!-- " . $similarity . " -->\n\n";
			}
		}
	}
	
   //Output comparison data. Levels based on arbitary values	
   echo $data[$i]["name"] . " <br />\n\tSimilarity: " . $similarity . "<br />\n";
   if($similarity >= 0 && $similarity <= 0.03)
   {
   	//0
   	echo "UNKNOWN\n\n";
   	
   	//Construct Google Chart API URL and use. Do same for other levels
   	echo "<img src=\"http://chart.apis.google.com/chart?
   			chxl=0:|Low|High&chxt=y&
   			chs=300x150&cht=gm&
   			chd=s:A,,\" width=\"300\"
   			 height=\"150\" alt=\"\" /><hr />";
   }
   elseif($similarity >= 0 && $similarity <= 0.1)
   {
   	//10
   	echo "VERY LOW\n\n";
   	echo "<img src=\"http://chart.apis.google.com/chart?
   			chxl=0:|Low|High&chxt=y&
   			chs=300x150&cht=gm&
   			chd=s:G,,\" width=\"300\"
   	 		 height=\"150\" alt=\"\" /><hr />";
   }
   elseif($similarity >= 0.1 && $similarity <= 0.3)
   {
   	//LOW - 30
   	echo "LOW\n\n";
   	echo "<img src=\"http://chart.apis.google.com/chart?
   			chxl=0:|Low|High&chxt=y&
   			chs=300x150&cht=gm&
   			chd=s:S,,\" width=\"300\"
   			 height=\"150\" alt=\"\" /><hr />";
   }
   elseif($similarity >= 0.3 && $similarity <= 0.5)
   {
   	//MEDIUM - 50
   	echo "MEDIUM\n\n";
   	echo "<img src=\"http://chart.apis.google.com/chart?
   			chxl=0:|Low|High&chxt=y&
   			chs=300x150&cht=gm&
   			chd=s:f,,\" width=\"300\"
   			 height=\"150\" alt=\"\" /><hr />";
   }
   elseif($similarity >= 0.5 && $similarity <= 0.7)
   {
   	//HIGH - 60
   	echo "HIGH\n\n";
   	echo "<img src=\"http://chart.apis.google.com/chart?
   			chxl=0:|Low|High&chxt=y&
   			chs=300x150&cht=gm&
   			chd=s:l,,\" width=\"300\"
   			 height=\"150\" alt=\"\" /><hr />";
   }
   elseif($similarity >= 0.7 && $similarity <= 0.9)
   {
   	//VERY HIGH - 80
   	echo "VERY HIGH\n\n";
   	echo "<img src=\"http://chart.apis.google.com/chart?
   			chxl=0:|Low|High&chxt=y&
   			chs=300x150&cht=gm&
   			chd=s:x,,\" width=\"300\"
   			 height=\"150\" alt=\"\" /><hr />";
   }
   elseif($similarity >= 0.9 && $similarity <= 1)
   {
   	//SUPER - 100
   	echo "SUPER\n\n";
   	echo "<img src=\"http://chart.apis.google.com/chart?
   			chxl=0:|Low|High&chxt=y&
   			chs=300x150&cht=gm&
   			chd=s:9,,\" width=\"300\"
   			 height=\"150\" alt=\"\" /><hr />";
   }
   else
   {
   	echo "<hr />\n\n";
   }
}

//Update previous/next page values
$nextpage = $page + 1;
$prevpage = $page - 1;

//Depending on page number, output next/previous page buttons
if($page>=2)
{
	echo "<a href=\"?user=".$user."&page=".$prevpage."\">
			Last 20</a> | <a href=\"?user=".$user
			."&page=".$nextpage."\">Next 20</a><br />";
}else{
	echo "<a href=\"?user=".$user."&page=".$nextpage."\">
			Next 20</a><br />";
}

?>

<-- Show Last.FM logo as per terms -->
<a href="http://www.last.fm"><img src="/images/lastfm.png"
 width="90px" height="28px" border="0" /></a>

<?php
//Calculate how long page took to load
//Expected to be high due to high number of API calls
$execT = microtime() - $stTime;
echo "This took ". $execT . " seconds";
?>


</body>
</html>
		
 

© John Tiernan 2019 | This site uses 960 Grid System as a layout support. | Valid XHTML 1.0 Strict | Valid CSS