]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Spotify/SpotifyPlugin.php
Merge commit 'refs/merge-requests/165' of git://gitorious.org/statusnet/mainline...
[quix0rs-gnu-social.git] / plugins / Spotify / SpotifyPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to create pretty Spotify URLs
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
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/
28  *
29  * @see      Event
30  */
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34 define('SPOTIFYPLUGIN_VERSION', '0.1');
35
36 /**
37  * Plugin to create pretty Spotify URLs
38  *
39  * The Spotify API is called before the notice is saved to gather artist and track information.
40  *
41  * @category  Plugin
42  * @package   StatusNet
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/
47  *
48  * @see       Event
49  */
50 class SpotifyPlugin extends Plugin
51 {
52     function __construct()
53     {
54         parent::__construct();
55     }
56
57     function onStartNoticeSave($notice)
58     {
59         $notice->rendered = preg_replace_callback('/spotify:[a-z]{5,6}:[a-z0-9]{22}/i',
60                                                   "renderSpotifyURILink",
61                                                   $notice->rendered);
62
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",
65                                                   $notice->rendered);
66
67         return true;
68     }
69
70     function userAgent()
71     {
72         return 'SpotifyPlugin/'.SPOTIFYPLUGIN_VERSION .
73                ' StatusNet/' . STATUSNET_VERSION;
74     }
75
76     function onPluginVersion(&$versions)
77     {
78         $versions[] = array('name' => 'Spotify',
79                             'version' => SPOTIFYPLUGIN_VERSION,
80                             'author' => 'Nick Holliday',
81                             'homepage' => 'http://status.net/wiki/Plugin:Spotify',
82                             'rawdescription' =>
83                             // TRANS: Plugin description.
84                             _m('Create pretty <a href="http://www.spotify.com">Spotify</a> URLs.'));
85
86         return true;
87     }
88 }
89
90 // @todo FIXME: This probably should not be global functions.
91 function doSpotifyLookup($uri, $isArtist)
92 {
93     $request = HTTPClient::start();
94     $response = $request->get('http://ws.spotify.com/lookup/1/?uri=' . $uri);
95     if ($response->isOk()) {
96         $xml = simplexml_load_string($response->getBody());
97
98         if($isArtist)
99             return $xml->name;
100         else
101             return $xml->artist->name . ' - ' . $xml->name;
102     }
103 }
104
105 function renderSpotifyURILink($match)
106 {
107     $isArtist = false;
108     if(preg_match('/artist/', $match[0]) > 0) $isArtist = true;
109
110     $name = doSpotifyLookup($match[0], $isArtist);
111     return "<a href=\"{$match[0]}\">" . $name . "</a>";
112 }
113
114 function renderSpotifyHTTPLink($match)
115 {
116     $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]);
117     $match[0] = preg_replace('/<\/a>/', '', $match[0]);
118     $match[0] = preg_replace('/\//', ':', $match[0]);
119
120     $isArtist = false;
121     if(preg_match('/artist/', $match[0]) > 0) $isArtist = true;
122
123     $name = doSpotifyLookup($match[0], $isArtist);
124     return "<a href=\"{$match[0]}\">" . $name . "</a>";
125 }