Create a Track proxy for spotify (Fixes #72)
This commit is contained in:
parent
21a85c0898
commit
0aeb11b22e
@ -5,21 +5,54 @@ from spotify import Link, SpotifyError
|
||||
|
||||
from mopidy.backends.base import BaseLibraryProvider
|
||||
from mopidy.backends.spotify.translator import SpotifyTranslator
|
||||
from mopidy.models import Playlist
|
||||
from mopidy.models import Track, Playlist
|
||||
|
||||
logger = logging.getLogger('mopidy.backends.spotify.library')
|
||||
|
||||
|
||||
class SpotifyTrack(Track):
|
||||
def __init__(self, uri):
|
||||
self._spotify_track = Link.from_string(uri).as_track()
|
||||
self._unloaded_track = Track(uri=uri, name=u'[loading...]')
|
||||
self._track = None
|
||||
|
||||
@property
|
||||
def _proxy(self):
|
||||
if self._track:
|
||||
return self._track
|
||||
elif self._spotify_track.is_loaded():
|
||||
self._track = SpotifyTranslator.to_mopidy_track(self._spotify_track)
|
||||
return self._track
|
||||
else:
|
||||
return self._unloaded_track
|
||||
|
||||
def __getattribute__(self, name):
|
||||
if name.startswith('_'):
|
||||
return super(SpotifyTrack, self).__getattribute__(name)
|
||||
return self._proxy.__getattribute__(name)
|
||||
|
||||
def __repr__(self):
|
||||
return self._proxy.__repr__()
|
||||
|
||||
def __hash__(self): # hash on just uri for consistency?
|
||||
return hash(self._proxy.uri)
|
||||
|
||||
def __eq__(self, other): # compare on just uri for consistency?
|
||||
if not isinstance(other, Track):
|
||||
return False
|
||||
return self._proxy.uri == other.uri
|
||||
|
||||
def copy(self, **values): # is it okay to return a plain track?
|
||||
return self._proxy.copy(**values)
|
||||
|
||||
|
||||
class SpotifyLibraryProvider(BaseLibraryProvider):
|
||||
def find_exact(self, **query):
|
||||
return self.search(**query)
|
||||
|
||||
def lookup(self, uri):
|
||||
try:
|
||||
spotify_track = Link.from_string(uri).as_track()
|
||||
# TODO Block until metadata_updated callback is called. Before that
|
||||
# the track will be unloaded, unless it's already in the stored
|
||||
# playlists.
|
||||
return SpotifyTranslator.to_mopidy_track(spotify_track)
|
||||
return SpotifyTrack(uri)
|
||||
except SpotifyError as e:
|
||||
logger.debug(u'Failed to lookup "%s": %s', uri, e)
|
||||
return None
|
||||
|
||||
Loading…
Reference in New Issue
Block a user