# HG changeset patch # User Reed Wade # Date 1588833471 -7200 # Thu May 07 08:37:51 2020 +0200 # Node ID 8077f8abfa4182cda752493e9a3298a9ce9913fd # Parent bd1036bce2dc11f29956801e18cf1c22bb50096b Refacto build_track_model diff --git a/mopidy_multisonic/backend.py b/mopidy_multisonic/backend.py --- a/mopidy_multisonic/backend.py +++ b/mopidy_multisonic/backend.py @@ -5,10 +5,10 @@ logger = logging.getLogger(__name__) -from .library import Library -from .playback import Playback -from .httpclient import HttpClientConfig -from .uri_parser import root_uri +from . import library +from . import playback +from . import httpclient +from . import uri_parser def load_http_client_config(provider): @@ -20,7 +20,7 @@ password=match.group(4) url=match.group(5) - return HttpClientConfig( + return httpclient.HttpClientConfig( name=name, url=protocol + "://" + url, username=username, @@ -40,6 +40,6 @@ self.audio = audio self.http_client_configs = load_http_client_configs(self.config) - self.library = Library(self) - self.playback = Playback(audio, self) - self.uri_schemes = [root_uri] + self.library = library.Library(self) + self.playback = playback.Playback(audio, self) + self.uri_schemes = [uri_parser.root_uri] diff --git a/mopidy_multisonic/browser.py b/mopidy_multisonic/browser.py --- a/mopidy_multisonic/browser.py +++ b/mopidy_multisonic/browser.py @@ -3,6 +3,7 @@ from . import uri_parser from . import httpclient from . import cache +from . import lookup top_root_dir = models.Ref.directory( name="Multisonic", @@ -135,40 +136,6 @@ return albums -def build_track_model(song, http_client_config): - artists = [models.Artist( - name=song["artist"], - uri=uri_parser.build_artist( - str(song["artistId"]), - http_client_config.name - ) - )] - album = models.Album( - uri=uri_parser.build_album( - str(song["albumId"]), - http_client_config.name - ), - name=song["album"], - date=str(song["year"]), - artists=artists - ) - - track = models.Track( - uri=uri_parser.build_track( - str(song["id"]), - http_client_config.name - ), - name=song["title"], - date=str(song["year"]), - length=song["duration"]*1000, - disc_no=song["discNumber"], - track_no=song["track"], - artists=artists, - album=album, - ) - - return track - def build_track_ref(song, http_client_config): return models.Ref.track( name=song["title"], @@ -190,7 +157,7 @@ for song in data["album"]["song"]: tracks.append(build_track_ref(song, http_client_config)) - track = build_track_model(song, http_client_config) + track = lookup.build_track_model(song, http_client_config) cache.hydrate_model(track.uri, [track]) return tracks @@ -207,7 +174,7 @@ for song in data["playlist"]["entry"]: tracks.append(build_track_ref(song, http_client_config)) - track = build_track_model(song, http_client_config) + track = lookup.build_track_model(song, http_client_config) cache.hydrate_model(track.uri, [track]) return tracks diff --git a/mopidy_multisonic/lookup.py b/mopidy_multisonic/lookup.py --- a/mopidy_multisonic/lookup.py +++ b/mopidy_multisonic/lookup.py @@ -3,21 +3,12 @@ from . import cache from . import uri_parser from . import httpclient -from .browser import get_http_client_config +from . import browser def log_subsonic_error(error): logger.error("Failed to lookup: " + str(error["code"]) + " : " + error["message"]) -def lookup_track(http_client_config, uri): - id = uri_parser.get_id(uri) - - data = httpclient.get_track(http_client_config, id).json() - data = data["subsonic-response"] - if 'failed' == data["status"]: - log_subsonic_error(data["error"]) - return [] - - song = data["song"] +def build_track_model(song, http_client_config): artists = [models.Artist( name=song["artist"], uri=uri_parser.build_artist( @@ -49,6 +40,20 @@ album=album, ) + return track + +def lookup_track(http_client_config, uri): + id = uri_parser.get_id(uri) + + data = httpclient.get_track(http_client_config, id).json() + data = data["subsonic-response"] + if 'failed' == data["status"]: + log_subsonic_error(data["error"]) + return [] + + song = data["song"] + track = build_track_model(song, http_client_config) + return [track] def lookup_album(http_client_config, uri): @@ -130,7 +135,7 @@ return tracks def lookup(http_client_configs, uri): - http_client_config = get_http_client_config(http_client_configs, uri) + http_client_config = browser.get_http_client_config(http_client_configs, uri) tracks = cache.fetch_model(uri) if tracks: diff --git a/mopidy_multisonic/playback.py b/mopidy_multisonic/playback.py --- a/mopidy_multisonic/playback.py +++ b/mopidy_multisonic/playback.py @@ -1,7 +1,7 @@ from mopidy import backend from . import uri_parser -from .httpclient import get_stream -from .browser import get_http_client_config +from . import httpclient +from . import browser class Playback(backend.PlaybackProvider): def __init__(self, audio, backend): @@ -12,7 +12,7 @@ return self.backend.http_client_configs def translate_uri(self, uri): - http_client_config = get_http_client_config(self.get_http_client_configs(), uri) + http_client_config = browser.get_http_client_config(self.get_http_client_configs(), uri) id = uri_parser.get_id(uri) - return get_stream(http_client_config, str(id)).url + return httpclient.get_stream(http_client_config, str(id)).url