Posted By

cleverswine on 10/03/08


Tagged

php DOM cache XPath lastfm


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

cleverswine
KF
xmartyxcorex
4rp70x1n
Priestd09
danfinrud55


Get album covers for recent tracks from Last.fm


 / Published in: PHP
 

This code uses the Last.fm API to get recent tracks for a user and displays the album covers as an UL. It makes a local copy of the generated html and the album cover images and reuses those for one hour.

To use the code, you must have a Last.fm API key. Get one here: http://www.last.fm/api

This is actually my first PHP code (I'm a C# and Ruby dev), so please give me your feedback/criticism.

  1. /*
  2.  * Return an UL of album covers for a user's recent tracks from Last.fm
  3.  * TODO: find some way to periodically clean up the local album cover cache (cron job?)
  4.  */
  5. function GetLastfm($user, $limit){
  6.  
  7. #first, try to get the cache file
  8. $lastfmCacheDir = './cache/lastfm/';
  9. $filename = $lastfmCacheDir . 'lastfm';
  10. if (file_exists($filename)) {
  11. $filedate = filemtime($filename);
  12. $FileAge = time() - $filedate;
  13. if($FileAge < 3600){
  14. #cache is < 1 hour old
  15. return file_get_contents($filename);
  16. }
  17. }
  18.  
  19. #get the URL to the cache directory, for serving up images
  20. $thispath = $_SERVER['REQUEST_URI'];
  21. $thispath = explode('/', $thispath);
  22. $lastfmCacheUrl = $thispath[count($thispath)-2] . '/cache/lastfm/';
  23.  
  24. #get 20 items, then show the first $limit that have album images (not all will have images)
  25. $recentTracksUrl = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=' . $user . '&limit=20&api_key=[your api key]';
  26.  
  27. $count = 0;
  28.  
  29. #load the dom with last.fm response
  30. $dom = new DOMDocument();
  31. $dom->load($recentTracksUrl);
  32.  
  33. #get all tracks and loop
  34. $xPath = new DOMXPath($dom);
  35. $tracks = $dom->getElementsByTagName("track");
  36. $result = '<ul class=\'lastfm\'>';
  37. foreach($tracks as $track){
  38. $songname = $track->getElementsByTagName('name')->item(0)->nodeValue;
  39. $artist = $track->getElementsByTagName('artist')->item(0)->nodeValue;
  40. $url = $track->getElementsByTagName('url')->item(0)->nodeValue;
  41. $imgurl = $track->getElementsByTagName('image')->item(1)->nodeValue;
  42. if($imgurl != ''){
  43. #make a local copy of the image for serving from the cache
  44. $gotlocalcopy = false;
  45. $localfilename = file_name_from_url($imgurl);
  46. if (!file_exists($lastfmCacheDir . $localfilename)) {
  47. $gotlocalcopy = cURLdownload($imgurl, $lastfmCacheDir . $localfilename);
  48. }
  49. else { $gotlocalcopy = true; }
  50. #make sure we got it
  51. if ($gotlocalcopy) {
  52. $result .= "<li><a href='$url' target='_blank'><img src='$lastfmCacheUrl$localfilename' alt='$artist - $songname' title='$artist - $songname'/></a></li>";
  53. $count = $count + 1;
  54. }
  55. }
  56. if($count == $limit){ break; }
  57. }
  58. $result .= '</ul>';
  59.  
  60. #write out cache file for use next time
  61. file_put_contents($filename, $result);
  62.  
  63. return $result;
  64. }
  65.  
  66. /*
  67.  * Adapted from code found in php documentation comments
  68.  */
  69. function cURLcheckBasicFunctions()
  70. {
  71. if( !function_exists("curl_init") &&
  72. !function_exists("curl_setopt") &&
  73. !function_exists("curl_exec") &&
  74. !function_exists("curl_close") ) return false;
  75. else return true;
  76. }
  77. function cURLdownload($url, $file)
  78. {
  79. if( !cURLcheckBasicFunctions() ) return false;
  80. $ch = curl_init();
  81. if($ch)
  82. {
  83. $fp = fopen($file, "w");
  84. if($fp)
  85. {
  86. if( !curl_setopt($ch, CURLOPT_URL, $url) ) return false;
  87. if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return false;
  88. if( !curl_setopt($ch, CURLOPT_HEADER, 0) ) return false;
  89. if( !curl_exec($ch) ) return false;
  90. curl_close($ch);
  91. fclose($fp);
  92. return true;
  93. }
  94. else return false;
  95. }
  96. else return false;
  97. }
  98.  
  99. /*
  100.  * Adapted from code found in php documentation comments
  101.  */
  102. function file_name_from_url($url) {
  103. if($url===null || strlen($url)<= 0) {
  104. return null;
  105. }
  106. $url= explode('?', $url);
  107. $url= explode('/', $url[0]);
  108. $basename= $url[count($url)-1];
  109.  
  110. return $basename;
  111. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: anup on December 11, 2009

this script is very nice but i want to create dynamic images. Please can you help me?

You need to login to post a comment.