cakePHP last-fm datasource

I couldn't find a simple existing cake solution for interfacing with last.fm, so I put together a cakePHP datasource for connecting up to lastfm. At the moment, i've only implemented the getrecenttracks last-fm method (will update soon).

in /app/config/database.php I added the authentication details as per last-fm's api requirements:

Show Plain Text
  1.  
  2.  var $lastfm = array(
  3.         'datasource' => 'lastfm',
  4.         'apikey' => 'APIKEY!',
  5.         'secret' => 'SECRET!'
  6.       );
  7.  

Next, in /app/models/datasources i added my datasource (create the file lastfm_source.php):

Show Plain Text
  1. <?php
  2.  
  3. /**
  4.  *  @desc ultra-simple last-fm datasource
  5.  *  @author joshskeen
  6.  *  @version .1
  7.  *  @filename lastfm_source.php
  8.  **/
  9.  
  10. App::import('Core', array('Xml', 'HttpSocket'));
  11.  
  12. class LastfmSource extends DataSource {
  13.  
  14.     var $secret = '';
  15.     var $apikey = '';
  16.     var $Http = '';
  17.     var $base_url = '';
  18.     var $_baseConfig = array(
  19.         'apiHost' => 'http://ws.audioscrobbler.com/',
  20.         'apiPath' => '2.0/'
  21.         );
  22.  
  23.         function __construct($config) {
  24.             parent::__construct($config);
  25.             $this->Http =& new HttpSocket();
  26.             $this->secret = $this->config['secret'];
  27.             $this->apikey = $this->config['apikey'];
  28.             $this->base_url = $this->_baseConfig['apiHost'] . $this->_baseConfig['apiPath'];
  29.         }
  30.  
  31.         function get_recent_tracks($user, $limit=5){
  32.             return $this->__process($this->Http->get($this->base_url, array('method' => 'user.getrecenttracks',
  33.                                                    'limit' => $limit,
  34.                                                    'user' => $user,
  35.                                                    'api_key' => $this->apikey)));
  36.         }
  37.  
  38.         function __process($response) {
  39.             $xml = new XML($response);
  40.             $array = $xml->toArray();
  41.             $xml->__killParent();
  42.             $xml->__destruct();
  43.             $xml = null;
  44.             unset($xml);
  45.             return $array;
  46.         }
  47. }
  48.  
  49. ?>

And here's a controller demonstrating its use (in /app/controllers/lastfms_controller.php). Notice, i go the route of disabling the model by setting the $uses var to null, since I'm using my datasource instead of an actual model.

Show Plain Text
  1. <?php
  2. class LastfmsController extends AppController {
  3.     var $uses = null;
  4.  
  5.     function index(){
  6.         $this->pageTitle = 'Recently scrobbled';
  7.         $tracks = $this->getRecentTracks('mutexKid', 15);
  8.         $this->set('lfm_tracks', $tracks['Lfm']['Recenttracks']['Track']);
  9.     }
  10.    
  11.     function getRecentTracks($user, $limit){
  12.         $this->Lastfm = ConnectionManager::getDataSource('lastfm');    
  13.         return $this->Lastfm->get_recent_tracks($user, $limit);
  14.     }
  15. }
  16. ?>
download