forked from JanisV/Rompr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetInternetPlaylist.php
More file actions
371 lines (315 loc) · 12.3 KB
/
Copy pathgetInternetPlaylist.php
File metadata and controls
371 lines (315 loc) · 12.3 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
<?php
include ("vars.php");
// This is for getting a remote playlist from a radio station - eg PLS or ASX files
// This script parses that remote playlist and creates a local XSPF which will be
// used for adding the stream(s) to the playlist and for putting the info into the playlist
// Called with : url : The remote playlist to download or stream to add
// station : The name of the radio station (Groove Salad)
// creator : The name of the broadcaster (Soma FM)
// image : The image to use in the playlist
// The generated playlists can be updated later if no information is known -
// the playlist will handle that when it gets stream info from mpd
include("functions.php");
header('Content-Type: text/xml; charset=utf-8');
$url = rawurldecode($_REQUEST['url']);
$station = (array_key_exists('station', $_REQUEST)) ? rawurldecode($_REQUEST['station']) : "Unknown Internet Stream";
$creator = (array_key_exists('creator', $_REQUEST)) ? rawurldecode($_REQUEST['creator']) : "Internet Radio";
$image = (array_key_exists('image', $_REQUEST)) ? rawurldecode($_REQUEST['image']) : "newimages/broadcast.png";
$usersupplied = (array_key_exists('usersupplied', $_REQUEST)) ? true : false;
debug_print("Getting Internet Stream:","RADIO_PLAYLIST");
debug_print(" url : ".$url,"RADIO_PLAYLIST");
debug_print(" station : ".$station,"RADIO_PLAYLIST");
debug_print(" creator : ".$creator,"RADIO_PLAYLIST");
debug_print(" image : ".$image,"RADIO_PLAYLIST");
debug_print(" user : ".$usersupplied,"RADIO_PLAYLIST");
$existing_album_names = array();
// Check which names we already have
$all_playlists = glob("prefs/*STREAM*.xspf");
foreach($all_playlists as $file) {
if (file_exists($file)) {
if ($url) {
if (preg_match("/".md5($url)."/", $file)) {
debug_print("We already have a playlist for URL ".$url,"RADIO_PLAYLIST");
print file_get_contents($file);
if ($usersupplied && preg_match("/^STREAM/", basename($file))) {
// Make this a USERSTREAM
$newname = "prefs/USER".basename($file);
system('mv "'.$file.'" "'.$newname.'"');
}
exit(0);
}
}
$x = simplexml_load_file($file);
$n = (string)$x->trackList->track[0]->album;
$n = preg_replace('/ \d+$/', '', $n);
if (array_key_exists($n, $existing_album_names)) {
$existing_album_names[$n]++;
} else {
$existing_album_names[$n] = 1;
}
}
}
if ($url) {
$path = $url;
$type = pathinfo($path, PATHINFO_EXTENSION);
$qpos = strpos($type, "?");
if ($qpos != false) $type = substr($type, 0, $qpos);
debug_print("Playlist Type Is ".$type,"RADIO_PLAYLIST");
if ($type != "" && $type != null) {
$playlist = null;
switch ($type) {
case "pls":
case "PLS":
$content = url_get_contents($url, 'RompR Media Player/0.1', false, true);
debug_print("Playlist Is ".$content['contents'],"RADIO_PLAYLIST");
$playlist = new plsFile($content['contents'], $url, $station, $creator, $image);
break;
case "asx";
case "ASX";
$content = url_get_contents($url, 'RompR Media Player/0.1', false, true);
debug_print("Playlist Is ".$content['contents'],"RADIO_PLAYLIST");
$playlist = new asxFile($content['contents'], $url, $station, $creator, $image);
break;
case "xspf";
case "XSPF";
$content = url_get_contents($url, 'RompR Media Player/0.1', false, true);
debug_print("Playlist Is ".$content['contents'],"RADIO_PLAYLIST");
$playlist = new xspfFile($content['contents'], $url, $station, $creator, $image);
break;
case "m3u";
case "M3U";
$content = url_get_contents($url, 'RompR Media Player/0.1', false, true);
debug_print("Playlist Is ".$content['contents'],"RADIO_PLAYLIST");
$playlist = new m3uFile($content['contents'], $url, $station, $creator, $image);
break;
default;
$playlist = new possibleStreamUrl($url, $station, $creator, $image);
break;
}
if ($playlist) {
$output = '<?xml version="1.0" encoding="utf-8"?>'."\n".
'<playlist>'."\n".
'<trackList>'."\n";
$output = $output . $playlist->getTracks();
$output = $output . "</trackList>\n</playlist>\n";
$fp = null;
if ($usersupplied) {
$fp = fopen('prefs/USERSTREAM_'.md5($url).'.xspf', 'w');
} else {
$fp = fopen('prefs/STREAM_'.md5($url).'.xspf', 'w');
}
if ($fp) {
fwrite($fp, $output);
}
fclose($fp);
print $output;
}
} else {
debug_print("Could not determine playlist type","RADIO_PLAYLIST");
}
}
// [playlist]
// numberofentries=3
// File1=http://streamer-dtc-aa04.somafm.com:80/stream/1018
// Title1=SomaFM: Groove Salad (#1 128k mp3): A nicely chilled plate of ambient/downtempo beats and grooves.
// Length1=-1
// File2=http://mp2.somafm.com:8032
// Title2=SomaFM: Groove Salad (#2 128k mp3): A nicely chilled plate of ambient/downtempo beats and grooves.
// Length2=-1
// File3=http://ice.somafm.com/groovesalad
// Title3=SomaFM: Groove Salad (Firewall-friendly 128k mp3) A nicely chilled plate of ambient/downtempo beats and grooves.
// Length3=-1
// Version=2
// For Soma FM, is called with url = playlist URL
// station = (eg) Groove Salad
// creator = Soma FM
class plsFile {
public function __construct($data, $url, $station, $creator, $image) {
$this->url = $url;
$this->station = checkStationName($station);
$this->creator = $creator;
$this->image = $image;
$parts = explode(PHP_EOL, $data);
$tracks = array();
$pointer = -1;
foreach ($parts as $line) {
$bits = explode("=", $line);
if (preg_match('/File/', $bits[0])) {
$pointer++;
$tracks[$pointer]['track'] = $bits[1];
if (!array_key_exists('title', $tracks[$pointer])) {
$tracks[$pointer]['title'] = "";
}
}
if (preg_match('/Title/', $bits[0])) {
$tracks[$pointer]['title'] = $bits[1];
$this->station = checkStationAgain($this->station, $tracks[$pointer]['title']);
}
}
$this->tracks = $tracks;
}
public function getTracks() {
$output = "";
foreach($this->tracks as $i => $track) {
$output = $output . "<track>\n";
$output = $output . xmlnode('album', $this->station);
$output = $output . xmlnode('creator', $this->creator);
$output = $output . xmlnode('image', $this->image);
if ($this->tracks[$i]['title'] != "") {
$output = $output . xmlnode('stream', $this->tracks[$i]['title']);
}
$output = $output . xmlnode('location', $this->tracks[$i]['track']).
xmlnode('compilation', 'yes').
"</track>\n";
}
return $output;
}
}
// <ASX version="3.0">
// <ABSTRACT>http://www.bbc.co.uk/5livesportsextra/</ABSTRACT>
// <TITLE>BBC Radio 5 live sports extra</TITLE>
// <AUTHOR>BBC</AUTHOR>
// <COPYRIGHT>(c) British Broadcasting Corporation</COPYRIGHT>
// <MOREINFO HREF="http://www.bbc.co.uk/5livesportsextra/" />
// <PARAM NAME="HTMLView" VALUE="http://www.bbc.co.uk/5livesportsextra/" />
// <PARAM NAME="GEO" VALUE="UK" />
// <Entry>
// <ref href="mms://wmlive-acl.bbc.co.uk/wms/bbc_ami/radio5/5spxtra_bb_live_ep1_sl0?BBC-UID=649f8b917418780a1d247f88818c39cb39a24514c0e00211d248f0c4e30ce058&SSO2-UID=" />
// </Entry>
// <Entry>
// <ref href="mms://wmlive-acl.bbc.co.uk/wms/bbc_ami/radio5/5spxtra_bb_live_eq1_sl0?BBC-UID=649f8b917418780a1d247f88818c39cb39a24514c0e00211d248f0c4e30ce058&SSO2-UID=" />
// </Entry>
class asxFile {
public function __construct($data, $url, $station, $creator, $image) {
$this->url = $url;
$this->xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->station = checkStationName(($this->xml->TITLE != null) ? $this->xml->TITLE : $station);
$this->creator = ($this->xml->AUTHOR != null) ? $this->xml->AUTHOR : $creator;
$this->image = $image;
}
public function getTracks() {
$output = "";
foreach($this->xml->Entry as $i => $r) {
$output = $output . "<track>\n".
xmlnode('stream', $this->station).
xmlnode('album', $this->station).
xmlnode('image', $this->image).
xmlnode('creator', $this->creator).
xmlnode('location', $r->ref['href']).
"</track>\n";
}
return $output;
}
}
// <playlist version="1" xmlns="http://xspf.org/ns/0/">
// <title>I Love Radio (www.iloveradio.de)</title>
// <info>http://www.iloveradio.de</info>
// <trackList>
// <track><location>http://87.230.53.70:80/iloveradio1.mp3</location></track>
// <track><location>http://80.237.157.79:80/iloveradio1.mp3</location></track>
// <track><location>http://80.237.156.44:80/iloveradio1.mp3</location></track>
// <track><location>http://87.230.100.70:80/iloveradio1.mp3</location></track>
// <track><location>http://80.237.158.62:80/iloveradio1.mp3</location></track>
// <track><location>http://80.237.157.81:80/iloveradio1.mp3</location></track>
// <track><location>http://80.237.157.76:80/iloveradio1.mp3</location></track>
// <track><location>http://80.237.158.76:80/iloveradio1.mp3</location></track>
// <track><location>http://89.149.254.214:80/iloveradio1.mp3</location></track>
// <track><location>http://80.237.157.64:80/iloveradio1.mp3</location></track>
// </trackList>
// </playlist>
class xspfFile {
public function __construct($data, $url, $station, $creator, $image) {
$this->url = $url;
$this->xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->station = checkStationName(($this->xml->title != null) ? $this->xml->title : $station);
$this->creator = ($this->xml->info != null) ? $this->xml->info : $creator;
$this->image = $image;
}
public function getTracks() {
$output = "";
foreach($this->xml->trackList->track as $i => $r) {
$output = $output . "<track>\n".
xmlnode('stream', $this->station).
xmlnode('album', $this->station).
xmlnode('image', $this->image).
xmlnode('creator', $this->creator).
xmlnode('location', $r->location).
"</track>\n";
}
return $output;
}
}
// #This is a comment
//
// http://uk1.internet-radio.com:15614/
class m3uFile {
public function __construct($data, $url, $station, $creator, $image) {
$this->url = $url;
$this->station = checkStationName($station);
$this->creator = $creator;
$this->image = $image;
$this->tracks = array();
$parts = explode(PHP_EOL, $data);
foreach ($parts as $line) {
if (preg_match('/^\#/', $line) ||
preg_match('/^\s*$/', $line)) {
} else {
array_push($this->tracks, trim($line));
}
}
}
public function getTracks() {
$output = "";
foreach($this->tracks as $i => $track) {
$output = $output . "<track>\n";
$output = $output . xmlnode('album', $this->station);
$output = $output . xmlnode('creator', $this->creator);
$output = $output . xmlnode('image', $this->image);
$output = $output . xmlnode('location', $track).
xmlnode('compilation', 'yes').
"</track>\n";
}
return $output;
}
}
// Fallback - if it's not any of the above types, treat it as a single stream URL and see what happens.
class possibleStreamUrl {
public function __construct($url, $station, $creator, $image) {
$this->url = $url;
$this->station = checkStationName($station);
$this->creator = $creator;
$this->image = $image;
}
public function getTracks() {
return "<track>\n".
xmlnode('stream', $this->station).
xmlnode('album', $this->station).
xmlnode('image', $this->image).
xmlnode('creator', $this->creator).
xmlnode('location', $this->url).
"</track>\n";
}
}
function checkStationName($station) {
global $existing_album_names;
$station = (string)$station;
if (array_key_exists($station, $existing_album_names)) {
$existing_album_names[$station]++;
$station = $station. " ".$existing_album_names[$station];
} else {
$existing_album_names[$station] = 1;
}
return $station;
}
function checkStationAgain($currenttitle, $tracktitle) {
global $existing_album_names;
$currenttitle = (string)$currenttitle;
if (preg_match('/Unknown Internet Stream/', $currenttitle)) {
$a = preg_replace('/\(.*?\)/', '', $tracktitle);
if ($a != '') {
$currenttitle = checkStationName($a);
}
}
return $currenttitle;
}
?>