Advertisement
dead__

Untitled

May 26th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.07 KB | None | 0 0
  1. <?php
  2.     class AudioScrobbler {
  3.         private $username = '',
  4.             $raw = '',
  5.             $data = array(), $tracks = array(),
  6.             $cache = null; //  array('class' => '', 'time' => 60); // 1 min
  7.            
  8.         public function __construct($username, $cache = null) {
  9.             $this->username = $username;
  10.             $this->cache = $cache;
  11.            
  12.             if((is_string($cache) && !class_exists($cache)) || (is_array($cache) && !class_exists($cache['class'])))
  13.                 throw new AudioScrobblerException("invalid cache class " . (is_string($cache) ? ($cache) : ($cache['class'])));
  14.            
  15.             if(is_string($cache))
  16.                 $this->cache = array('class' => $cache, 'time' => 60);
  17.            
  18.             $this->cache = (object)$this->cache;
  19.            
  20.             if ($this->raw = $this->getData()) {
  21.                 $this->data = simplexml_load_string($this->raw);
  22.                 $this->initTracks();
  23.             }
  24.             else
  25.                 throw new AudioScrobblerException("unable to load tracks (does user {$this->username} exist?)");
  26.         }
  27.        
  28.         private function getData() {
  29.             if($this->cache !== null)
  30.                 $this->cache->instance = new $this->cache->class($this->username, $this->cache->time);
  31.            
  32.             if(!$this->cache->instance->check())
  33.                 $this->cache->instance->set(file_get_contents("http://ws.audioscrobbler.com/2.0/user/{$this->username}/recenttracks"));
  34.  
  35.             return $this->cache->instance->get();
  36.         }
  37.        
  38.         private function initTracks() {
  39.             foreach ($this->data->track as $track) {
  40.                 $this->tracks[] = new AudioScrobblerTrack($track);
  41.             }
  42.         }
  43.        
  44.         public function getLatestTrack() {
  45.             return $this->tracks[0];
  46.         }
  47.        
  48.         public function getTracks() {
  49.             return $this->tracks;
  50.         }
  51.        
  52.         public function getUser() {
  53.             return $this->username;
  54.         }
  55.     }
  56.    
  57.     interface AudioScrobblerCacheInterface {
  58.         public function __construct($name, $time = 60);
  59.         public function get();
  60.         public function set($data);
  61.         public function check();
  62.     }
  63.  
  64.     class AudioScrobblerCache implements AudioScrobblerCacheInterface {
  65.         protected $name,
  66.             $time = 60;
  67.            
  68.         public function __construct($name, $time = 60) {
  69.             $this->name = $name;
  70.             $this->time = (int)$time;
  71.         }
  72.        
  73.         public function get() {}
  74.        
  75.         public function set($data) {}
  76.        
  77.         public function check() {}
  78.     }
  79.    
  80.     class AudioScrobblerFileCache extends AudioScrobblerCache {
  81.         public function get() {
  82.             return file_get_contents("cache/{$this->name}.fm");
  83.         }
  84.        
  85.         public function set($data) {
  86.             file_put_contents("cache/{$this->name}.fm", $data);
  87.         }
  88.        
  89.         public function check() {
  90.             if(file_exists("cache/{$this->name}.fm") && (time() - filemtime("cache/{$this->name}.fm")) < $this->time)
  91.                 return true;
  92.            
  93.             return false;
  94.         }
  95.        
  96.     }
  97.    
  98.     class AudioScrobblerTrack {
  99.         private $data = array();
  100.        
  101.         public function __construct($track) {
  102.             $this->data = (object)$track;
  103.         }
  104.        
  105.         public function getTime() {
  106.             return $this->data->date->attributes()->uts;
  107.         }
  108.        
  109.         public function getTitle() {
  110.             return $this->data->name;
  111.         }
  112.        
  113.         public function getAlbum() {
  114.             return $this->data->album;
  115.         }
  116.        
  117.         public function getArtist() {
  118.             return $this->data->artist;
  119.         }
  120.     }
  121.    
  122.     class AudioScrobblerException extends Exception {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement