Move model translation functions into backend class
This commit is contained in:
parent
20e360131f
commit
703b6c53d6
@ -24,10 +24,47 @@ class DespotifyBackend(BaseBackend):
|
|||||||
logger.info(u'Caching stored playlists')
|
logger.info(u'Caching stored playlists')
|
||||||
playlists = []
|
playlists = []
|
||||||
for spotify_playlist in self.spotify.stored_playlists:
|
for spotify_playlist in self.spotify.stored_playlists:
|
||||||
playlists.append(to_mopidy_playlist(spotify_playlist))
|
playlists.append(self._to_mopidy_playlist(spotify_playlist))
|
||||||
self._playlists = playlists
|
self._playlists = playlists
|
||||||
|
|
||||||
# Control methods
|
# Model translation
|
||||||
|
|
||||||
|
def _to_mopidy_id(self, spotify_uri):
|
||||||
|
return 0 # TODO
|
||||||
|
|
||||||
|
def _to_mopidy_artist(self, spotify_artist):
|
||||||
|
return Artist(
|
||||||
|
uri=spotify_artist.get_uri(),
|
||||||
|
name=spotify_artist.name.decode(ENCODING)
|
||||||
|
)
|
||||||
|
|
||||||
|
def _to_mopidy_album(self, spotify_album_name):
|
||||||
|
return Album(name=spotify_album_name.decode(ENCODING))
|
||||||
|
|
||||||
|
def _to_mopidy_track(self, spotify_track):
|
||||||
|
if dt.MINYEAR <= int(spotify_track.year) <= dt.MAXYEAR:
|
||||||
|
date = dt.date(spotify_track.year, 1, 1)
|
||||||
|
else:
|
||||||
|
date = None
|
||||||
|
return Track(
|
||||||
|
uri=spotify_track.get_uri(),
|
||||||
|
title=spotify_track.title.decode(ENCODING),
|
||||||
|
artists=[self._to_mopidy_artist(a) for a in spotify_track.artists],
|
||||||
|
album=self._to_mopidy_album(spotify_track.album),
|
||||||
|
track_no=spotify_track.tracknumber,
|
||||||
|
date=date,
|
||||||
|
length=spotify_track.length,
|
||||||
|
id=self._to_mopidy_id(spotify_track.get_uri()),
|
||||||
|
)
|
||||||
|
|
||||||
|
def _to_mopidy_playlist(self, spotify_playlist):
|
||||||
|
return Playlist(
|
||||||
|
uri=spotify_playlist.get_uri(),
|
||||||
|
name=spotify_playlist.name.decode(ENCODING),
|
||||||
|
tracks=[self._to_mopidy_track(t) for t in spotify_playlist.tracks],
|
||||||
|
)
|
||||||
|
|
||||||
|
# Play control
|
||||||
|
|
||||||
def _next(self):
|
def _next(self):
|
||||||
self._current_song_pos += 1
|
self._current_song_pos += 1
|
||||||
@ -68,7 +105,7 @@ class DespotifyBackend(BaseBackend):
|
|||||||
self.spotify.stop()
|
self.spotify.stop()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# Status methods
|
# Status querying
|
||||||
|
|
||||||
def status_bitrate(self):
|
def status_bitrate(self):
|
||||||
return 320
|
return 320
|
||||||
@ -76,45 +113,9 @@ class DespotifyBackend(BaseBackend):
|
|||||||
def url_handlers(self):
|
def url_handlers(self):
|
||||||
return [u'spotify:', u'http://open.spotify.com/']
|
return [u'spotify:', u'http://open.spotify.com/']
|
||||||
|
|
||||||
# Music database methods
|
# Music database
|
||||||
|
|
||||||
def search(self, type, what):
|
def search(self, type, what):
|
||||||
query = u'%s:%s' % (type, what)
|
query = u'%s:%s' % (type, what)
|
||||||
result = self.spotify.search(query.encode(ENCODING))
|
result = self.spotify.search(query.encode(ENCODING))
|
||||||
return to_mopidy_playlist(result.playlist).mpd_format()
|
return self._to_mopidy_playlist(result.playlist).mpd_format()
|
||||||
|
|
||||||
|
|
||||||
def to_mopidy_id(spotify_uri):
|
|
||||||
return 0 # TODO
|
|
||||||
|
|
||||||
def to_mopidy_artist(spotify_artist):
|
|
||||||
return Artist(
|
|
||||||
uri=spotify_artist.get_uri(),
|
|
||||||
name=spotify_artist.name.decode(ENCODING)
|
|
||||||
)
|
|
||||||
|
|
||||||
def to_mopidy_album(spotify_album_name):
|
|
||||||
return Album(name=spotify_album_name.decode(ENCODING))
|
|
||||||
|
|
||||||
def to_mopidy_track(spotify_track):
|
|
||||||
if dt.MINYEAR <= int(spotify_track.year) <= dt.MAXYEAR:
|
|
||||||
date = dt.date(spotify_track.year, 1, 1)
|
|
||||||
else:
|
|
||||||
date = None
|
|
||||||
return Track(
|
|
||||||
uri=spotify_track.get_uri(),
|
|
||||||
title=spotify_track.title.decode(ENCODING),
|
|
||||||
artists=[to_mopidy_artist(a) for a in spotify_track.artists],
|
|
||||||
album=to_mopidy_album(spotify_track.album),
|
|
||||||
track_no=spotify_track.tracknumber,
|
|
||||||
date=date,
|
|
||||||
length=spotify_track.length,
|
|
||||||
id=to_mopidy_id(spotify_track.get_uri()),
|
|
||||||
)
|
|
||||||
|
|
||||||
def to_mopidy_playlist(spotify_playlist):
|
|
||||||
return Playlist(
|
|
||||||
uri=spotify_playlist.get_uri(),
|
|
||||||
name=spotify_playlist.name.decode(ENCODING),
|
|
||||||
tracks=[to_mopidy_track(t) for t in spotify_playlist.tracks],
|
|
||||||
)
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user