From 668ce39c552b1b3623298909262c75708bc49d18 Mon Sep 17 00:00:00 2001 From: Adinan Date: Thu, 31 Dec 2020 12:02:19 -0300 Subject: [PATCH] 0.1.0 --- .gitignore | 4 + LICENSE | 21 +++ README.md | 6 + composer.json | 34 +++++ examples/a-get-tags.php | 24 +++ examples/b-get-stations-by-tag.php | 27 ++++ examples/y-get-servers-ips.php | 17 +++ examples/z-pick-a-server.php | 15 ++ src/RadioBrowser.php | 237 +++++++++++++++++++++++++++++ 9 files changed, 385 insertions(+) create mode 100755 .gitignore create mode 100755 LICENSE create mode 100755 README.md create mode 100755 composer.json create mode 100755 examples/a-get-tags.php create mode 100644 examples/b-get-stations-by-tag.php create mode 100755 examples/y-get-servers-ips.php create mode 100755 examples/z-pick-a-server.php create mode 100755 src/RadioBrowser.php diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..1f953a8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +vendor/ +phpunit.xml +composer.lock +test-phpunit.sh \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..69eee5a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2020, Adinan Cenci + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100755 index 0000000..1d8ee04 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# Shoutcast +A library to fetch radio-browser.info radio stations. + + +## License +MIT \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100755 index 0000000..a62b6dc --- /dev/null +++ b/composer.json @@ -0,0 +1,34 @@ +{ + "name": "adinan-cenci/radio-browser", + "description": "A library to fetch radio-browser.info radio stations.", + "type": "library", + "keywords": ["radio", "music"], + "license": "MIT", + "version": "0.1.0", + "authors": [ + { + "name": "Adinan Cenci", + "email": "adinancenci@protonmail.com", + "role": "developer" + } + ], + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/adinan-cenci/radio-browser" + }, + { + "type": "vcs", + "url": "https://github.com/adinan-cenci/simple-request" + } + ], + "require": { + "php": ">=7.0", + "adinan-cenci/simple-request": "^1.1.0" + }, + "autoload": { + "psr-4": { + "AdinanCenci\\RadioBrowser\\": "src/" + } + } +} diff --git a/examples/a-get-tags.php b/examples/a-get-tags.php new file mode 100755 index 0000000..48ddcbe --- /dev/null +++ b/examples/a-get-tags.php @@ -0,0 +1,24 @@ +getTags($filter, $orderBy, $reverse); +$data = json_decode($json, true); + +echo '
';
+print_r($data);
+echo '
'; diff --git a/examples/b-get-stations-by-tag.php b/examples/b-get-stations-by-tag.php new file mode 100644 index 0000000..c6a1c09 --- /dev/null +++ b/examples/b-get-stations-by-tag.php @@ -0,0 +1,27 @@ +getStationsByTag($tag, $orderBy, $reverse, $hideBroken, $offset, $limit); +$data = json_decode($json, true); + +echo '
';
+print_r($data);
+echo '
'; diff --git a/examples/y-get-servers-ips.php b/examples/y-get-servers-ips.php new file mode 100755 index 0000000..2c0bec9 --- /dev/null +++ b/examples/y-get-servers-ips.php @@ -0,0 +1,17 @@ +'; +print_r(RadioBrowser::getServersIps()); +echo '
';
diff --git a/examples/z-pick-a-server.php b/examples/z-pick-a-server.php
new file mode 100755
index 0000000..82eec98
--- /dev/null
+++ b/examples/z-pick-a-server.php
@@ -0,0 +1,15 @@
+server = $server;
+    }
+
+    public function getCountries($filter = '', $order = 'name', $reverse = false, $hideBroken = false) 
+    {
+        return $this->getInfo($this->server.'json/countries/'.$filter, $order, $reverse, $hideBroken);
+    }
+
+    public function getCountryCodes($filter = '', $order = 'name', $reverse = false, $hideBroken = false) 
+    {
+        return $this->getInfo($this->server.'json/countrycodes/'.$filter, $order, $reverse, $hideBroken);
+    }
+
+    public function getCodecs($filter = '', $order = 'name', $reverse = false, $hideBroken = false) 
+    {
+        return $this->getInfo($this->server.'json/codecs/'.$filter, $order, $reverse, $hideBroken);
+    }
+
+    public function getStates($filter = '', $order = 'name', $reverse = false, $hideBroken = false, $country = null) 
+    {
+        $url       = $this->server.'json/states//'.$filter;
+        $variables = [
+            'order'      => $order, 
+            'reverse'    => (string) $reverse, 
+            'hidebroken' => (string) $hideBroken, 
+            'country'    => $country
+        ];
+        
+        return $this->request($url, $variables);
+    }
+
+    public function getLanguages($filter = '', $order = 'name', $reverse = false, $hideBroken = false) 
+    {
+        return $this->getInfo($this->server.'json/languages/'.$filter, $order, $reverse, $hideBroken);
+    }
+
+    public function getTags($filter = '', $order = 'name', $reverse = false, $hideBroken = false) 
+    {
+        return $this->getInfo($this->server.'json/tags/'.$filter, $order, $reverse, $hideBroken);
+    }
+
+    //------------------------------------
+
+    public function getStationsByUuid($uuid, $order = 'name', $reverse = false, $hideBroken = false, $offset = 0, $limit = 100000) 
+    {
+        return $this->getStationsBy($this->server.'json/stations/byuuid/'.$uuid, $order, $reverse, $hideBroken, $offset, $limit);
+    }
+
+    public function getStationsByName($name, $order = 'name', $reverse = false, $hideBroken = false, $offset = 0, $limit = 100000) 
+    {
+        return $this->getStationsBy($this->server.'json/stations/byname/'.$name, $order, $reverse, $hideBroken, $offset, $limit);
+    }
+
+    public function getStationsByExactName($name, $order = 'name', $reverse = false, $hideBroken = false, $offset = 0, $limit = 100000) 
+    {
+        return $this->getStationsBy($this->server.'json/stations/bynameexact/'.$name, $order, $reverse, $hideBroken, $offset, $limit);
+    }
+
+    public function getStationsByCodec($codec, $order = 'name', $reverse = false, $hideBroken = false, $offset = 0, $limit = 100000) 
+    {
+        return $this->getStationsBy($this->server.'json/stations/bycodec/'.$codec, $order, $reverse, $hideBroken, $offset, $limit);
+    }
+
+    public function getStationsByExactCodec($codec, $order = 'name', $reverse = false, $hideBroken = false, $offset = 0, $limit = 100000) 
+    {
+        return $this->getStationsBy($this->server.'json/stations/bycodecexact/'.$codec, $order, $reverse, $hideBroken, $offset, $limit);
+    }
+
+    public function getStationsByCountry($country, $order = 'name', $reverse = false, $hideBroken = false, $offset = 0, $limit = 100000) 
+    {
+        return $this->getStationsBy($this->server.'json/stations/bycountry/'.$country, $order, $reverse, $hideBroken, $offset, $limit);
+    }
+
+    public function getStationsByExactCountry($country, $order = 'name', $reverse = false, $hideBroken = false, $offset = 0, $limit = 100000) 
+    {
+        return $this->getStationsBy($this->server.'json/stations/bycountryexact/'.$country, $order, $reverse, $hideBroken, $offset, $limit);
+    }
+
+    public function getStationsByState($state, $order = 'name', $reverse = false, $hideBroken = false, $offset = 0, $limit = 100000) 
+    {
+        return $this->getStationsBy($this->server.'json/stations/bystate/'.$state, $order, $reverse, $hideBroken, $offset, $limit);
+    }
+
+    public function getStationsByExactState($state, $order = 'name', $reverse = false, $hideBroken = false, $offset = 0, $limit = 100000) 
+    {
+        return $this->getStationsBy($this->server.'json/stations/bystateexact/'.$state, $order, $reverse, $hideBroken, $offset, $limit);
+    }
+
+    public function getStationsByLanguage($language, $order = 'name', $reverse = false, $hideBroken = false, $offset = 0, $limit = 100000) 
+    {
+        return $this->getStationsBy($this->server.'json/stations/bylanguage/'.$language, $order, $reverse, $hideBroken, $offset, $limit);
+    }
+
+    public function getStationsByExactLanguage($language, $order = 'name', $reverse = false, $hideBroken = false, $offset = 0, $limit = 100000) 
+    {
+        return $this->getStationsBy($this->server.'json/stations/bylanguageexact/'.$language, $order, $reverse, $hideBroken, $offset, $limit);
+    }
+
+    public function getStationsByTag($tag, $order = 'name', $reverse = false, $hideBroken = false, $offset = 0, $limit = 100000) 
+    {
+        $tag = rawurlencode( trim($tag, '#') );
+        return $this->getStationsBy($this->server.'json/stations/bytag/'.$tag, $order, $reverse, $hideBroken, $offset, $limit);
+    }
+
+    public function getStationsByExactTag($tag, $order = 'name', $reverse = false, $hideBroken = false, $offset = 0, $limit = 100000) 
+    {
+        $tag = rawurlencode( trim($tag, '#') );
+        return $this->getStationsBy($this->server.'json/stations/bytagexact/'.$tag, $order, $reverse, $hideBroken, $offset, $limit);
+    }
+
+    public function getStations($order = 'name', $reverse = false, $hideBroken = false, $offset = 0, $limit = 100000) 
+    {
+        return $this->getStationsBy($this->server.'json/stations/', $order, $reverse, $hideBroken, $offset, $limit);
+    }
+
+    //------------------------------------
+
+    public function getStationCheckResults($stationUuid = '', $lastCheckUuid = null, $seconds = 0) 
+    {
+        $url = $this->server.'json/checks/'.$stationUuid;
+
+        $variables = [
+            'lastcheckuuid' => $lastCheckUuid, 
+            'seconds'       => $seconds
+        ];
+
+        return $this->request($url, $variables);
+    }
+
+    public function getStationClicks($stationUuid = '', $lastClickUuid = null, $seconds = 0) 
+    {
+        $url = $this->server.'json/clicks/'.$stationUuid;
+
+        $variables = [
+            'lastclickuuid' => $lastClickUuid, 
+            'seconds'       => $seconds
+        ];
+
+        return $this->request($url, $variables);
+    }
+
+    public function clickStation($stationUuid) 
+    {
+        $url = $this->server.'json/url/'.$stationUuid;
+        return $this->request($url);
+    }
+
+    //------------------------------------
+
+    protected function getInfo($url, $order = 'name', $reverse = false, $hideBroken = false) 
+    {
+        $variables = [
+            'order'      => $order, 
+            'reverse'    => self::stringBoolean($reverse), 
+            'hidebroken' => self::stringBoolean($hideBroken)
+        ];
+
+        return $this->request($url, $variables);
+    }    
+
+    protected function getStationsBy($url, $order = 'name', $reverse = false, $hideBroken = false, $offset = 0, $limit = 100000) 
+    {
+        $variables = [
+            'order'      => $order, 
+            'reverse'    => self::stringBoolean($reverse), 
+            'hidebroken' => self::stringBoolean($hideBroken), 
+            'offset'     => $offset, 
+            'limit'      => $limit
+        ];
+
+        return $this->request($url, $variables);
+    }
+
+    protected function request($url, $fields = null) 
+    {
+        $r = new Request($url);
+        if ($fields) {
+            $r->fields($fields);
+        }
+        $response = $r->request();
+
+        if ($response->code != 200) {
+            throw new \Exception('Error requesting "'.$response->url.'", code: '.$response->code, 1);
+        }
+
+        return $response->body;
+    }
+
+    //------------------------------------
+
+    public static function getServersIps() 
+    {
+        $ips     = [];
+        $records = dns_get_record('all.api.radio-browser.info', \DNS_A);
+
+        foreach ($records as $r) {
+            $ips[] = $r['ip'];
+        }
+
+        return $ips;
+    }
+
+    // pick a random server
+    public static function pickAServer() 
+    {
+        $ips    = self::getServersIps();
+        $count  = count($ips);
+        $chosen = rand(0, $count - 1);
+        return gethostbyaddr($ips[$chosen]);
+    }
+
+    //------------------------------------
+
+    protected static function stringBoolean($value) 
+    {
+        if ($value === 'false' || $value == false) {
+            return 'false';
+        }
+
+        return 'true';
+    }
+}