SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | ||
3 | /** | |
4 | * --------------- ShoutCast Radio Stations Search API --------------- | |
5 | - | * |
5 | + | * |
6 | * @author Pedro P. L. Papadópolis (a.k.a Mandrakke). | |
7 | * @copyright There is no copyright, you can use it as you want, you can also show it to you mother to her see how much you are smart. | |
8 | - | * |
8 | + | * |
9 | * If you speak Portuguese, try out our server; | |
10 | * http://brazucas-server.com | |
11 | - | * |
11 | + | * |
12 | * If you dont, try out our translated-to-english tool (SA-MP Online Objects Viewer); | |
13 | * http://brazucas-server.com/$objetos | |
14 | - | * |
14 | + | * |
15 | - | * |
15 | + | * |
16 | * This script is also hosted on http://api.brazucas-dev.com/ShoutCastAPI.php but it can be turned offline at any time if the number of requests increase too much. | |
17 | - | * |
17 | + | * |
18 | * More information about how to integrate this script with a SA-MP server can be find at the oficial topic on SA-MP forums; | |
19 | * http://forum.sa-mp.com/showthread.php?t=347016 | |
20 | - | * |
20 | + | * |
21 | - | * |
21 | + | * |
22 | - | * |
22 | + | * |
23 | - | * Version: R1-1 Beta (released in 02/06/2012) |
23 | + | * Version: R2 (released in 04/08/2014) |
24 | - | * |
24 | + | * |
25 | * ------------------------------------------------------------------- | |
26 | */ | |
27 | - | |
27 | + | |
28 | if(!function_exists("curl_init")) die("cURL library is required."); | |
29 | ||
30 | //Disable error messages, if you have issues with this script, comment this line below to see which error occurs. | |
31 | - | error_reporting("E_ALL"); |
31 | + | error_reporting("E_ALL"); |
32 | ||
33 | final class ShoutCastAPI { | |
34 | private $forbidden = array('-','!','"','#','$','%','&','\'','(',')','*','+',',','.','/',':',';','<','=','>','?','@','[','\\',']','_','`','{','|','}','~'); | |
35 | - | |
35 | + | private $shoutcastUrlPrefix = 'http://www.shoutcast.com/'; |
36 | - | function __construct() { |
36 | + | |
37 | function __construct() { | |
38 | - | "ajax" => true, |
38 | + | |
39 | - | "count" => 10, |
39 | + | "genre" => ($_GET['genre'])?(int)$_GET['genre']:null, |
40 | - | "order" => "desc", |
40 | + | "station" => ($_GET['station'])?(int)$_GET['station']:null, |
41 | - | "strIndex" => 0 |
41 | + | "type" => ($_GET['type'])?(int)$_GET['type']:null, |
42 | "artist" => ($_GET['artist'])?(int)$_GET['artist']:null, | |
43 | - | |
43 | + | "song" => ($_GET['song'])?(int)$_GET['song']:null |
44 | - | if(!isset($_GET["search"]) || strlen($_GET["search"]) == 0) { |
44 | + | |
45 | - | $url = "http://www.shoutcast.com/ajax_dir"; |
45 | + | |
46 | - | |
46 | + | $validParams = false; |
47 | - | if(!isset($_GET["field"])) |
47 | + | foreach($_GET as $ind => $val) |
48 | - | $RequestValues["criteria"] = "listenershead"; |
48 | + | foreach($RequestValues as $searchInd => $searchVal) |
49 | - | else |
49 | + | if($searchInd == $ind) { |
50 | - | $RequestValues["criteria"] = $_GET["field"]."head"; |
50 | + | $validParams = true; |
51 | break; | |
52 | - | $url = "http://www.shoutcast.com/search-ajax/". urlencode($_GET["search"]); |
52 | + | } |
53 | - | |
53 | + | |
54 | - | if(!isset($_GET["field"])) |
54 | + | if(isset($_GET['getStreamUrl'])) { |
55 | - | $RequestValues["mode"] = "listenershead2"; |
55 | + | $url = $this->shoutcastUrlPrefix. 'Player/GetStreamUrl'; |
56 | - | else |
56 | + | } else if(count($_GET) > 0 && $validParams) { |
57 | - | $RequestValues["mode"] = $_GET["field"]."head2"; |
57 | + | $url = $this->shoutcastUrlPrefix. 'Search/UpdateAdvancedSearch'; |
58 | } else { | |
59 | - | |
59 | + | $url = $this->shoutcastUrlPrefix. 'Home/Top'; |
60 | } | |
61 | ||
62 | $count = ($_GET['count'])?(int)$_GET['count']:10; | |
63 | $start = ($_GET['start'])?(int)$_GET['start']:0; | |
64 | - | |
64 | + | $show = ($_GET['show'])?explode('|', $_GET['show']):false; |
65 | ||
66 | $params = null; | |
67 | - | |
67 | + | |
68 | if($_GET[$ind] !== null) | |
69 | - | |
69 | + | |
70 | - | $req = $this->Request($url, 1, null, $params, "http://www.shoutcast.com/"); |
70 | + | |
71 | $params .= $ind. "=". $RequestValues[$ind]. "&"; | |
72 | - | $filter = new DOMDocument; |
72 | + | |
73 | - | @$filter->loadHTML($req["data"][1]); |
73 | + | |
74 | - | |
74 | + | |
75 | - | $divs = $filter->getElementsByTagName("div"); |
75 | + | |
76 | $req = $this->Request($url, 1, null, $params, $this->shoutcastUrlPrefix); | |
77 | - | foreach($divs as $div) { |
77 | + | |
78 | - | if($div->getAttribute("class") == "dirlist") { |
78 | + | if(isset($_GET['getStreamUrl'])) die($req['data'][1]); |
79 | - | $a = $div->getElementsByTagName("a")->item(0); |
79 | + | |
80 | - | |
80 | + | |
81 | - | $stations["stations"]["s".$i]["stationid"] = $a->getAttribute("id"); |
81 | + | |
82 | - | |
82 | + | $response = json_decode($req['data'][1]); |
83 | - | $tmp = $a->getAttribute("name"); |
83 | + | |
84 | - | for($y = 0; $y < strlen($tmp); ++$y) |
84 | + | foreach($response as $station) { |
85 | - | if(!in_array($tmp[$y], $this->forbidden)) |
85 | + | if(isset($_GET['streamUrl'])) { |
86 | - | $stations["stations"]["s".$i]["name"] .= $tmp[$y]; |
86 | + | $stationUrlRequest = $this->Request($this->shoutcastUrlPrefix. 'Player/GetStreamUrl', 1, null, array('station' => $station->ID), $this->shoutcastUrlPrefix); |
87 | - | |
87 | + | preg_match('/"(.*?)"/', $stationUrlRequest['data'][2], $stationUrl); |
88 | - | $stations["stations"]["s".$i]["id"] = preg_replace("/^.*?id./i", "", $a->getAttribute("href")); |
88 | + | |
89 | - | |
89 | + | $stations['stations']['s'. $i]['streamUrl'] = $stationUrl[1]; |
90 | - | $childDivs = $div->getElementsByTagName("div"); |
90 | + | |
91 | - | foreach($childDivs as $childDiv) |
91 | + | |
92 | - | switch($childDiv->getAttribute("class")) { |
92 | + | foreach($station as $ind => $val) |
93 | - | case "playingtext": |
93 | + | if($i >= $start && (in_array($ind, $show) || !$show)) |
94 | - | $tmp = $childDiv->getAttribute("title"); |
94 | + | $stations['stations']['s'. $i][$ind] = $val; |
95 | - | |
95 | + | |
96 | - | if($tmp == null && $childDiv->getElementsByTagName("span")->item(0) !== null) |
96 | + | if($i > $count) break; |
97 | - | $tmp = $childDiv->getElementsByTagName("span")->item(0)->getAttribute("title"); |
97 | + | ++$i; |
98 | - | |
98 | + | |
99 | - | for($a = 0; $a < strlen($tmp); ++$a) |
99 | + | |
100 | - | if(!in_array($tmp[$a], $this->forbidden)) |
100 | + | |
101 | - | $stations["stations"]["s".$i]["nowplaying"] .= $tmp[$a]; |
101 | + | |
102 | - | |
102 | + | |
103 | - | if(strlen($stations["stations"]["s".$i]["nowplaying"]) > 13) |
103 | + | |
104 | - | $stations["stations"]["s".$i]["nowplaying"] = substr($stations["stations"]["s".$i]["nowplaying"], 0, 13). "..."; |
104 | + | |
105 | - | break; |
105 | + | |
106 | - | case "dirgenre": $stations["stations"]["s".$i]["genre"] = $childDiv->nodeValue; break; |
106 | + | |
107 | - | case "dirlistners": $stations["stations"]["s".$i]["listeners"] = $childDiv->nodeValue; break; |
107 | + | |
108 | - | case "dirbitrate": $stations["stations"]["s".$i]["bitrate"] = $childDiv->nodeValue; break; |
108 | + | |
109 | - | case "dirtype": $stations["stations"]["s".$i]["type"] = $childDiv->nodeValue; break; |
109 | + | |
110 | - | } |
110 | + | |
111 | - | |
111 | + | |
112 | - | ++$i; |
112 | + | |
113 | $output = json_encode($stations); | |
114 | break; | |
115 | - | |
115 | + | |
116 | default: | |
117 | - | |
117 | + | |
118 | foreach($stations["stations"] as $stationId => $stationData) | |
119 | if(is_array($stationData)) | |
120 | - | |
120 | + | |
121 | $output .= $stationId. $ind. "=". $val. "\r\n"; | |
122 | else | |
123 | $output .= $stationId. "=". $stationData. "\r\n"; | |
124 | } | |
125 | ||
126 | - | |
126 | + | |
127 | } | |
128 | ||
129 | - | $output .= json_encode($stations); |
129 | + | |
130 | - | break; |
130 | + | |
131 | ||
132 | $exec = curl_setopt_array($ch, array( | |
133 | CURLOPT_URL => $a, | |
134 | CURLOPT_RETURNTRANSFER => $b, | |
135 | CURLOPT_HEADER => 1, | |
136 | CURLOPT_NOBODY => 0, | |
137 | CURLOPT_COOKIE => $c, | |
138 | CURLOPT_CUSTOMREQUEST => ($d)?"POST":"GET", | |
139 | - | $output .= $stationId. "=". $stationData. "\r\n"; |
139 | + | |
140 | CURLOPT_FOLLOWLOCATION => 1, | |
141 | - | |
141 | + | |
142 | CURLOPT_REFERER => $e | |
143 | ) | |
144 | - | |
144 | + | |
145 | ||
146 | return array( | |
147 | - | |
147 | + | "data" => explode("\r\n\r\n", curl_exec($ch), 3), |
148 | "httpcode" => (int)curl_getinfo($ch, CURLINFO_HTTP_CODE) | |
149 | ); | |
150 | } | |
151 | } new ShoutCastAPI(); | |
152 | ?> |