fixed apiUrl and added favorites

This commit is contained in:
Mario Krattenmacher
2020-12-17 15:59:58 +01:00
parent b728e9db3f
commit 558e550350
5 changed files with 45 additions and 1 deletions

View File

@@ -6,7 +6,7 @@ import os
from mopidy import config, ext from mopidy import config, ext
__version__ = '0.2.1' __version__ = '0.2.2'
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -26,6 +26,7 @@ class Extension(ext.Extension):
schema = super(Extension, self).get_config_schema() schema = super(Extension, self).get_config_schema()
schema['language'] = config.String() schema['language'] = config.String()
schema['min_bitrate'] = config.String() schema['min_bitrate'] = config.String()
schema['favorite_stations'] = config.List()
return schema return schema
def setup(self, registry): def setup(self, registry):

View File

@@ -29,6 +29,7 @@ class RadioNetBackend(pykka.ThreadingActor, backend.Backend):
self.radionet.min_bitrate = int(config['radionet']['min_bitrate']) self.radionet.min_bitrate = int(config['radionet']['min_bitrate'])
self.radionet.set_lang(str(config['radionet']['language'])) self.radionet.set_lang(str(config['radionet']['language']))
self.radionet.set_favorites(tuple(file_ext.lower() for file_ext in config["radionet"]["favorite_stations"]))
def set_update_timeout(self, minutes=2): def set_update_timeout(self, minutes=2):
self.update_timeout = time.time() + 60 * minutes self.update_timeout = time.time() + 60 * minutes
@@ -43,4 +44,5 @@ class RadioNetBackend(pykka.ThreadingActor, backend.Backend):
if force or time.time() > self.update_timeout: if force or time.time() > self.update_timeout:
self.radionet.get_top_stations() self.radionet.get_top_stations()
self.radionet.get_local_stations() self.radionet.get_local_stations()
self.radionet.get_favorites()
self.set_update_timeout() self.set_update_timeout()

View File

@@ -2,3 +2,4 @@
enabled = true enabled = true
language = pl language = pl
min_bitrate = 96 min_bitrate = 96
favorite_stations =

View File

@@ -62,6 +62,11 @@ class RadioNetLibraryProvider(backend.LibraryProvider):
self.ref_directory( self.ref_directory(
"radionet:category:top100", "Top 100") "radionet:category:top100", "Top 100")
) )
if self.backend.radionet.favorite_stations:
directories.append(
self.ref_directory(
"radionet:category:favorites", "Favorites")
)
return directories return directories
elif variant == 'category' and identifier: elif variant == 'category' and identifier:
if identifier == "localstations": if identifier == "localstations":
@@ -70,6 +75,9 @@ class RadioNetLibraryProvider(backend.LibraryProvider):
if identifier == "top100": if identifier == "top100":
for station in self.backend.radionet.top_stations: for station in self.backend.radionet.top_stations:
tracks.append(self.station_to_ref(station)) tracks.append(self.station_to_ref(station))
if identifier == "favorites":
for station in self.backend.radionet.favorite_stations:
tracks.append(self.station_to_ref(station))
tracks.sort(key=lambda ref: ref.name) tracks.sort(key=lambda ref: ref.name)
return tracks return tracks
else: else:

View File

@@ -76,6 +76,7 @@ class RadioNetClient(object):
apiprefix_search = re.search('apiPrefix ?: ?\'(.*)\',?', tmp_str.content.decode()) apiprefix_search = re.search('apiPrefix ?: ?\'(.*)\',?', tmp_str.content.decode())
self.api_prefix = apiprefix_search.group(1) self.api_prefix = apiprefix_search.group(1)
self.api_prefix = "https://api.radio." + 'de' + "/info/v2"
apikey_search = re.search('apiKey ?: ?[\'|"](.*)[\'|"],?', tmp_str.content.decode()) apikey_search = re.search('apiKey ?: ?[\'|"](.*)[\'|"],?', tmp_str.content.decode())
self.api_key = apikey_search.group(1) self.api_key = apikey_search.group(1)
@@ -170,6 +171,37 @@ class RadioNetClient(object):
logger.info('Radio.net: Loaded ' + str(len(self.local_stations)) + logger.info('Radio.net: Loaded ' + str(len(self.local_stations)) +
' local stations.') ' local stations.')
def set_favorites(self, favorites):
self.favortes = favorites
def get_favorites(self):
self.favorite_stations = []
for station in self.favortes:
api_suffix = '/search/stationsonly'
url_params = {
'apikey': self.api_key,
'_': self.current_milli_time(),
'query': station,
'pageindex': 1,
}
response = self.do_post(api_suffix, url_params)
if response.status_code is not 200:
logger.error('Radio.net: Search error ' + response.text)
else:
logger.debug('Radio.net: Done search')
json = response.json()
# take only the first match!
station = self.get_station_by_id(json['categories'][0]['matches'][0]['id'])
if station and station.playable:
self.favorite_stations.append(station)
logger.info('Radio.net: Loaded ' + str(len(self.favorite_stations)) +
' favorite stations.')
def get_top_stations(self): def get_top_stations(self):
self.top_stations = [] self.top_stations = []
api_suffix = '/search/topstations' api_suffix = '/search/topstations'