From 9f5b9c65c1958b1c55f4e4bd70a0e64da993a005 Mon Sep 17 00:00:00 2001 From: Michael Haschke Date: Fri, 24 Feb 2012 20:00:04 +0100 Subject: [PATCH] [fix] correct LovedTracks and using TopAlbums 7day period for WeeklyAlbums --- lib/Services/Lastfm.php | 171 +++------------------------------------- 1 file changed, 10 insertions(+), 161 deletions(-) diff --git a/lib/Services/Lastfm.php b/lib/Services/Lastfm.php index 1e22354..deb8fb3 100644 --- a/lib/Services/Lastfm.php +++ b/lib/Services/Lastfm.php @@ -77,10 +77,10 @@ public function populateItemTemplate( &$item ) { class LastFMLovedTracks extends LastFM { public function __construct( $config ) { - parent::setVariables( $config ); + parent::setVariables( $config ); $this->setURL( sprintf( 'http://ws.audioscrobbler.com/2.0/?method=user.getlovedtracks&api_key=%s&user=%s&limit=%d', $this->key, $this->username, $this->total ) ); - $this->setItemTemplate('{%track%}{%artist%} {%track%}'."\n"); + $this->setItemTemplate('
  • {{{track}}} — {{{artist}}}
  • '."\n"); parent::__construct( $config ); } @@ -97,23 +97,14 @@ public function getData() { * @return array */ public function populateItemTemplate( &$item ) { - $album = $item->album; - $artist = $item->artist->name ; + $artist = $item->artist->name; $title= $item->name; $this->compteur++; - - $img = ($item->image[2] != '' ? $item->image[2] : Pubwich::getThemeUrl().'/img/cover.png'); - return array( 'link' => htmlspecialchars( $item->url ), 'artist' => $artist, - 'album' => $album, 'track' => $title, 'date' => $item->date, - 'image' => $img, - 'size' => $this->size, - - 'classe' => isset($this->classes[$this->compteur-1]) ? ' class="'.$this->classes[$this->compteur-1].'"' : '', ); } @@ -123,11 +114,10 @@ public function populateItemTemplate( &$item ) { class LastFMWeeklyTracks extends LastFM { public function __construct( $config ) { - parent::setVariables( $config ); - $this->classes = array( 'premier', 'deuxieme', 'troisieme', 'quatrieme' ); - + parent::setVariables( $config ); + $this->setURL( sprintf( 'http://ws.audioscrobbler.com/2.0/?method=user.getweeklytrackchart&api_key=%s&user=%s', $this->key, $this->username ) ); - $this->setItemTemplate('{%track%}{%artist%} {%track%}'."\n"); + $this->setItemTemplate('
  • {{{track}}} — {{{artist}}} ({{{playcount}}}x)
  • '."\n"); parent::__construct( $config ); } @@ -144,167 +134,26 @@ public function getData() { * @return array */ public function populateItemTemplate( &$item ) { - $album = $item->album; $artist = $item->artist; $title= $item->name; $this->compteur++; - - $img = ($item->image[2] != '' ? $item->image[2] : Pubwich::getThemeUrl().'/img/cover.png'); - return array( 'link' => htmlspecialchars( $item->url ), 'artist' => $artist, - 'album' => $album, 'track' => $title, 'date' => $item->date, - 'image' => $img, - 'size' => $this->size, 'playcount' => $item->playcount, - 'classe' => isset($this->classes[$this->compteur-1]) ? ' class="'.$this->classes[$this->compteur-1].'"' : '', ); } } - class LastFMWeeklyAlbums extends LastFM { - - public function __construct( $config ){ - parent::setVariables( $config ); - - $this->setURL( sprintf( 'http://ws.audioscrobbler.com/2.0/?method=user.getweeklyalbumchart&api_key=%s&user=%s', $this->key, $this->username ) ); - $this->classes = array( 'premier', 'deuxieme', 'troisieme', 'quatrieme' ); - $this->setItemTemplate('{{{title}}}{{{artist}}} {{{album}}}'."\n"); - + class LastFMWeeklyAlbums extends LastFMTopAlbums { + public function __construct( $config ) { + $config['period'] = '7day'; parent::__construct( $config ); } - - public function setVariables( $config ) { - parent::setVariables($config); - $this->size = $config['size']; - } - - /** - * @param string $url - * @return void - */ - public function buildCache($Cache_Lite = null) { - parent::buildCache($Cache_Lite); - $this->buildAlbumCache( true ); - } - - /** - * @param bool $rebuildCache Force cache rebuild - * @return void - */ - public function buildAlbumCache( $rebuildCache ) { - $data = $this->getData(); - $compteur = 0; - if ( $data ) { - foreach ( $this->getData() as $album ) { - $compteur++; - if ($compteur > $this->total) { break; } - $this->fetchAlbum( $album, $rebuildCache ); - } - } - } - - /** - * @param SimpleXMLElement $album - * [@param bool $rebuildCache] - * @return void - */ - public function fetchAlbum($album, $rebuildCache=false) { - $Cache_Lite = new Cache_Lite( parent::getCacheOptions() ); - $id = $this->buildAlbumId( $album ); - if ( !$rebuildCache && $data = $Cache_Lite->get( $id ) ) { - $this->albumdata[$id] = simplexml_load_string( $data ); - } else { - $Cache_Lite->get( $id ); - PubwichLog::log( 2, Pubwich::_( 'Rebuilding cache for a Last.fm album' ) ); - $url = sprintf( "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=%s&artist=%s&album=%s", $this->key, urlencode( $album->artist ), urlencode( $album->name ) ); - $data = FileFetcher::get( $url ); - $cacheWrite = $Cache_Lite->save( $data ); - if ( PEAR::isError($cacheWrite) ) { - //var_dump( $cacheWrite ); - } - $this->albumdata[$id] = simplexml_load_string( $data ); - } - - } - - /** - * @param SimpleXMLElement $album - * @return string - */ - public function buildAlbumId($album) { - return md5( $album->artist . "|" . $album->name ); - } - - /** - * @param SimpleXMLElement $album - * @return string - */ - public function getImage( $album ) { - $id = $this->buildAlbumId( $album ); - $image = $this->albumdata[$id]->album->image[1]; - return ( $image == '' ) ? Pubwich::getThemeUrl().'/img/cover.png' : $image; - } - - /** - * @return array - */ - public function populateItemTemplate( &$item ) { - $album = $item->name; - $artist = $item->artist; - $this->compteur++; - return array( - 'link' => htmlspecialchars( $item->url ), - 'title' => ( $artist . ' — ' . $album ), - 'artist' => $artist, - 'release_date' => $item->release_date, - 'listeners' => $item->listeners, - 'playcount' => $item->playcount, - 'album' => $album, - 'image' => $this->getImage( $item ), - 'size' => $this->size, - 'classe' => isset($this->classes[$this->compteur-1]) ? ' class="'.$this->classes[$this->compteur-1].'"' : '', - ); - } - - /** - * @var $albumdata - */ - public $albumdata; - - /** - * @return SimpleXMLElement - */ - public function getData() { - $data = parent::getData(); - return $data->weeklyalbumchart->album; - } - - /** - * @param SimpleXMLElement $album - * @return string - */ - public function getYear( $album ) { - $id = $this->buildAlbumId( $album ); - $date = $this->albumdata[$id]->album->releasedate; - return $date; - } - - /** - * @param string $url - * @return LastFMWeeklyAlbums - */ - public function init() { - parent::init(); - $this->buildAlbumCache( false ); - return $this; - } - } class LastFMTopAlbums extends LastFM { @@ -312,7 +161,7 @@ public function __construct( $config ) { parent::setVariables( $config ); $period = isset($config['period']) ? $config['period'] : 'overall'; $this->setURL( sprintf( 'http://ws.audioscrobbler.com/2.0/?method=user.gettopalbums&api_key=%s&user=%s&period=%s', $this->key, $this->username, $period ) ); - $this->setItemTemplate('
  • {{{title}}}{{{artist}}} {{{album}}}
  • '."\n"); + $this->setItemTemplate('
  • {{{title}}}{{{artist}}} {{{album}}} ({{{playcount}}}x)
  • '."\n"); parent::__construct( $config ); }