]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Spotify/SpotifyPlugin.php
Merge branch '1.0.x' 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
51 class SpotifyPlugin extends Plugin
52 {
53
54     function __construct()
55     {
56         parent::__construct();
57     }
58
59     function onStartNoticeSave($notice)
60     {
61         $notice->rendered = preg_replace_callback('/spotify:[a-z]{5,6}:[a-z0-9]{22}/i',
62                                                   "renderSpotifyURILink",
63                                                   $notice->rendered);
64
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",
67                                                   $notice->rendered);
68
69         return true;
70     }
71
72     function userAgent()
73     {
74         return 'SpotifyPlugin/'.SPOTIFYPLUGIN_VERSION .
75                ' StatusNet/' . STATUSNET_VERSION;
76     }
77 }
78
79 function doSpotifyLookup($uri, $isArtist)
80 {
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());
85
86         if($isArtist)
87             return $xml->name;
88         else
89             return $xml->artist->name . ' - ' . $xml->name;
90     }
91 }
92
93 function renderSpotifyURILink($match)
94 {
95     $isArtist = false;
96     if(preg_match('/artist/', $match[0]) > 0) $isArtist = true;
97
98     $name = doSpotifyLookup($match[0], $isArtist);
99     return "<a href=\"{$match[0]}\">" . $name . "</a>";
100 }
101
102 function renderSpotifyHTTPLink($match)
103 {
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]);
107
108     $isArtist = false;
109     if(preg_match('/artist/', $match[0]) > 0) $isArtist = true;
110
111     $name = doSpotifyLookup($match[0], $isArtist);
112     return "<a href=\"{$match[0]}\">" . $name . "</a>";
113 }