Skip to content

Commit

Permalink
0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Adinan committed Dec 31, 2020
0 parents commit 668ce39
Show file tree
Hide file tree
Showing 9 changed files with 385 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vendor/
phpunit.xml
composer.lock
test-phpunit.sh
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Shoutcast
A library to fetch radio-browser.info radio stations.


## License
MIT
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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/"
}
}
}
24 changes: 24 additions & 0 deletions examples/a-get-tags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
use AdinanCenci\RadioBrowser\RadioBrowser;

//-----------------------------

error_reporting(E_ALL);
ini_set('display_errors', 1);

//-----------------------------

require '../vendor/autoload.php';

//-----------------------------

$browser = new RadioBrowser();
$filter = 'metal';
$orderBy = 'stationcount';
$reverse = true; // ( decrescent )
$json = $browser->getTags($filter, $orderBy, $reverse);
$data = json_decode($json, true);

echo '<pre>';
print_r($data);
echo '</pre>';
27 changes: 27 additions & 0 deletions examples/b-get-stations-by-tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
use AdinanCenci\RadioBrowser\RadioBrowser;

//-----------------------------

error_reporting(E_ALL);
ini_set('display_errors', 1);

//-----------------------------

require '../vendor/autoload.php';

//-----------------------------

$browser = new RadioBrowser();
$tag = 'Metal';
$orderBy = 'clickcount';
$reverse = true; // ( decrescent )
$hideBroken = false;
$offset = 0;
$limit = 50;
$json = $browser->getStationsByTag($tag, $orderBy, $reverse, $hideBroken, $offset, $limit);
$data = json_decode($json, true);

echo '<pre>';
print_r($data);
echo '</pre>';
17 changes: 17 additions & 0 deletions examples/y-get-servers-ips.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
use AdinanCenci\RadioBrowser\RadioBrowser;

//-----------------------------

error_reporting(E_ALL);
ini_set('display_errors', 1);

//-----------------------------

require '../vendor/autoload.php';

//-----------------------------

echo '<pre>';
print_r(RadioBrowser::getServersIps());
echo '<pre>';
15 changes: 15 additions & 0 deletions examples/z-pick-a-server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
use AdinanCenci\RadioBrowser\RadioBrowser;

//-----------------------------

error_reporting(E_ALL);
ini_set('display_errors', 1);

//-----------------------------

require '../vendor/autoload.php';

//-----------------------------

echo RadioBrowser::pickAServer();
237 changes: 237 additions & 0 deletions src/RadioBrowser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
<?php
namespace AdinanCenci\RadioBrowser;

use \AdinanCenci\SimpleRequest\Request;

class RadioBrowser
{
protected $server = null;

public function __construct($server = 'https://de1.api.radio-browser.info/')
{
if ($server == false) {
$server = self::pickAServer();
}

$this->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/<country>/'.$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';
}
}

0 comments on commit 668ce39

Please sign in to comment.