Update despotify backend to use new models
This commit is contained in:
parent
a5aaa31eca
commit
4d19cae66b
@ -31,9 +31,9 @@ backend requires you to get an application key from Spotify before use.
|
|||||||
Installing despotify and spytify
|
Installing despotify and spytify
|
||||||
--------------------------------
|
--------------------------------
|
||||||
|
|
||||||
Check out the despotify source code revision 483 (or possibly newer)::
|
Check out the despotify source code revision 497 (or possibly newer)::
|
||||||
|
|
||||||
svn co https://despotify.svn.sourceforge.net/svnroot/despotify@483 despotify
|
svn co https://despotify.svn.sourceforge.net/svnroot/despotify@497 despotify
|
||||||
|
|
||||||
Install despotify's dependencies. At Debian/Ubuntu systems::
|
Install despotify's dependencies. At Debian/Ubuntu systems::
|
||||||
|
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import datetime as dt
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
@ -5,42 +6,76 @@ import spytify
|
|||||||
|
|
||||||
from mopidy import config
|
from mopidy import config
|
||||||
from mopidy.backends import BaseBackend
|
from mopidy.backends import BaseBackend
|
||||||
|
from mopidy.models import Artist, Album, Track, Playlist
|
||||||
|
|
||||||
logger = logging.getLogger(u'backends.despotify')
|
logger = logging.getLogger(u'backends.despotify')
|
||||||
|
|
||||||
def encode(string):
|
ENCODING = 'utf-8'
|
||||||
return string.encode('utf-8')
|
|
||||||
|
|
||||||
def decode(string):
|
def to_mopidy_id(spotify_uri):
|
||||||
return string.decode('utf-8')
|
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],
|
||||||
|
)
|
||||||
|
|
||||||
class DespotifyBackend(BaseBackend):
|
class DespotifyBackend(BaseBackend):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
logger.info(u'Connecting to Spotify')
|
logger.info(u'Connecting to Spotify')
|
||||||
self.spotify = spytify.Spytify(
|
self.spotify = spytify.Spytify(
|
||||||
config.SPOTIFY_USERNAME, config.SPOTIFY_PASSWORD)
|
config.SPOTIFY_USERNAME, config.SPOTIFY_PASSWORD)
|
||||||
logger.info(u'Preloading data')
|
self._playlists # Touch to cache
|
||||||
self._playlists
|
|
||||||
logger.debug(u'Done preloading data')
|
def cache_stored_playlists(self):
|
||||||
|
logger.info(u'Caching stored playlists')
|
||||||
|
for spotify_playlist in self.spotify.stored_playlists:
|
||||||
|
self._x_playlists.append(to_mopidy_playlist(spotify_playlist))
|
||||||
|
|
||||||
|
# State
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _playlists(self):
|
def _playlists(self):
|
||||||
if not hasattr(self, '_x_playlists') or not self._x_playlists:
|
if not hasattr(self, '_x_playlists') or not self._x_playlists:
|
||||||
logger.debug(u'Caching stored playlists')
|
|
||||||
self._x_playlists = []
|
self._x_playlists = []
|
||||||
for playlist in self.spotify.stored_playlists:
|
|
||||||
self._x_playlists.append(playlist)
|
|
||||||
return self._x_playlists
|
return self._x_playlists
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _current_playlist(self):
|
def _current_playlist(self):
|
||||||
if not hasattr(self, '_x_current_playlist'):
|
if not hasattr(self, '_x_current_playlist'):
|
||||||
self._x_current_playlist = []
|
self._x_current_playlist = Playlist()
|
||||||
return self._x_current_playlist
|
return self._x_current_playlist
|
||||||
|
|
||||||
@_current_playlist.setter
|
@_current_playlist.setter
|
||||||
def _current_playlist(self, tracks):
|
def _current_playlist(self, playlist):
|
||||||
self._x_current_playlist = tracks
|
self._x_current_playlist = playlist
|
||||||
self._x_current_playlist_version += 1
|
self._x_current_playlist_version += 1
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -52,55 +87,30 @@ class DespotifyBackend(BaseBackend):
|
|||||||
@property
|
@property
|
||||||
def _current_track(self):
|
def _current_track(self):
|
||||||
if self._current_song_pos is not None:
|
if self._current_song_pos is not None:
|
||||||
return self._current_playlist[self._current_song_pos]
|
return self._current_playlist.tracks[self._current_song_pos]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _current_song_pos(self):
|
def _current_song_pos(self):
|
||||||
if not hasattr(self, '_x_current_song_pos'):
|
if not hasattr(self, '_x_current_song_pos'):
|
||||||
self._x_current_song_pos = None
|
self._x_current_song_pos = None
|
||||||
if self._current_playlist is None or len(self._current_playlist) == 0:
|
if (self._current_playlist is None
|
||||||
|
or self._current_playlist.length == 0):
|
||||||
self._x_current_song_pos = None
|
self._x_current_song_pos = None
|
||||||
elif self._x_current_song_pos < 0:
|
elif self._x_current_song_pos < 0:
|
||||||
self._x_current_song_pos = 0
|
self._x_current_song_pos = 0
|
||||||
elif self._x_current_song_pos >= len(self._current_playlist):
|
elif self._x_current_song_pos >= self._current_playlist.length:
|
||||||
self._x_current_song_pos = len(self._current_playlist) - 1
|
self._x_current_song_pos = self._current_playlist.length - 1
|
||||||
return self._x_current_song_pos
|
return self._x_current_song_pos
|
||||||
|
|
||||||
@_current_song_pos.setter
|
@_current_song_pos.setter
|
||||||
def _current_song_pos(self, songid):
|
def _current_song_pos(self, songid):
|
||||||
self._x_current_song_pos = songid
|
self._x_current_song_pos = songid
|
||||||
|
|
||||||
def _format_playlist(self, playlist, pos_range=None):
|
|
||||||
if pos_range is None:
|
|
||||||
pos_range = range(len(playlist))
|
|
||||||
tracks = []
|
|
||||||
for track, pos in zip(playlist, pos_range):
|
|
||||||
tracks.append(self._format_track(track, pos))
|
|
||||||
return tracks
|
|
||||||
|
|
||||||
def _format_track(self, track, pos=0):
|
|
||||||
result = [
|
|
||||||
('file', track.get_uri()),
|
|
||||||
('Time', (track.length // 1000)),
|
|
||||||
('Artist', self._format_artists(track.artists)),
|
|
||||||
('Title', decode(track.title)),
|
|
||||||
('Album', decode(track.album)),
|
|
||||||
('Track', '%d/0' % track.tracknumber),
|
|
||||||
('Date', track.year),
|
|
||||||
('Pos', pos),
|
|
||||||
('Id', pos),
|
|
||||||
]
|
|
||||||
return result
|
|
||||||
|
|
||||||
def _format_artists(self, artists):
|
|
||||||
artist_names = [decode(artist.name) for artist in artists]
|
|
||||||
return u', '.join(artist_names)
|
|
||||||
|
|
||||||
# Control methods
|
# Control methods
|
||||||
|
|
||||||
def _next(self):
|
def _next(self):
|
||||||
self._current_song_pos += 1
|
self._current_song_pos += 1
|
||||||
self.spotify.play(self._current_track)
|
self.spotify.play(self.spotify.lookup(self._current_track.uri))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _pause(self):
|
def _pause(self):
|
||||||
@ -109,24 +119,24 @@ class DespotifyBackend(BaseBackend):
|
|||||||
|
|
||||||
def _play(self):
|
def _play(self):
|
||||||
if self._current_track is not None:
|
if self._current_track is not None:
|
||||||
self.spotify.play(self._current_track)
|
self.spotify.play(self.spotify.lookup(self._current_track.uri))
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _play_id(self, songid):
|
def _play_id(self, songid):
|
||||||
self._current_song_pos = songid # XXX
|
self._current_song_pos = songid # XXX
|
||||||
self.spotify.play(self._current_track)
|
self.spotify.play(self.spotify.lookup(self._current_track.uri))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _play_pos(self, songpos):
|
def _play_pos(self, songpos):
|
||||||
self._current_song_pos = songpos
|
self._current_song_pos = songpos
|
||||||
self.spotify.play(self._current_track)
|
self.spotify.play(self.spotify.lookup(self._current_track.uri))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _previous(self):
|
def _previous(self):
|
||||||
self._current_song_pos -= 1
|
self._current_song_pos -= 1
|
||||||
self.spotify.play(self._current_track)
|
self.spotify.play(self.spotify.lookup(self._current_track.uri))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _resume(self):
|
def _resume(self):
|
||||||
@ -140,41 +150,30 @@ class DespotifyBackend(BaseBackend):
|
|||||||
# Playlist methods
|
# Playlist methods
|
||||||
|
|
||||||
def playlist_load(self, name):
|
def playlist_load(self, name):
|
||||||
playlists = filter(lambda p: decode(p.name) == name, self._playlists)
|
matches = filter(lambda p: p.name == name, self._playlists)
|
||||||
if playlists:
|
if matches:
|
||||||
self._current_playlist = playlists[0].tracks
|
self._current_playlist = matches[0]
|
||||||
else:
|
else:
|
||||||
self._current_playlist = []
|
self._current_playlist = None
|
||||||
|
|
||||||
def playlists_list(self):
|
def playlists_list(self):
|
||||||
return [u'playlist: %s' % decode(p.name) for p in self._playlists]
|
return [u'playlist: %s' % p.name for p in self._playlists]
|
||||||
|
|
||||||
def playlist_changes_since(self, version='0'):
|
def playlist_changes_since(self, version='0'):
|
||||||
if int(version) < self._current_playlist_version:
|
if int(version) < self._current_playlist_version:
|
||||||
return self._format_playlist(self._current_playlist)
|
return self._current_playlist.mpd_format()
|
||||||
|
|
||||||
def playlist_info(self, songpos=None, start=None, end=None):
|
def playlist_info(self, songpos=None, start=0, end=None):
|
||||||
if songpos is not None:
|
if songpos is not None:
|
||||||
songpos = int(songpos)
|
start = int(songpos)
|
||||||
return self._format_track(self._current_playlist[songpos], songpos)
|
end = start + 1
|
||||||
elif start is not None:
|
return self._current_playlist.mpd_format(start, end)
|
||||||
start = int(start)
|
|
||||||
if end is not None:
|
|
||||||
end = int(end)
|
|
||||||
return self._format_playlist(self._current_playlist[start:end],
|
|
||||||
range(start, end))
|
|
||||||
else:
|
|
||||||
return self._format_playlist(self._current_playlist[start:],
|
|
||||||
range(start, len(self._current_playlist)))
|
|
||||||
else:
|
|
||||||
return self._format_playlist(self._current_playlist)
|
|
||||||
|
|
||||||
# Status methods
|
# Status methods
|
||||||
|
|
||||||
def current_song(self):
|
def current_song(self):
|
||||||
if self.state is not self.STOP and self._current_track is not None:
|
if self.state is not self.STOP and self._current_track is not None:
|
||||||
return self._format_track(self._current_track,
|
return self._current_track.mpd_format(self._current_song_pos)
|
||||||
self._current_song_pos)
|
|
||||||
|
|
||||||
def status_bitrate(self):
|
def status_bitrate(self):
|
||||||
return 320
|
return 320
|
||||||
@ -183,7 +182,7 @@ class DespotifyBackend(BaseBackend):
|
|||||||
return self._current_playlist_version
|
return self._current_playlist_version
|
||||||
|
|
||||||
def status_playlist_length(self):
|
def status_playlist_length(self):
|
||||||
return len(self._current_playlist)
|
return self._current_playlist.length
|
||||||
|
|
||||||
def status_song_id(self):
|
def status_song_id(self):
|
||||||
return self._current_song_pos # XXX
|
return self._current_song_pos # XXX
|
||||||
@ -200,5 +199,6 @@ class DespotifyBackend(BaseBackend):
|
|||||||
# Music database methods
|
# Music database methods
|
||||||
|
|
||||||
def search(self, type, what):
|
def search(self, type, what):
|
||||||
result = self.spotify.search(encode(u'%s:%s' % (type, what)))
|
query = u'%s:%s' % (type, what)
|
||||||
return self._format_playlist(result.playlist.tracks)
|
result = self.spotify.search(query.encode(ENCODING))
|
||||||
|
return to_mopidy_playlist(result.playlist).mpd_format()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user