Radiobrowser: Migrate to new API

Also fix up attributes and countrycode mapping as outlined in migration suggestions at https://api.radio-browser.info/

Co-authored-by: Thomas Endt <thomas.endt@uipath.com>
Co-authored-by: Jonathan Rawle <git@jonathanrawle.co.uk>
This commit is contained in:
milaq 2020-10-10 15:20:22 +02:00
parent 1c296ec089
commit 0a2aaa163d
2 changed files with 12 additions and 10 deletions

View file

@ -5,6 +5,7 @@ from ycast import __version__
import ycast.vtuner as vtuner import ycast.vtuner as vtuner
import ycast.generic as generic import ycast.generic as generic
API_ENDPOINT = "http://all.api.radio-browser.info"
MINIMUM_COUNT_GENRE = 5 MINIMUM_COUNT_GENRE = 5
MINIMUM_COUNT_COUNTRY = 5 MINIMUM_COUNT_COUNTRY = 5
MINIMUM_COUNT_LANGUAGE = 5 MINIMUM_COUNT_LANGUAGE = 5
@ -22,12 +23,12 @@ def get_json_attr(json, attr):
class Station: class Station:
def __init__(self, station_json): def __init__(self, station_json):
self.id = generic.generate_stationid_with_prefix(get_json_attr(station_json, 'id'), ID_PREFIX) self.id = generic.generate_stationid_with_prefix(get_json_attr(station_json, 'stationuuid'), ID_PREFIX)
self.name = get_json_attr(station_json, 'name') self.name = get_json_attr(station_json, 'name')
self.url = get_json_attr(station_json, 'url') self.url = get_json_attr(station_json, 'url')
self.icon = get_json_attr(station_json, 'favicon') self.icon = get_json_attr(station_json, 'favicon')
self.tags = get_json_attr(station_json, 'tags').split(',') self.tags = get_json_attr(station_json, 'tags').split(',')
self.country = get_json_attr(station_json, 'country') self.countrycode = get_json_attr(station_json, 'countrycode')
self.language = get_json_attr(station_json, 'language') self.language = get_json_attr(station_json, 'language')
self.votes = get_json_attr(station_json, 'votes') self.votes = get_json_attr(station_json, 'votes')
self.codec = get_json_attr(station_json, 'codec') self.codec = get_json_attr(station_json, 'codec')
@ -35,7 +36,7 @@ class Station:
def to_vtuner(self): def to_vtuner(self):
return vtuner.Station(self.id, self.name, ', '.join(self.tags), self.url, self.icon, return vtuner.Station(self.id, self.name, ', '.join(self.tags), self.url, self.icon,
self.tags[0], self.country, self.codec, self.bitrate, None) self.tags[0], self.countrycode, self.codec, self.bitrate, None)
def get_playable_url(self): def get_playable_url(self):
try: try:
@ -49,7 +50,7 @@ def request(url):
logging.debug("Radiobrowser API request: %s", url) logging.debug("Radiobrowser API request: %s", url)
headers = {'content-type': 'application/json', 'User-Agent': generic.USER_AGENT + '/' + __version__} headers = {'content-type': 'application/json', 'User-Agent': generic.USER_AGENT + '/' + __version__}
try: try:
response = requests.get('http://www.radio-browser.info/webservice/json/' + url, headers=headers) response = requests.get(API_ENDPOINT + '/json/' + url, headers=headers)
except requests.exceptions.ConnectionError as err: except requests.exceptions.ConnectionError as err:
logging.error("Connection to Radiobrowser API failed (%s)", err) logging.error("Connection to Radiobrowser API failed (%s)", err)
return {} return {}
@ -71,7 +72,7 @@ def search(name, limit=DEFAULT_STATION_LIMIT):
stations = [] stations = []
stations_json = request('stations/search?order=name&reverse=false&limit=' + str(limit) + '&name=' + str(name)) stations_json = request('stations/search?order=name&reverse=false&limit=' + str(limit) + '&name=' + str(name))
for station_json in stations_json: for station_json in stations_json:
if SHOW_BROKEN_STATIONS or get_json_attr(station_json, 'lastcheckok') == '1': if SHOW_BROKEN_STATIONS or get_json_attr(station_json, 'lastcheckok') == 1:
stations.append(Station(station_json)) stations.append(Station(station_json))
return stations return stations
@ -122,7 +123,7 @@ def get_stations_by_country(country):
stations = [] stations = []
stations_json = request('stations/search?order=name&reverse=false&countryExact=true&country=' + str(country)) stations_json = request('stations/search?order=name&reverse=false&countryExact=true&country=' + str(country))
for station_json in stations_json: for station_json in stations_json:
if SHOW_BROKEN_STATIONS or get_json_attr(station_json, 'lastcheckok') == '1': if SHOW_BROKEN_STATIONS or get_json_attr(station_json, 'lastcheckok') == 1:
stations.append(Station(station_json)) stations.append(Station(station_json))
return stations return stations
@ -131,7 +132,7 @@ def get_stations_by_language(language):
stations = [] stations = []
stations_json = request('stations/search?order=name&reverse=false&languageExact=true&language=' + str(language)) stations_json = request('stations/search?order=name&reverse=false&languageExact=true&language=' + str(language))
for station_json in stations_json: for station_json in stations_json:
if SHOW_BROKEN_STATIONS or get_json_attr(station_json, 'lastcheckok') == '1': if SHOW_BROKEN_STATIONS or get_json_attr(station_json, 'lastcheckok') == 1:
stations.append(Station(station_json)) stations.append(Station(station_json))
return stations return stations
@ -140,7 +141,7 @@ def get_stations_by_genre(genre):
stations = [] stations = []
stations_json = request('stations/search?order=name&reverse=false&tagExact=true&tag=' + str(genre)) stations_json = request('stations/search?order=name&reverse=false&tagExact=true&tag=' + str(genre))
for station_json in stations_json: for station_json in stations_json:
if SHOW_BROKEN_STATIONS or get_json_attr(station_json, 'lastcheckok') == '1': if SHOW_BROKEN_STATIONS or get_json_attr(station_json, 'lastcheckok') == 1:
stations.append(Station(station_json)) stations.append(Station(station_json))
return stations return stations
@ -149,6 +150,7 @@ def get_stations_by_votes(limit=DEFAULT_STATION_LIMIT):
stations = [] stations = []
stations_json = request('stations?order=votes&reverse=true&limit=' + str(limit)) stations_json = request('stations?order=votes&reverse=true&limit=' + str(limit))
for station_json in stations_json: for station_json in stations_json:
if SHOW_BROKEN_STATIONS or get_json_attr(station_json, 'lastcheckok') == '1': print(station_json)
if SHOW_BROKEN_STATIONS or get_json_attr(station_json, 'lastcheckok') == 1:
stations.append(Station(station_json)) stations.append(Station(station_json))
return stations return stations

View file

@ -137,7 +137,7 @@ class Station:
ET.SubElement(item, 'Logo').text = self.icon ET.SubElement(item, 'Logo').text = self.icon
ET.SubElement(item, 'StationFormat').text = self.genre ET.SubElement(item, 'StationFormat').text = self.genre
ET.SubElement(item, 'StationLocation').text = self.location ET.SubElement(item, 'StationLocation').text = self.location
ET.SubElement(item, 'StationBandWidth').text = self.bitrate ET.SubElement(item, 'StationBandWidth').text = str(self.bitrate)
ET.SubElement(item, 'StationMime').text = self.mime ET.SubElement(item, 'StationMime').text = self.mime
ET.SubElement(item, 'Relia').text = '3' ET.SubElement(item, 'Relia').text = '3'
ET.SubElement(item, 'Bookmark').text = self.bookmark ET.SubElement(item, 'Bookmark').text = self.bookmark