]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Spotify/SpotifyPlugin.php
Merge remote-tracking branch 'upstream/master' into social-master
[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 $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 onPluginVersion(array &$versions)
71     {
72         $versions[] = array('name' => 'Spotify',
73                             'version' => SPOTIFYPLUGIN_VERSION,
74                             'author' => 'Nick Holliday',
75                             'homepage' => 'http://status.net/wiki/Plugin:Spotify',
76                             'rawdescription' =>
77                             // TRANS: Plugin description.
78                             _m('Create pretty <a href="http://www.spotify.com">Spotify</a> URLs.'));
79
80         return true;
81     }
82 }
83
84 // @todo FIXME: This probably should not be global functions.
85 function doSpotifyLookup($uri, $isArtist)
86 {
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());
91
92         if($isArtist)
93             return $xml->name;
94         else
95             return $xml->artist->name . ' - ' . $xml->name;
96     }
97 }
98
99 function renderSpotifyURILink($match)
100 {
101     $isArtist = false;
102     if(preg_match('/artist/', $match[0]) > 0) $isArtist = true;
103
104     $name = doSpotifyLookup($match[0], $isArtist);
105     return "<a href=\"{$match[0]}\">" . $name . "</a>";
106 }
107
108 function renderSpotifyHTTPLink($match)
109 {
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]);
113
114     $isArtist = false;
115     if(preg_match('/artist/', $match[0]) > 0) $isArtist = true;
116
117     $name = doSpotifyLookup($match[0], $isArtist);
118     return "<a href=\"{$match[0]}\">" . $name . "</a>";
119 }