SpotifyPlugin.php 4.29 KB
Newer Older
1 2 3
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
Siebrand Mazeland's avatar
Siebrand Mazeland committed
4
 *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
 * Plugin to create pretty Spotify URLs
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  Plugin
 * @package   StatusNet
 * @author    Nick Holliday <n.g.holliday@gmail.com>
 * @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 <n.g.holliday@gmail.com>
 * @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->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 userAgent()
    {
        return 'SpotifyPlugin/'.SPOTIFYPLUGIN_VERSION .
               ' StatusNet/' . STATUSNET_VERSION;
    }
Siebrand Mazeland's avatar
Siebrand Mazeland committed
75 76 77 78 79 80 81 82 83 84 85 86 87

    function onPluginVersion(&$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 <a href="http://www.spotify.com">Spotify</a> URLs.'));

        return true;
    }
88 89
}

Siebrand Mazeland's avatar
Siebrand Mazeland committed
90
// @todo FIXME: This probably should not be global functions.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
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>";
}