123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- define('SPOTIFYPLUGIN_VERSION', '0.1');
- class SpotifyPlugin extends Plugin
- {
- function __construct()
- {
- parent::__construct();
- }
- function onStartNoticeSave($notice)
- {
- $notice->rendered = preg_replace_callback('/spotify:[a-z]{5,6}:[a-z0-9]{22}/i',
- "renderSpotifyURILink",
- $notice->rendered);
- $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',
- "renderSpotifyHTTPLink",
- $notice->rendered);
- return true;
- }
- function onPluginVersion(&$versions)
- {
- $versions[] = array('name' => 'Spotify',
- 'version' => SPOTIFYPLUGIN_VERSION,
- 'author' => 'Nick Holliday',
- 'homepage' => 'http://status.net/wiki/Plugin:Spotify',
- 'rawdescription' =>
-
- _m('Create pretty <a href="http://www.spotify.com">Spotify</a> URLs.'));
- return true;
- }
- }
- 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 "<a href=\"{$match[0]}\">" . $name . "</a>";
- }
- function renderSpotifyHTTPLink($match)
- {
- $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]);
- $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 "<a href=\"{$match[0]}\">" . $name . "</a>";
- }
|