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/
51 class SpotifyPlugin extends Plugin
54 function __construct()
56 parent::__construct();
59 function onStartNoticeSave($notice)
61 $notice->rendered = preg_replace_callback('/spotify:[a-z]{5,6}:[a-z0-9]{22}/i',
62 "renderSpotifyURILink",
65 $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',
66 "renderSpotifyHTTPLink",
74 return 'SpotifyPlugin/'.SPOTIFYPLUGIN_VERSION .
75 ' StatusNet/' . STATUSNET_VERSION;
79 function doSpotifyLookup($uri, $isArtist)
81 $request = HTTPClient::start();
82 $response = $request->get('http://ws.spotify.com/lookup/1/?uri=' . $uri);
83 if ($response->isOk()) {
84 $xml = simplexml_load_string($response->getBody());
89 return $xml->artist->name . ' - ' . $xml->name;
93 function renderSpotifyURILink($match)
96 if(preg_match('/artist/', $match[0]) > 0) $isArtist = true;
98 $name = doSpotifyLookup($match[0], $isArtist);
99 return "<a href=\"{$match[0]}\">" . $name . "</a>";
102 function renderSpotifyHTTPLink($match)
104 $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]);
105 $match[0] = preg_replace('/<\/a>/', '', $match[0]);
106 $match[0] = preg_replace('/\//', ':', $match[0]);
109 if(preg_match('/artist/', $match[0]) > 0) $isArtist = true;
111 $name = doSpotifyLookup($match[0], $isArtist);
112 return "<a href=\"{$match[0]}\">" . $name . "</a>";