Author: kielabokkie Date: 2010-02-03 13:20:03 +0100 (Wed, 03 Feb 2010) New Revision: 27485
Added: plugins/wpLastFmPlugin/trunk/ plugins/wpLastFmPlugin/trunk/LICENSE plugins/wpLastFmPlugin/trunk/README plugins/wpLastFmPlugin/trunk/config/ plugins/wpLastFmPlugin/trunk/config/app.yml plugins/wpLastFmPlugin/trunk/config/config.php plugins/wpLastFmPlugin/trunk/lib/ plugins/wpLastFmPlugin/trunk/lib/wpLastFm.class.php plugins/wpLastFmPlugin/trunk/lib/wpLastFmConfig.php plugins/wpLastFmPlugin/trunk/modules/ plugins/wpLastFmPlugin/trunk/modules/wpLastFm/ plugins/wpLastFmPlugin/trunk/modules/wpLastFm/actions/ plugins/wpLastFmPlugin/trunk/modules/wpLastFm/actions/recentTracksComponent.class.php plugins/wpLastFmPlugin/trunk/modules/wpLastFm/actions/topArtistsComponent.class.php plugins/wpLastFmPlugin/trunk/modules/wpLastFm/templates/ plugins/wpLastFmPlugin/trunk/modules/wpLastFm/templates/_recentTracks.php plugins/wpLastFmPlugin/trunk/modules/wpLastFm/templates/_topArtists.php plugins/wpLastFmPlugin/trunk/package.xml plugins/wpLastFmPlugin/trunk/web/ plugins/wpLastFmPlugin/trunk/web/css/ plugins/wpLastFmPlugin/trunk/web/css/wp_lastfm.css plugins/wpLastFmPlugin/trunk/web/images/ plugins/wpLastFmPlugin/trunk/web/images/default_artist_large.png plugins/wpLastFmPlugin/trunk/web/images/default_artist_medium.png plugins/wpLastFmPlugin/trunk/web/images/default_artist_small.png Log: Initial import of the wpLastFm plugin Added: plugins/wpLastFmPlugin/trunk/LICENSE =================================================================== --- plugins/wpLastFmPlugin/trunk/LICENSE (rev 0) +++ plugins/wpLastFmPlugin/trunk/LICENSE 2010-02-03 12:20:03 UTC (rev 27485) @@ -0,0 +1,7 @@ +Copyright (c) 2010 Wouter Peschier | http://www.kielabokkie.nl + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Added: plugins/wpLastFmPlugin/trunk/README =================================================================== --- plugins/wpLastFmPlugin/trunk/README (rev 0) +++ plugins/wpLastFmPlugin/trunk/README 2010-02-03 12:20:03 UTC (rev 27485) @@ -0,0 +1,102 @@ +# wpLastFm Plugin # + +The `wpLastFmPlugin` is a Symfony plugin that retrieves information from last.fm using their API. + +It comes with default templates for quick and easy display of standard last.fm methods (getRecentTracks and getTopArtists) but can also be used to access any other method from the +[last.fm API](http://www.last.fm/api "last.fm"). Before you can use this plugin you need to apply for an API key on the [last.fm](http://www.last.fm/api/account "last.fm") website. + +# Installation # + + * Install the plugin + + $ ./symfony plugin:install wpLastFmPlugin + + * Next you have to change both the API key that you received from last.fm and your last.fm username in the `app.yml` of the plugin: + + [yml] + # plugins/wpLastFmPlugin/config/app.yml + all: + wp_lastfm: + web_dir: /wpLastFmPlugin + api_key: xxxxxx + username: xxxxxx + + * If you want to use one of the default templates you have to enable the lastFm module in your `settings.yml` + + [php] + all: + enabled_modules: [default, …, wpLastFm] + + * Clear you cache + + $ ./symfony cc + + +# Using a default template # + +In the template of the page where you want to display your top played artists just do the following: + + [php] + include_component('wpLastFm', 'topArtists'); + +You can also show your last played tracks by including the following component: + + [php] + include_component('wpLastFm', 'recentTracks'); + +## Modify the displayed results ## + +You can pass parameters to both components to modify the displayed results. + +The topArtists component accepts the following parameters: + + * `count`: number of results you want to show + * `rows`: number of vertical rows you want to use to display your results + * `image_size`: size of the artist thumbnail (value can be SMALL, MEDIUM or LARGE) + + [php] + include_component('wpLastFm', 'topArtists', array('count' => 18, 'rows' => 3, 'image_size' => 'SMALL')); + +The topArtists component accepts the following parameters: + + * `count`: number of results you want to show + * `image_size`: size of the artist thumbnail (value can be SMALL, MEDIUM or LARGE) + + [php] + include_component('wpLastFm', 'recentTracks', array('count' => 3, 'image_size' => 'MEDIUM')); + +If you are not happy about the used colors just override the css classes in the main css of your application. + +# Retrieve and display data manually # + +To get your data from last.fm just look up the name of the method in the [last.fm API](http://www.last.fm/api "last.fm") and pass it to the get method (in this example we get the top tracks of a user): + + [php] + // Retrieve the top tracks for the user that we have set in the app.yml + $lastfm = new wpLastFm(); + $result = $lastfm->get('user.getTopTracks'); + + // Retrieve the top tracks of a different last.fm user + $lastfm = new wpLastFm('kielabokkie'); + $result = $lastfm->get('user.getTopTracks'); + +This will return a SimpleXML object containing the result of your query. So to get the information of the tracks in this example we would do the following: + + [php] + // Retrieve the top tracks for the user that we have set in the app.yml + $lastfm = new wpLastFm(); + $result = $lastfm->get('user.getTopTracks'); + $tracks = $result->toptracks->track; + + // Iterate through the $tracks object to get the individual results + foreach($tracks AS $track) { + // Do something with the track attributes here + echo 'name: ' . $track->name . '<br/>'; + } + +Some methods in the last.fm API also accept additional parameters. We can make use of these parameters by passing them to the get method in an array: + + [php] + // Retrieve the top tracks of the last seven days + $lastfm = new wpLastFm(); + $result = $lastfm->get('user.getTopTracks', array('period' => '7day')); Added: plugins/wpLastFmPlugin/trunk/config/app.yml =================================================================== --- plugins/wpLastFmPlugin/trunk/config/app.yml (rev 0) +++ plugins/wpLastFmPlugin/trunk/config/app.yml 2010-02-03 12:20:03 UTC (rev 27485) @@ -0,0 +1,5 @@ +all: + wp_lastfm: + web_dir: /wpLastFmPlugin + api_key: xxxxxx + username: xxxxxx Added: plugins/wpLastFmPlugin/trunk/config/config.php =================================================================== --- plugins/wpLastFmPlugin/trunk/config/config.php (rev 0) +++ plugins/wpLastFmPlugin/trunk/config/config.php 2010-02-03 12:20:03 UTC (rev 27485) @@ -0,0 +1,6 @@ +<?php +// If the plugin module is enabled in the setting we add the assets +if (in_array('wpLastFm', sfConfig::get('sf_enabled_modules', array()))) +{ + $this->dispatcher->connect('context.load_factories', array('wpLastFmConfig', 'listenToContextLoadFactoriesEvent')); +} Added: plugins/wpLastFmPlugin/trunk/lib/wpLastFm.class.php =================================================================== --- plugins/wpLastFmPlugin/trunk/lib/wpLastFm.class.php (rev 0) +++ plugins/wpLastFmPlugin/trunk/lib/wpLastFm.class.php 2010-02-03 12:20:03 UTC (rev 27485) @@ -0,0 +1,90 @@ +<?php +class wpLastFm +{ + private $http_status; + private $last_api_call; + private $lastfm_api_key; + private $lastfm_username; + + protected $lastfm_api_address = 'http://ws.audioscrobbler.com/2.0/?method='; + + public function __construct($username = null) + { + if(is_null($username)) { + $this->setLastFmUsername(sfConfig::get('app_wp_lastfm_username')); + } else { + $this->setLastFmUsername($username); + } + $this->setLastFmApiKey(sfConfig::get('app_wp_lastfm_api_key')); + } + + private function getLastFmApiKey() + { + return $this->lastfm_api_key; + } + + private function setLastFmApiKey($value) + { + $this->lastfm_api_key = $value; + } + + private function getLastFmUsername() + { + return $this->lastfm_username; + } + + private function setLastFmUsername($value) + { + $this->lastfm_username = $value; + } + + private function getLastFmApiAddress() + { + return $this->lastfm_api_address; + } + + /** + * Debug helpers + */ + public function lastStatusCode() { + return $this->http_status; + } + + public function lastAPICall() { + return $this->last_api_call; + } + + public function get($method, $parameters = null) + { + $params = ''; + // If there are any parameters we add them to the URL + if (is_array($parameters)) { + foreach($parameters AS $key => $value) { + $params .= '&' . $key . '=' . $value; + } + } + return $this->http($this->getLastFmApiAddress() . $method . '&user=' . $this->getLastFmUsername() . '&api_key=' . $this->getLastFmApiKey() . $params); + } + + /** + * Make an HTTP request + * + * @return API results + */ + private function http($url) { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + curl_setopt($ch, CURLOPT_TIMEOUT, 10); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + + $response = curl_exec($ch); + + $this->http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $this->last_api_call = $url; + + curl_close ($ch); + + return simplexml_load_string($response); + } +} Added: plugins/wpLastFmPlugin/trunk/lib/wpLastFmConfig.php =================================================================== --- plugins/wpLastFmPlugin/trunk/lib/wpLastFmConfig.php (rev 0) +++ plugins/wpLastFmPlugin/trunk/lib/wpLastFmConfig.php 2010-02-03 12:20:03 UTC (rev 27485) @@ -0,0 +1,18 @@ +<?php +class wpLastFmConfig +{ + /** + * After the context has been initiated, we can add the required assets + * + * @param sfEvent $event + */ + public static function listenToContextLoadFactoriesEvent(sfEvent $event) + { + // Add the stylesheet + $context = $event->getSubject(); + $context->getResponse()->addStylesheet(sfConfig::get('app_wp_lastfm_web_dir').'/css/wp_lastfm.css'/*, 'first'*/); + + // Add the date helper + sfConfig::set('sf_standard_helpers', array_unique(array_merge(sfConfig::get('sf_standard_helpers', array()), array('Date')))); + } +} Added: plugins/wpLastFmPlugin/trunk/modules/wpLastFm/actions/recentTracksComponent.class.php =================================================================== --- plugins/wpLastFmPlugin/trunk/modules/wpLastFm/actions/recentTracksComponent.class.php (rev 0) +++ plugins/wpLastFmPlugin/trunk/modules/wpLastFm/actions/recentTracksComponent.class.php 2010-02-03 12:20:03 UTC (rev 27485) @@ -0,0 +1,48 @@ +<?php + +class recentTracksComponent extends sfComponent +{ + public function execute($request) + { + $lastfm = new wpLastFm(); + $lastfm_recent_tracks = $lastfm->get('user.getRecentTracks'); + $tracks = $lastfm_recent_tracks->recenttracks->track; + + // If the count parameter is set for the component we use that otherwise we get all the tracks (10 by default) + $count = $this->count ? $this->count : count($tracks); + + // If the image_size parameter is set for the component we use that otherwise we use SMALL as the default + $image_size = $this->image_size ? $this->image_size : 'SMALL'; + + for($i = 0; $i < $count; $i++) { + $track = $tracks[$i]; + $track_arr[$i]['artist'] = (string)$track->artist; + $track_arr[$i]['name'] = (string)$track->name; + $track_arr[$i]['date'] = (string)$track->date; + + // Get the right image based on the image size + switch($image_size) { + case 'SMALL': + $image = str_replace('/34/', '/34s/', (string)$track->image[0]); + break; + case 'MEDIUM': + $image = str_replace('/64/', '/64s/', (string)$track->image[1]); + break; + case 'LARGE': + $image = str_replace('/126/', '/126s/', (string)$track->image[2]); + break; + } + + // If the track doesn't have a picture we set a default picture + if (empty($image)) { + $image = sfConfig::get('app_wp_lastfm_web_dir') . '/images/default_artist_' . strtolower($image_size) . '.png'; + } + + $track_arr[$i]['image'] = $image; + } + $this->lfm_recent_tracks = $track_arr; + + // Create a lowercase string of the image size for use with our css class + $this->image_size = strtolower($image_size); + } +} Added: plugins/wpLastFmPlugin/trunk/modules/wpLastFm/actions/topArtistsComponent.class.php =================================================================== --- plugins/wpLastFmPlugin/trunk/modules/wpLastFm/actions/topArtistsComponent.class.php (rev 0) +++ plugins/wpLastFmPlugin/trunk/modules/wpLastFm/actions/topArtistsComponent.class.php 2010-02-03 12:20:03 UTC (rev 27485) @@ -0,0 +1,62 @@ +<?php + +class topArtistsComponent extends sfComponent +{ + public function execute($request) + { + $lastfm = new wpLastFm(); + $lastfm_artists = $lastfm->get('user.getTopArtists'); + $artists = $lastfm_artists->topartists->artist; + + // If the count parameter is set for the component we use that otherwise we count all the artists + $count = $this->count ? $this->count : count($artists); + // If the rows parameter is set for the component we use that otherwise we use the default + $rows = $this->rows ? $this->rows : 3; + // If the image_size parameter is set for the component we use that otherwise we use MEDIUM as the default + $image_size = $this->image_size ? $this->image_size : 'MEDIUM'; + + // Calculate the number of artists per row + $count_per_row = ceil($count / $rows); + + $x = 1; + for($i=0; $i<$count; $i++) { + if ($i % $count_per_row == 0) { + $row = 'row_'.$x; + $x++; + } + $artist = $artists[$i]; + $artists_arr[$row][$i]['name'] = (string)$artist->name; + $artists_arr[$row][$i]['playcount'] = (string)$artist->playcount; + + // Get the right image based on the image size + switch($image_size) { + case 'SMALL': + $image = str_replace('/34/', '/34s/', (string)$artist->image[0]); + $image_width = 34; + break; + case 'MEDIUM': + $image = str_replace('/64/', '/64s/', (string)$artist->image[1]); + $image_width = 64; + break; + case 'LARGE': + $image = str_replace('/126/', '/126s/', (string)$artist->image[2]); + $image_width = 126; + break; + } + + // If the track doesn't have a picture we set a default picture + if (empty($image)) { + $image = sfConfig::get('app_wp_lastfm_web_dir') . '/images/default_artist_' . strtolower($image_size) . '.png'; + } + + $artists_arr[$row][$i]['image'] = $image; + } + $this->lfm_artists = $artists_arr; + + // Create a lowercase string of the image size for use with our css class + $this->image_size = strtolower($image_size); + + // Calculate the row width + $this->row_width = ($image_width + 2 + 2) * $count_per_row . 'px'; + } +} Added: plugins/wpLastFmPlugin/trunk/modules/wpLastFm/templates/_recentTracks.php =================================================================== --- plugins/wpLastFmPlugin/trunk/modules/wpLastFm/templates/_recentTracks.php (rev 0) +++ plugins/wpLastFmPlugin/trunk/modules/wpLastFm/templates/_recentTracks.php 2010-02-03 12:20:03 UTC (rev 27485) @@ -0,0 +1,13 @@ +<div class='lfm-recent-tracks'> + <?php foreach($lfm_recent_tracks AS $track) : ?> + <div class='lfm-recent-track-<?php echo $image_size?>'> + <div class='lfm-recent-track-image-<?php echo $image_size?>'> + <?php echo image_tag($track['image'], array('class' => 'lfm-image-' . $image_size))?> + </div> + <div class='lfm-recent-track-info-<?php echo $image_size?>'> + <?php echo $track['artist'] . ' - ' . $track['name']?><br/> + <?php echo time_ago_in_words(strtotime($track['date'] . ' + ' . date('Z') . ' seconds')) . ' ago'?> + </div> + </div> + <?php endforeach; ?> +</div> Added: plugins/wpLastFmPlugin/trunk/modules/wpLastFm/templates/_topArtists.php =================================================================== --- plugins/wpLastFmPlugin/trunk/modules/wpLastFm/templates/_topArtists.php (rev 0) +++ plugins/wpLastFmPlugin/trunk/modules/wpLastFm/templates/_topArtists.php 2010-02-03 12:20:03 UTC (rev 27485) @@ -0,0 +1,11 @@ +<div class='lfm' style='width:<?php echo $row_width?>'> + <?php foreach($lfm_artists AS $row) : ?> + <div class='lfm-artist-row-<?php echo $image_size?>'> + <?php foreach($row AS $artist) : ?> + <div class='lfm-artist-<?php echo $image_size?>'> + <?php echo image_tag($artist['image'], array('title'=>$artist['name'] . ', ' . $artist['playcount'] . ' times played'))?> + </div> + <?php endforeach; ?> + </div> + <?php endforeach; ?> +</div> Added: plugins/wpLastFmPlugin/trunk/package.xml =================================================================== --- plugins/wpLastFmPlugin/trunk/package.xml (rev 0) +++ plugins/wpLastFmPlugin/trunk/package.xml 2010-02-03 12:20:03 UTC (rev 27485) @@ -0,0 +1,49 @@ +<?xml version="1.0" encoding="UTF-8"?> +<package xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" packagerversion="1.4.1" version="2.0" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> + <name>wpLastFmPlugin</name> + <channel>pear.symfony-project.com</channel> + <summary>Retrieve information from last.fm</summary> + <description>The wpLastFmPlugin is a Symfony plugin that retrieves information from last.fm using their API. It comes with default templates for quick and easy display of some standard last.fm methods but can also be used to access any other method from the last.fm API. Before you can use this plugin you need to apply for an API key on the last.fm website (http://www.last.fm/api/account).</description> + <lead> + <name>Wouter Peschier</name> + <user>kielabokkie</user> + <email>[email protected]</email> + <active>yes</active> + </lead> + <date>2010-02-03</date> + <version> + <release>0.0.1</release> + <api>0.0.1</api> + </version> + <stability> + <release>beta</release> + <api>beta</api> + </stability> +<license uri="http://www.symfony-project.com/license">MIT</license> + <notes>-</notes> + <contents><dir name="/"><file name="LICENSE" role="data"/><dir name="config"><file name="config.php" role="data"/><file name="app.yml" role="data"/></dir><dir name="web"><dir name="images"><file name="default_artist_large.png" role="data"/><file name="default_artist_small.png" role="data"/><file name="default_artist_medium.png" role="data"/></dir><dir name="css"><file name="wp_lastfm.css" role="data"/></dir></dir><dir name="lib"><file name="wpLastFmConfig.php" role="data"/><file name="wpLastFm.class.php" role="data"/></dir><file name="README" role="data"/><dir name="modules"><dir name="wpLastFm"><dir name="templates"><file name="_recentTracks.php" role="data"/><file name="_topArtists.php" role="data"/></dir><dir name="actions"><file name="topArtistsComponent.class.php" role="data"/><file name="recentTracksComponent.class.php" role="data"/></dir></dir></dir></dir></contents> + + <dependencies> + <required> + <php> + <min>5.1.0</min> + </php> + <pearinstaller> + <min>1.4.1</min> + </pearinstaller> + <package> + <name>symfony</name> + <channel>pear.symfony-project.com</channel> + <min>1.1.0</min> + <max>1.3.0</max> + <exclude>1.3.0</exclude> + </package> + </required> + </dependencies> + + <phprelease> + </phprelease> + + <changelog> + <release><version><release>=0.0.1</release><api>=0.0.1</api></version><stability><release>=beta</release><api>=beta</api></stability><license uri="http://www.symfony-project.com/license">MIT</license><date>2010-02-03</date><notes>=First beta release</notes></release></changelog> +</package> Added: plugins/wpLastFmPlugin/trunk/web/css/wp_lastfm.css =================================================================== --- plugins/wpLastFmPlugin/trunk/web/css/wp_lastfm.css (rev 0) +++ plugins/wpLastFmPlugin/trunk/web/css/wp_lastfm.css 2010-02-03 12:20:03 UTC (rev 27485) @@ -0,0 +1,23 @@ +.lfm {border: 2px solid white} + +.lfm-artist-row-small {height:38px} +.lfm-artist-small {width:34px; height:34px; border: 2px solid white; float:left} +.lfm-artist-row-medium {height:68px} +.lfm-artist-medium {width:64px; height:64px; border: 2px solid white; float:left} +.lfm-artist-row-large {height:130px} +.lfm-artist-large {width:126px; height:126px; border: 2px solid white; float:left} + +.lfm-recent-tracks {width:408px} +.lfm-recent-track-small {width:408px; height:36px} +.lfm-recent-track-image-small {width:34px; height:36px; float:left} +.lfm-recent-track-info-small {width:364px; height:36px; font-size:0.8em; line-height:1.1em; padding-left:10px; float:right} +.lfm-recent-track-medium {width:408px; height:66px} +.lfm-recent-track-image-medium {width:64px; height:66px; float:left} +.lfm-recent-track-info-medium {width:334px; height:66px; font-size:1em; line-height:1.3em; padding-left:10px; float:right} +.lfm-recent-track-large {width:408px; height:128px} +.lfm-recent-track-image-large {width:126px; height:128px; float:left} +.lfm-recent-track-info-large {width:272px; height:128px; font-size:1.4em; line-height:1.7em; padding-left:10px; float:right} + +.lfm-image-small {width:34px} +.lfm-image-medium {width:64px} +.lfm-image-large {width:126px} Added: plugins/wpLastFmPlugin/trunk/web/images/default_artist_large.png =================================================================== (Binary files differ) Property changes on: plugins/wpLastFmPlugin/trunk/web/images/default_artist_large.png ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: plugins/wpLastFmPlugin/trunk/web/images/default_artist_medium.png =================================================================== (Binary files differ) Property changes on: plugins/wpLastFmPlugin/trunk/web/images/default_artist_medium.png ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: plugins/wpLastFmPlugin/trunk/web/images/default_artist_small.png =================================================================== (Binary files differ) Property changes on: plugins/wpLastFmPlugin/trunk/web/images/default_artist_small.png ___________________________________________________________________ Added: svn:mime-type + application/octet-stream -- You received this message because you are subscribed to the Google Groups "symfony SVN" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/symfony-svn?hl=en.
