From 0db5edae833812a9bbcdbe2c7617588e2bdde669 Mon Sep 17 00:00:00 2001 From: elpatron68 Date: Sat, 27 Jul 2019 19:53:16 +0200 Subject: [PATCH] Moved rb files to librb --- librb/__init__.py | 0 librb/radiobrowser.py | 27 +++++++ librb/rbConstants.py | 16 ++++ librb/rbRadios.py | 183 ++++++++++++++++++++++++++++++++++++++++++ mumbleBot.py | 2 +- 5 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 librb/__init__.py create mode 100644 librb/radiobrowser.py create mode 100644 librb/rbConstants.py create mode 100644 librb/rbRadios.py diff --git a/librb/__init__.py b/librb/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/librb/radiobrowser.py b/librb/radiobrowser.py new file mode 100644 index 0000000..2318cbe --- /dev/null +++ b/librb/radiobrowser.py @@ -0,0 +1,27 @@ +from rbRadios import RadioBrowser + +rb = RadioBrowser() + +def getstations_byname(query): + results = rb.stations_byname(query) + stations = [] + for st in results: + try: + # url = rb.playable_station(st['id'])['url'] + station = {'stationname': st['name'], 'id':st['id']} + stations.append(station) + except: + pass + return stations + +def geturl_byid(id): + url = rb.playable_station(id)['url'] + if url != None: + return url + else: + return "-1" + + +if __name__ == "__main__": + r = getstations_byname('r.sh') + pass \ No newline at end of file diff --git a/librb/rbConstants.py b/librb/rbConstants.py new file mode 100644 index 0000000..bd0f121 --- /dev/null +++ b/librb/rbConstants.py @@ -0,0 +1,16 @@ +BASE_URL = "http://www.radio-browser.info/webservice/" + +endpoints = { + "countries": {1: "{fmt}/countries", 2: "{fmt}/countries/{filter}"}, + "codecs": {1: "{fmt}/codecs", 2: "{fmt}/codecs/{filter}"}, + "states": { + 1: "{fmt}/states", + 2: "{fmt}/states/{filter}", + 3: "{fmt}/states/{country}/{filter}", + }, + "languages": {1: "{fmt}/languages", 2: "{fmt}/languages/{filter}"}, + "tags": {1: "{fmt}/tags", 2: "{fmt}/tags/{filter}"}, + "stations": {1: "{fmt}/stations", 3: "{fmt}/stations/{by}/{search_term}"}, + "playable_station": {3: "{ver}/{fmt}/url/{station_id}"}, + "station_search": {1: "{fmt}/stations/search"}, +} diff --git a/librb/rbRadios.py b/librb/rbRadios.py new file mode 100644 index 0000000..7fc5bd4 --- /dev/null +++ b/librb/rbRadios.py @@ -0,0 +1,183 @@ +import requests + +from xml.etree import ElementTree +from urllib.parse import urljoin + +from rbConstants import endpoints, BASE_URL + + +def request(endpoint, **kwargs): + + fmt = kwargs.get("format", "json") + + if fmt == "xml": + content_type = f"application/{fmt}" + else: + content_type = f"application/{fmt}" + + headers = {"content-type": content_type, "User-Agent": "pyradios/dev"} + + params = kwargs.get("params", {}) + + url = BASE_URL + endpoint + + resp = requests.get(url, headers=headers, params=params) + + if resp.status_code == 200: + if fmt == "xml": + return resp.text + return resp.json() + + return resp.raise_for_status() + + +class EndPointBuilder: + def __init__(self, fmt="json"): + self.fmt = fmt + self._option = None + self._endpoint = None + + @property + def endpoint(self): + return endpoints[self._endpoint][self._option] + + def produce_endpoint(self, **parts): + self._option = len(parts) + self._endpoint = parts["endpoint"] + parts.update({"fmt": self.fmt}) + return self.endpoint.format(**parts) + + +class RadioBrowser: + def __init__(self, fmt="json"): + self.fmt = fmt + self.builder = EndPointBuilder(fmt=self.fmt) + + def countries(self, filter=""): + endpoint = self.builder.produce_endpoint(endpoint="countries") + return request(endpoint) + + def codecs(self, filter=""): + endpoint = self.builder.produce_endpoint(endpoint="codecs") + return request(endpoint) + + def states(self, country="", filter=""): + endpoint = self.builder.produce_endpoint( + endpoint="states", country=country, filter=filter + ) + return request(endpoint) + + def languages(self, filter=""): + endpoint = self.builder.produce_endpoint(endpoint="languages", filter=filter) + return request(endpoint) + + def tags(self, filter=""): + endpoint = self.builder.produce_endpoint(endpoint="tags", filter=filter) + return request(endpoint) + + def stations(self, **params): + endpoint = self.builder.produce_endpoint(endpoint="stations") + kwargs = {} + if params: + kwargs.update({"params": params}) + return request(endpoint, **kwargs) + + def stations_byid(self, id): + endpoint = self.builder.produce_endpoint( + endpoint="stations", by="byid", search_term=id + ) + return request(endpoint) + + def stations_byuuid(self, uuid): + endpoint = self.builder.produce_endpoint( + endpoint="stations", by="byuuid", search_term=uuid + ) + return request(endpoint) + + def stations_byname(self, name): + endpoint = self.builder.produce_endpoint( + endpoint="stations", by="byname", search_term=name + ) + return request(endpoint) + + def stations_bynameexact(self, nameexact): + endpoint = self.builder.produce_endpoint( + endpoint="stations", by="bynameexact", search_term=nameexact + ) + return request(endpoint) + + def stations_bycodec(self, codec): + endpoint = self.builder.produce_endpoint( + endpoint="stations", by="bycodec", search_term=codec + ) + return request(endpoint) + + def stations_bycodecexact(self, codecexact): + endpoint = self.builder.produce_endpoint( + endpoint="stations", by="bycodecexact", search_term=codecexact + ) + return request(endpoint) + + def stations_bycountry(self, country): + endpoint = self.builder.produce_endpoint( + endpoint="stations", by="bycountry", search_term=country + ) + return request(endpoint) + + def stations_bycountryexact(self, countryexact): + endpoint = self.builder.produce_endpoint( + endpoint="stations", by="bycountryexact", search_term=countryexact + ) + return request(endpoint) + + def stations_bystate(self, state): + endpoint = self.builder.produce_endpoint( + endpoint="stations", by="bystate", search_term=state + ) + return request(endpoint) + + def stations_bystateexact(self, stateexact): + endpoint = self.builder.produce_endpoint( + endpoint="stations", by="bystateexact", search_term=stateexact + ) + return request(endpoint) + + # + def stations_bylanguage(self, language): + endpoint = self.builder.produce_endpoint( + endpoint="stations", by="bylanguage", search_term=language + ) + return request(endpoint) + + def stations_bylanguageexact(self, languageexact): + endpoint = self.builder.produce_endpoint( + endpoint="stations", by="bylanguageexact", search_term=languageexact + ) + return request(endpoint) + + def stations_bytag(self, tag): + endpoint = self.builder.produce_endpoint( + endpoint="stations", by="bytag", search_term=tag + ) + return request(endpoint) + + def stations_bytagexact(self, tagexact): + endpoint = self.builder.produce_endpoint( + endpoint="stations", by="bytagexact", search_term=tagexact + ) + return request(endpoint) + + def playable_station(self, station_id): + endpoint = self.builder.produce_endpoint( + endpoint="playable_station", station_id=station_id, ver="v2" + ) + + return request(endpoint) + + def station_search(self, params, **kwargs): + # http://www.radio-browser.info/webservice#Advanced_station_search + assert isinstance(params, dict), "params is not a dict" + kwargs["params"] = params + endpoint = self.builder.produce_endpoint(endpoint="station_search") + return request(endpoint, **kwargs) + \ No newline at end of file diff --git a/mumbleBot.py b/mumbleBot.py index 15e2f8a..3333b22 100644 --- a/mumbleBot.py +++ b/mumbleBot.py @@ -27,7 +27,7 @@ import media.file import media.playlist import media.radio import media.system -from lib import radiobrowser +from librb import radiobrowser """ FORMAT OF A MUSIC INTO THE PLAYLIST