Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class AudioScrobbler {
- private $username = '',
- $raw = '',
- $data = array(), $tracks = array(),
- $cache = null; // array('class' => '', 'time' => 60); // 1 min
- public function __construct($username, $cache = null) {
- $this->username = $username;
- $this->cache = $cache;
- if((is_string($cache) && !class_exists($cache)) || (is_array($cache) && !class_exists($cache['class'])))
- throw new AudioScrobblerException("invalid cache class " . (is_string($cache) ? ($cache) : ($cache['class'])));
- if(is_string($cache))
- $this->cache = array('class' => $cache, 'time' => 60);
- $this->cache = (object)$this->cache;
- if ($this->raw = $this->getData()) {
- $this->data = simplexml_load_string($this->raw);
- $this->initTracks();
- }
- else
- throw new AudioScrobblerException("unable to load tracks (does user {$this->username} exist?)");
- }
- private function getData() {
- if($this->cache !== null)
- $this->cache->instance = new $this->cache->class($this->username, $this->cache->time);
- if(!$this->cache->instance->check())
- $this->cache->instance->set(file_get_contents("http://ws.audioscrobbler.com/2.0/user/{$this->username}/recenttracks"));
- return $this->cache->instance->get();
- }
- private function initTracks() {
- foreach ($this->data->track as $track) {
- $this->tracks[] = new AudioScrobblerTrack($track);
- }
- }
- public function getLatestTrack() {
- return $this->tracks[0];
- }
- public function getTracks() {
- return $this->tracks;
- }
- public function getUser() {
- return $this->username;
- }
- }
- interface AudioScrobblerCacheInterface {
- public function __construct($name, $time = 60);
- public function get();
- public function set($data);
- public function check();
- }
- class AudioScrobblerCache implements AudioScrobblerCacheInterface {
- protected $name,
- $time = 60;
- public function __construct($name, $time = 60) {
- $this->name = $name;
- $this->time = (int)$time;
- }
- public function get() {}
- public function set($data) {}
- public function check() {}
- }
- class AudioScrobblerFileCache extends AudioScrobblerCache {
- public function get() {
- return file_get_contents("cache/{$this->name}.fm");
- }
- public function set($data) {
- file_put_contents("cache/{$this->name}.fm", $data);
- }
- public function check() {
- if(file_exists("cache/{$this->name}.fm") && (time() - filemtime("cache/{$this->name}.fm")) < $this->time)
- return true;
- return false;
- }
- }
- class AudioScrobblerTrack {
- private $data = array();
- public function __construct($track) {
- $this->data = (object)$track;
- }
- public function getTime() {
- return $this->data->date->attributes()->uts;
- }
- public function getTitle() {
- return $this->data->name;
- }
- public function getAlbum() {
- return $this->data->album;
- }
- public function getArtist() {
- return $this->data->artist;
- }
- }
- class AudioScrobblerException extends Exception {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement