3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to create pretty Spotify URLs
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Nick Holliday <n.g.holliday@gmail.com>
25 * @copyright Nick Holliday
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
31 if (!defined('STATUSNET')) {
34 define('SPOTIFYPLUGIN_VERSION', '0.1');
37 * Plugin to create pretty Spotify URLs
39 * The Spotify API is called before the notice is saved to gather artist and track information.
43 * @author Nick Holliday <n.g.holliday@gmail.com>
44 * @copyright Nick Holliday
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46 * @link http://status.net/
50 class SpotifyPlugin extends Plugin
52 function __construct()
54 parent::__construct();
57 function onStartNoticeSave(Notice $notice)
59 $notice->rendered = preg_replace_callback('/spotify:[a-z]{5,6}:[a-z0-9]{22}/i',
60 "renderSpotifyURILink",
63 $notice->rendered = preg_replace_callback('/<a href="http:\/\/open.spotify.com\/[a-z]{5,6}\/[a-z0-9]{22}" title="http:\/\/open.spotify.com\/[a-z]{5,6}\/[a-z0-9]{22}" rel="external">http:\/\/open.spotify.com\/[a-z]{5,6}\/[a-z0-9]{22}<\/a>/i',
64 "renderSpotifyHTTPLink",
70 function onPluginVersion(array &$versions)
72 $versions[] = array('name' => 'Spotify',
73 'version' => SPOTIFYPLUGIN_VERSION,
74 'author' => 'Nick Holliday',
75 'homepage' => 'http://status.net/wiki/Plugin:Spotify',
77 // TRANS: Plugin description.
78 _m('Create pretty <a href="http://www.spotify.com">Spotify</a> URLs.'));
84 // @todo FIXME: This probably should not be global functions.
85 function doSpotifyLookup($uri, $isArtist)
87 $request = HTTPClient::start();
88 $response = $request->get('http://ws.spotify.com/lookup/1/?uri=' . $uri);
89 if ($response->isOk()) {
90 $xml = simplexml_load_string($response->getBody());
95 return $xml->artist->name . ' - ' . $xml->name;
99 function renderSpotifyURILink($match)
102 if(preg_match('/artist/', $match[0]) > 0) $isArtist = true;
104 $name = doSpotifyLookup($match[0], $isArtist);
105 return "<a href=\"{$match[0]}\">" . $name . "</a>";
108 function renderSpotifyHTTPLink($match)
110 $match[0] = preg_replace('/<a href="http:\/\/open.spotify.com\/[a-z]{5,6}\/[a-z0-9]{22}" title="http:\/\/open.spotify.com\/[a-z]{5,6}\/[a-z0-9]{22}" rel="external">http:\/\/open.spotify.com\//i', 'spotify:', $match[0]);
111 $match[0] = preg_replace('/<\/a>/', '', $match[0]);
112 $match[0] = preg_replace('/\//', ':', $match[0]);
115 if(preg_match('/artist/', $match[0]) > 0) $isArtist = true;
117 $name = doSpotifyLookup($match[0], $isArtist);
118 return "<a href=\"{$match[0]}\">" . $name . "</a>";