This repository was archived by the owner on Dec 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathicecast_api.php
More file actions
414 lines (305 loc) · 11.7 KB
/
Copy pathicecast_api.php
File metadata and controls
414 lines (305 loc) · 11.7 KB
1
2
3
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?php
/*
/* @package IceCast2 PHP API
/* http://github.com/okwinza/icecast2-php-api
/* @author Okwinza
/* This work is licensed under the Creative Commons Attribution 3.0 Unported License.
/* To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/
/* or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
*/
if(!defined('IN_APP')){
die('Hacking Attempt!');
}
class IcecastApi {
private $config = array();
private $result = null;
private $_memcache = null;
private $_db = null;
private $icecast_xml = null;
function __construct(array $config){
$this->config = $config;
if($this->config['use_memcached']) $this->MemcachedInit();
if($this->config['use_db']) $this->GetDB();
$this->icecast_xml = $this->Request('getDataFromIcecast')->result;
if(empty($this->icecast_xml)) die('Failed to connect to Icecast Server.');
}
//Preparing response according to its type.
// @param (array) $type
// @returns formatted xml/json or just "as is".
public function Response($type = null){
switch($type){
case 'xml':
return $this->array_to_xml($this->result);
break;
case 'json':
return $this->array_to_json($this->result);
break;
default:
return $this->result;
break;
}
}
//main factory
public function Request($method, array $args = array(), $memcachedOverride = false){
if(!empty($args['mount']) && !empty($this->config['icecast_mount_fallback_map'][$args['mount']])){
$args['mount'] = (!in_array($args['mount'],$this->listMounts(true))) ? $this->config['icecast_mount_fallback_map'][$args['mount']] : $args['mount'];
}
$this->result = (($this->config['use_memcached'] === true) && ($memcachedOverride === false)) ? $this->WithMemcached($method , $args) : $this->{$method . 'Action'}($args);
return $this;
}
public function listMounts($only_active = false){
$xml = simplexml_load_string($this->icecast_xml);
if($only_active){
$xml = $xml->xpath("/icestats/source[source_ip != '']/@mount");
}else{
$xml = $xml->xpath("/icestats/source/@mount");
}
// to array
$json = json_encode($xml);
$array = json_decode($json,TRUE);
$list = array();
foreach($array as $key => $item){
$list[] = str_replace('/','',$item['@attributes']['mount']);
}
return $list;
}
//Memcached wrapper.
private function WithMemcached($method, array $args){
$key = $_SERVER['SERVER_NAME'].'_'.$method;
foreach($args as $k => $val){
$key .= '_'.$val;
}
$data = $this->_memcache->get($key);
if(empty($data)){
$data = $this->{$method . 'Action'}($args);
$this->_memcache->set($key,$data,$this->config['memcached']['compressed'],$this->config['memcached']['lifetime']);
}
return $data;
}
/*
Getting number of listeners for given mountpoint
@param array $args
@return array
*/
private function GetListenersAction(array $args){
extract($args); // $mount
$xml = simplexml_load_string($this->icecast_xml);
$xml = $xml->xpath("/icestats/source[@mount='/".$mount."']/listeners");
//to array
$json = json_encode($xml);
$array = json_decode($json,TRUE);
return array('listeners' => $array[0][0]);
}
/*
Getting total number of listeners
@param array $args - empty array
@return array
*/
private function GetTotalListenersAction(array $args){
$xml = simplexml_load_string($this->icecast_xml);
$xml = $xml->xpath("/icestats/listeners");
$json = json_encode($xml);
$array = json_decode($json,TRUE);
return array('totalListeners'=>$array[0][0]);
}
/*
Getting the current track and its timestamp for given $mount
@param array $args[$mount]
@return array
*/
private function GetTrackAction(array $args){
extract($args); // $mount
$data = $this->GetHistoryAction(array('mount'=> $mount,'amount'=>1)); //using already defined method.
return $data;
}
/*
Parsing the logfile, returning an array of with specified $amount of last tracks for given $mount
@param array $args[string $mount, int $amount]
@return array
*/
private function GetHistoryAction(array $args){
extract($args); // $mount, $amount
$amount = ($amount > $this->config['max_amount_of_history']) ? $this->config['max_amount_of_history'] : $amount; // checking if number of requested songs is lower than max
$grab_lines = intval($amount * pow(count($this->listMounts()),1.2)); // amount of lines to work with
$last_lines = $this->GetLastLinesFromFile($this->config['playlist_logfile'], $grab_lines); //gettins required number of lines from the logfile
$last_lines = explode("\n",$last_lines);
array_pop($last_lines); // deleting last empty line
$last_lines = array_reverse($last_lines); // desc. order
$line_parsed = array();
$result_array = array();
$i = 0;
foreach($last_lines as $key => $line){
$line_parsed = explode("|",$line);
/*
$line_parsed[0] - timestamp of the song, i.e. 14/Apr/2013:13:52:58 +0400
$line_parsed[1] - mountpoint of the song, i.e. /trance
$line_parsed[2] - icecast's ID of the song(or something like that), i.e. 36
$line_parsed[3] - full title of the song, i.e. Pakito - You Wanna Rock
*/
if($line_parsed[1] == "/".$mount){
if(empty($line_parsed[3])) continue; //empty song title, skipping
if($i < $amount){
if($this->config['fix_non_utf8_encoding'] == true){
$line_parsed[3] = mb_convert_encoding($line_parsed[3],'UTF-8',$this->config['mp3_title_charset']);
}
$song_parts = explode("-",htmlspecialchars($line_parsed[3])); // exploding to artist and title
$result_array[$i]['track'] = htmlspecialchars($line_parsed[3]);
$result_array[$i]['title'] = (!empty($song_parts[1])) ? trim($song_parts[1]) : 'Unknown Title'; //only title, i.e. You Wanna Rock
$result_array[$i]['artist'] = (!empty($song_parts[0])) ? trim($song_parts[0]) : 'Unknown Artist'; //only artist, i.e. Pakito
$result_array[$i]['timestamp'] = strtotime($line_parsed[0]); //unixtime
$result_array[$i]['album_art_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/cover/'.urlencode($result_array[$i]['artist']).'/'.urlencode($result_array[$i]['title']);
$result_array[$i]['artist_image_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/cover/'.urlencode($result_array[$i]['artist']);
$i++;
}else break;
}
}
return $result_array;
}
private function GetAlbumArtAction(array $args){
require_once('gracenote-php/Gracenote.class.php');
if(!is_writable($this->config['album_art_folder'])){
die('album art folder is not writable.');
}
$default_img = $this->config['default_storage_folder'] . '404.jpg';
$filename = $this->config['album_art_folder'] . md5($args['artist'] . $args['song']) . '.jpg';
if(file_exists($filename)){ //found in cache, returning.
if(filesize($filename) == 0){
return $default_img;
}
return $filename;
}
if(!empty($this->config['gracenote']['userID'])){
$gracenote_api = new Gracenote\WebAPI\GracenoteWebAPI($this->config['gracenote']['clientID'], $this->config['gracenote']['clientTag'], $this->config['gracenote']['userID']);
}else{
die('Get your Gracenote userID via <a href="/gracenote-php/register.php">register.php</a> to continue.');
}
$result = array();
//querying the GraceNote API
try
{
$result = $gracenote_api->searchTrack($args['artist'], '',$args['song'], Gracenote\WebAPI\GracenoteWebAPI::BEST_MATCH_ONLY);
}
catch( Exception $e )
{
return $default_img; // something went wrong, returning dummy picture
}
if(empty($result[0]['album_art_url'])){
touch($filename);
return $default_img; // something went wrong, returning dummy picture
}
$album_art = file_get_contents($result[0]['album_art_url']);
if(touch($filename) && file_put_contents($filename , $album_art)){ //attempt to create a cache image
return $filename; // everything is ok
}else{
return $default_img; // something went wrong, returning dummy picture;
}
return $default_img;
}
private function GetArtistArtAction(array $args){
require_once('gracenote-php/Gracenote.class.php');
if(!is_writable($this->config['artist_art_folder'])){
die('artist art folder is not writable.');
}
$default_img = $this->config['default_storage_folder'] . '404.jpg';
$filename = $this->config['artist_art_folder'] . md5($args['artist']) . '.jpg';
if(file_exists($filename)){ //found in cache, returning.
if(filesize($filename) == 0){
return $default_img;
}
return $filename;
}
if(!empty($this->config['gracenote']['userID'])){
$gracenote_api = new Gracenote\WebAPI\GracenoteWebAPI($this->config['gracenote']['clientID'], $this->config['gracenote']['clientTag'], $this->config['gracenote']['userID']);
}else{
die('Get your Gracenote userID via <a href="/gracenote-php/register.php">register.php</a> to continue.');
}
$result = array();
//querying the GraceNote API
try
{
$result = $gracenote_api->searchArtist($args['artist'], Gracenote\WebAPI\GracenoteWebAPI::BEST_MATCH_ONLY);
}
catch( Exception $e )
{
return $default_img; // something went wrong, returning dummy picture
}
if(empty($result[0]['artist_image_url'])){ //empty response, probably wrong artist name. Caching.
touch($filename);
return $default_img; // something went wrong, returning dummy picture
}
$artist_art = file_get_contents($result[0]['artist_image_url']);
if(touch($filename) && file_put_contents($filename , $artist_art)){ //attempt to create a cache image
return $filename; // everything is ok
}else{
return $default_img; // something went wrong, returning dummy picture;
}
return $default_img;
}
/*
private function YourCustomMethodAction(array $args){
return array('Hello' => 'Im a template for your custom methods.');
}
*/
private function MemcachedInit(){
$this->_memcache = new Memcache();
$this->_memcache->connect($this->config['memcached']['host'], $this->config['memcached']['port']);
}
private function GetDB(){
//setup your db interface here...
//$this->_db = your DB object.
}
private function getDataFromIcecastAction(){
$process = curl_init($this->config['icecast_server_hostname'].':'.$this->config['icecast_server_port'].'/admin/stats');
curl_setopt($process, CURLOPT_USERPWD, $this->config['icecast_admin_username'] . ":" . $this->config['icecast_admin_password']);
curl_setopt($process, CURLOPT_TIMEOUT, 5);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
return curl_exec($process);
}
private function array_to_xml(array $array, $xml=null){
if ($xml == null)
{
$xml = simplexml_load_string("<?xml version='1.0' encoding='UTF-8'?><". $this->config['xmlrootnode'] ."/>");
}
foreach($array as $key => $value)
{
if (is_numeric($key))
{
$key = 'item';
}
$key = preg_replace('/[^a-z]/i', '', $key);
if (is_array($value))
{
$node = $xml->addChild($key);
$this->array_to_xml($value, $node);
}
else
{
$value = mb_convert_encoding($value, 'UTF-8');
$xml->addChild($key,$value);
}
}
return $xml->asXML();
}
private function array_to_json(array $array){
return json_encode($array);
}
private function GetLastLinesFromFile($filename, $lines)
{
$offset = -1;
$c = '';
$read = '';
$i = 0;
$fp = @fopen($filename, "r");
while( $lines && fseek($fp, $offset, SEEK_END) >= 0 ) {
$c = fgetc($fp);
if($c == "\n" || $c == "\r"){
$lines--;
}
$read .= $c;
$offset--;
}
fclose ($fp);
return strrev(rtrim($read,"\n\r"));
}
}
?>