-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata.php
More file actions
82 lines (61 loc) · 2.53 KB
/
metadata.php
File metadata and controls
82 lines (61 loc) · 2.53 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
<?php
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
function get_web_content($requestedStation){
// create curl resource
$ch = curl_init();
$header = array();
$header[] = 'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
$header[] = 'Cache-Control: max-age=0';
$header[] = 'Connection: keep-alive';
$header[] = 'Keep-Alive: 300';
$header[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
$header[] = 'Accept-Language: en-us,en;q=0.5';
$header[] = 'Pragma: ';
// set url
curl_setopt($ch, CURLOPT_URL, $requestedStation->metadata);
// set user agent
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
// $output contains the output string
$resultPageString = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
return get_string_between($resultPageString, '<body>', '</body>');
}
$stations = json_decode(file_get_contents('./stations.json'));
$requestedStationName = htmlspecialchars($_GET["name"]);
//$requestedStationName = htmlspecialchars('NERadio.fm');
$requestedStation = new stdClass();
foreach($stations as $station){
if($station->name==$requestedStationName){
$requestedStation = $station;
}
}
$response = new stdClass();
$response->station = $requestedStation;
$response->metadata = new stdClass();
if($requestedStation->type==='shoutcast'){
$bodyContent = get_web_content($requestedStation);
$pos = strrpos($bodyContent, ',');
$id = $pos === false ? $bodyContent : substr($bodyContent, $pos + 1);
$response->metadata->title = trim($id);
}
elseif($requestedStation->type==='icecast2'){
$bodyContent = get_web_content($requestedStation);
$id = get_string_between($bodyContent, '<td>Current Song:</td><td class="streamdata">', '</td></tr>');
$response->metadata->title = trim($id);
}
// Response the user
echo(json_encode($response));