. * * @category Plugin * @package StatusNet * @author Nick Holliday * @copyright Nick Holliday * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @link http://status.net/ * * @see Event */ if (!defined('STATUSNET')) { exit(1); } define('SPOTIFYPLUGIN_VERSION', '0.1'); /** * Plugin to create pretty Spotify URLs * * The Spotify API is called before the notice is saved to gather artist and track information. * * @category Plugin * @package StatusNet * @author Nick Holliday * @copyright Nick Holliday * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @link http://status.net/ * * @see Event */ class SpotifyPlugin extends Plugin { function __construct() { parent::__construct(); } function onStartNoticeSave(Notice $notice) { $notice->rendered = preg_replace_callback('/spotify:[a-z]{5,6}:[a-z0-9]{22}/i', "renderSpotifyURILink", $notice->rendered); $notice->rendered = preg_replace_callback('/http:\/\/open.spotify.com\/[a-z]{5,6}\/[a-z0-9]{22}<\/a>/i', "renderSpotifyHTTPLink", $notice->rendered); return true; } function onPluginVersion(array &$versions) { $versions[] = array('name' => 'Spotify', 'version' => SPOTIFYPLUGIN_VERSION, 'author' => 'Nick Holliday', 'homepage' => 'http://status.net/wiki/Plugin:Spotify', 'rawdescription' => // TRANS: Plugin description. _m('Create pretty Spotify URLs.')); return true; } } // @todo FIXME: This probably should not be global functions. function doSpotifyLookup($uri, $isArtist) { $request = HTTPClient::start(); $response = $request->get('http://ws.spotify.com/lookup/1/?uri=' . $uri); if ($response->isOk()) { $xml = simplexml_load_string($response->getBody()); if($isArtist) return $xml->name; else return $xml->artist->name . ' - ' . $xml->name; } } function renderSpotifyURILink($match) { $isArtist = false; if(preg_match('/artist/', $match[0]) > 0) $isArtist = true; $name = doSpotifyLookup($match[0], $isArtist); return "" . $name . ""; } function renderSpotifyHTTPLink($match) { $match[0] = preg_replace('/http:\/\/open.spotify.com\//i', 'spotify:', $match[0]); $match[0] = preg_replace('/<\/a>/', '', $match[0]); $match[0] = preg_replace('/\//', ':', $match[0]); $isArtist = false; if(preg_match('/artist/', $match[0]) > 0) $isArtist = true; $name = doSpotifyLookup($match[0], $isArtist); return "" . $name . ""; }