From 9225dc34bb58ee72fc393ab3adf9ec4cfa4c5ea7 Mon Sep 17 00:00:00 2001 From: Samuel Sapalski Date: Fri, 23 Jun 2017 13:25:25 +0200 Subject: [PATCH] settings: simplify stream key handling The specification of valid stream keys in settings.json was previously defined for each network. During early development there had been differences between quality settings and the associated stream keys. These differences could be now unified and thus it isn't needed anymore to specify the mapping between quality_key and stream_key for each network separately. The implementation in settings.py has been modified with the goal to keep other parts of the code untouched. Therefore the streams definition is now given to the network class. This also leaves the possibility to react on future changes of the mapping between quality_key and stream_key in the AudioAddict API to go back to a more differentiated specification if needed without touching too much code. --- resources/data/settings.json | 34 +++++++-------------------- resources/lib/audioaddict/settings.py | 13 +++++----- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/resources/data/settings.json b/resources/data/settings.json index 670347d..40fb303 100644 --- a/resources/data/settings.json +++ b/resources/data/settings.json @@ -2,46 +2,30 @@ "stream_url": { "user_agent": "Mozilla/5.0" }, + "streams": { + "public": {"low": "android_low", "moderate": "android"}, + "premium": {"low": "premium_low", "moderate": "premium", "medium": "premium_medium", "high": "premium_high"} + }, "networks": { "di": { "domain": "di.fm", - "display_name": "DigitallyImported", - "streams": { - "public": {"low": "android_low", "moderate": "android"}, - "premium": {"low": "premium_low", "moderate": "premium", "medium": "premium_medium", "high": "premium_high"} - } + "display_name": "DigitallyImported" }, "radiotunes": { "domain": "radiotunes.com", - "display_name": "RadioTunes", - "streams": { - "public": {"low": "android_low", "moderate": "android"}, - "premium": {"low": "premium_low", "moderate": "premium", "medium": "premium_medium", "high": "premium_high"} - } + "display_name": "RadioTunes" }, "rockradio": { "domain": "rockradio.com", - "display_name": "RockRadio", - "streams": { - "public": {"low": "android_low", "moderate": "android"}, - "premium": {"low": "premium_low", "moderate": "premium", "medium": "premium_medium", "high": "premium_high"} - } + "display_name": "RockRadio" }, "jazzradio": { "domain": "jazzradio.com", - "display_name": "JazzRadio", - "streams": { - "public": {"low": "android_low", "moderate": "android"}, - "premium": {"low": "premium_low", "moderate": "premium", "medium": "premium_medium", "high": "premium_high"} - } + "display_name": "JazzRadio" }, "classicalradio": { "domain": "classicalradio.com", - "display_name": "ClassicalRadio", - "streams": { - "public": {"low": "android_low", "moderate": "android"}, - "premium": {"low": "premium_low", "moderate": "premium", "medium": "premium_medium", "high": "premium_high"} - } + "display_name": "ClassicalRadio" } }, "network_order": ["di", "radiotunes", "rockradio", "jazzradio", "classicalradio"] diff --git a/resources/lib/audioaddict/settings.py b/resources/lib/audioaddict/settings.py index ac560ef..1765870 100644 --- a/resources/lib/audioaddict/settings.py +++ b/resources/lib/audioaddict/settings.py @@ -32,7 +32,8 @@ def user_agent(self): def get_network(self, network_key): return NetworkSettings(network_key, - self._settings['networks'][network_key]) + self._settings['networks'][network_key], + self._settings['streams']) @property def networks(self): @@ -45,8 +46,10 @@ def networks(self): class NetworkSettings(object): - def __init__(self, key, network): + def __init__(self, key, network, streams): self._network = network + self._streams = streams + self.key = key @property @@ -62,11 +65,9 @@ def referer(self): return "http://www.%s/" % self.domain def get_stream_key(self, quality_key, premium=False): - streams = self._network['streams'] - if premium: - stream_key = streams['premium'][quality_key] + stream_key = self._streams['premium'][quality_key] else: - stream_key = streams['public'][quality_key] + stream_key = self._streams['public'][quality_key] return stream_key