Improve creating classes from MPD responses
This commit is contained in:
parent
00b1efbd27
commit
1fdab35391
1 changed files with 94 additions and 68 deletions
146
mcg/client.py
146
mcg/client.py
|
|
@ -367,15 +367,13 @@ class Client(Base):
|
||||||
pos = 0
|
pos = 0
|
||||||
song = self._parse_dict(self._call("currentsong"))
|
song = self._parse_dict(self._call("currentsong"))
|
||||||
if song:
|
if song:
|
||||||
|
# Track
|
||||||
|
track = self._extract_playlist_track(song)
|
||||||
|
if track:
|
||||||
# Album
|
# Album
|
||||||
if 'album' not in song:
|
album = self._extract_album(song)
|
||||||
song['album'] = MCGAlbum.DEFAULT_ALBUM
|
|
||||||
hash = MCGAlbum.hash(song['album'])
|
|
||||||
if hash in self._albums.keys():
|
|
||||||
album = self._albums[hash]
|
|
||||||
# Position
|
# Position
|
||||||
if 'pos' in song:
|
pos = track.get_pos()
|
||||||
pos = int(song['pos'])
|
|
||||||
for palbum in self._playlist:
|
for palbum in self._playlist:
|
||||||
if palbum == album and len(palbum.get_tracks()) >= pos:
|
if palbum == album and len(palbum.get_tracks()) >= pos:
|
||||||
album = palbum
|
album = palbum
|
||||||
|
|
@ -388,30 +386,14 @@ class Client(Base):
|
||||||
"""Action: Perform the real update."""
|
"""Action: Perform the real update."""
|
||||||
self._albums = {}
|
self._albums = {}
|
||||||
# Albums
|
# Albums
|
||||||
for mpdAlbum in self._parse_list(self._call('list album'), ['album']):
|
for album in self._parse_list(self._call('list album'), ['album']):
|
||||||
albumTitle = mpdAlbum['album']
|
# Album
|
||||||
if albumTitle == "":
|
album = self._extract_album(album)
|
||||||
albumTitle = MCGAlbum.DEFAULT_ALBUM
|
|
||||||
albumHash = MCGAlbum.hash(albumTitle)
|
|
||||||
if hash in self._albums.keys():
|
|
||||||
album = self._albums[hash]
|
|
||||||
else:
|
|
||||||
album = MCGAlbum(albumTitle, self._host, self._image_dir)
|
|
||||||
self._albums[albumHash] = album
|
|
||||||
self._logger.debug("album: %r", album)
|
self._logger.debug("album: %r", album)
|
||||||
# Tracks
|
# Tracks
|
||||||
for mpdTrack in self._parse_list(self._call('find album ', mpdAlbum['album']), ['file']):
|
for song in self._parse_list(self._call('find album ', album.get_title()), ['file']):
|
||||||
if 'artist' in mpdTrack and 'title' in mpdTrack and 'file' in mpdTrack:
|
track = self._extract_track(song)
|
||||||
trackNumber = None
|
if track:
|
||||||
if 'track' in mpdTrack:
|
|
||||||
trackNumber = mpdTrack['track']
|
|
||||||
trackTime = 0
|
|
||||||
if 'time' in mpdTrack:
|
|
||||||
trackTime = mpdTrack['time']
|
|
||||||
trackDate = None
|
|
||||||
if 'date' in mpdTrack:
|
|
||||||
trackDate = mpdTrack['date']
|
|
||||||
track = MCGTrack(mpdTrack['artist'], mpdTrack['title'], trackNumber, trackTime, trackDate, mpdTrack['file'])
|
|
||||||
self._logger.debug("track: %r", track)
|
self._logger.debug("track: %r", track)
|
||||||
album.add_track(track)
|
album.add_track(track)
|
||||||
self._callback(Client.SIGNAL_LOAD_ALBUMS, self._albums)
|
self._callback(Client.SIGNAL_LOAD_ALBUMS, self._albums)
|
||||||
|
|
@ -426,22 +408,11 @@ class Client(Base):
|
||||||
for song in self._parse_list(self._call('playlistinfo'), ['file', 'playlist']):
|
for song in self._parse_list(self._call('playlistinfo'), ['file', 'playlist']):
|
||||||
self._logger.debug("song: %r", song)
|
self._logger.debug("song: %r", song)
|
||||||
# Track
|
# Track
|
||||||
track = None
|
track = self._extract_playlist_track(song)
|
||||||
if 'artist' in song and 'title' in song and 'file' in song:
|
|
||||||
if 'track' not in song:
|
|
||||||
song['track'] = None
|
|
||||||
if 'time' not in song:
|
|
||||||
song['time'] = 0
|
|
||||||
if 'date' not in song:
|
|
||||||
song['date'] = None
|
|
||||||
track = MCGPlaylistTrack(song['artist'], song['title'], song['track'], song['time'], song['date'], song['file'], song['id'], song['pos'])
|
|
||||||
self._logger.debug("track: %r", track)
|
self._logger.debug("track: %r", track)
|
||||||
# Album
|
# Album
|
||||||
if 'album' not in song:
|
album = self._extract_album(song, lookup=False)
|
||||||
song['album'] = MCGAlbum.DEFAULT_ALBUM
|
if len(self._playlist) == 0 or self._playlist[len(self._playlist)-1].get_hash() != album.get_hash():
|
||||||
hash = MCGAlbum.hash(song['album'])
|
|
||||||
if len(self._playlist) == 0 or self._playlist[len(self._playlist)-1].get_hash() != hash:
|
|
||||||
album = MCGAlbum(song['album'], self._host, self._image_dir)
|
|
||||||
self._playlist.append(album)
|
self._playlist.append(album)
|
||||||
else:
|
else:
|
||||||
album = self._playlist[len(self._playlist)-1]
|
album = self._playlist[len(self._playlist)-1]
|
||||||
|
|
@ -633,6 +604,40 @@ class Client(Base):
|
||||||
return parts[0].lower(), ':'.join(parts[1:]).lstrip()
|
return parts[0].lower(), ':'.join(parts[1:]).lstrip()
|
||||||
|
|
||||||
|
|
||||||
|
def _extract_album(self, song, lookup=True):
|
||||||
|
album = None
|
||||||
|
if 'album' not in song:
|
||||||
|
song['album'] = MCGAlbum.DEFAULT_ALBUM
|
||||||
|
hash = MCGAlbum.hash(song['album'])
|
||||||
|
if lookup and hash in self._albums.keys():
|
||||||
|
album = self._albums[hash]
|
||||||
|
else:
|
||||||
|
album = MCGAlbum(song['album'], self._host, self._image_dir)
|
||||||
|
if lookup:
|
||||||
|
self._albums[hash] = album
|
||||||
|
return album
|
||||||
|
|
||||||
|
|
||||||
|
def _extract_track(self, song):
|
||||||
|
track = None
|
||||||
|
if 'artist' in song and 'title' in song and 'file' in song:
|
||||||
|
track = MCGTrack(song['artist'], song['title'], song['file'])
|
||||||
|
if 'track' in song:
|
||||||
|
track.set_track(song['track'])
|
||||||
|
if 'time' in song:
|
||||||
|
track.set_length(song['time'])
|
||||||
|
if 'date' in song:
|
||||||
|
track.set_date(song['date'])
|
||||||
|
return track
|
||||||
|
|
||||||
|
|
||||||
|
def _extract_playlist_track(self, song):
|
||||||
|
track = self._extract_track(song)
|
||||||
|
if track and 'id' in song and 'pos' in song:
|
||||||
|
track = MCGPlaylistTrack(track, song['id'], song['pos'])
|
||||||
|
return track
|
||||||
|
|
||||||
|
|
||||||
def _set_connection_status(self, status):
|
def _set_connection_status(self, status):
|
||||||
self._callback(Client.SIGNAL_CONNECTION, status)
|
self._callback(Client.SIGNAL_CONNECTION, status)
|
||||||
|
|
||||||
|
|
@ -833,31 +838,21 @@ class MCGAlbum:
|
||||||
|
|
||||||
|
|
||||||
class MCGTrack:
|
class MCGTrack:
|
||||||
def __init__(self, artists, title, track, length, date, file):
|
def __init__(self, artists, title, file):
|
||||||
if type(artists) is not list:
|
if type(artists) is not list:
|
||||||
artists = [artists]
|
artists = [artists]
|
||||||
self._artists = artists
|
self._artists = artists
|
||||||
if type(title) is list:
|
if type(title) is list:
|
||||||
title = title[0]
|
title = title[0]
|
||||||
self._title = title
|
self._title = title
|
||||||
if type(track) is list:
|
|
||||||
track = track[0]
|
|
||||||
if track is not None and '/' in track:
|
|
||||||
track = track[0: track.index('/')]
|
|
||||||
if track is not None:
|
|
||||||
try:
|
|
||||||
track = int(track)
|
|
||||||
except ValueError:
|
|
||||||
track = 0
|
|
||||||
self._track = track
|
|
||||||
self._length = int(length)
|
|
||||||
if type(date) is list:
|
|
||||||
date = date[0]
|
|
||||||
self._date = date
|
|
||||||
if type(file) is list:
|
if type(file) is list:
|
||||||
file = file[0]
|
file = file[0]
|
||||||
self._file = file
|
self._file = file
|
||||||
|
|
||||||
|
self._track = None
|
||||||
|
self._length = 0
|
||||||
|
self._date = None
|
||||||
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return self._file == other.get_file()
|
return self._file == other.get_file()
|
||||||
|
|
@ -875,14 +870,37 @@ class MCGTrack:
|
||||||
return self._track
|
return self._track
|
||||||
|
|
||||||
|
|
||||||
|
def set_track(self, track):
|
||||||
|
if type(track) is list:
|
||||||
|
track = track[0]
|
||||||
|
if type(track) is str and '/' in track:
|
||||||
|
track = track[0: track.index('/')]
|
||||||
|
if track is not None:
|
||||||
|
try:
|
||||||
|
track = int(track)
|
||||||
|
except ValueError:
|
||||||
|
track = 0
|
||||||
|
self._track = track
|
||||||
|
|
||||||
|
|
||||||
def get_length(self):
|
def get_length(self):
|
||||||
return self._length
|
return self._length
|
||||||
|
|
||||||
|
|
||||||
|
def set_length(self, length):
|
||||||
|
self._length = int(length)
|
||||||
|
|
||||||
|
|
||||||
def get_date(self):
|
def get_date(self):
|
||||||
return self._date
|
return self._date
|
||||||
|
|
||||||
|
|
||||||
|
def set_date(self, date):
|
||||||
|
if type(date) is list:
|
||||||
|
date = date[0]
|
||||||
|
self._date = date
|
||||||
|
|
||||||
|
|
||||||
def get_file(self):
|
def get_file(self):
|
||||||
return self._file
|
return self._file
|
||||||
|
|
||||||
|
|
@ -890,8 +908,16 @@ class MCGTrack:
|
||||||
|
|
||||||
|
|
||||||
class MCGPlaylistTrack(MCGTrack):
|
class MCGPlaylistTrack(MCGTrack):
|
||||||
def __init__(self, artists, title, track, length, date, file, id, pos):
|
def __init__(self, track, id, pos):
|
||||||
MCGTrack.__init__(self, artists, title, track, length, date, file)
|
MCGTrack.__init__(
|
||||||
|
self,
|
||||||
|
track.get_artists(),
|
||||||
|
track.get_title(),
|
||||||
|
track.get_file()
|
||||||
|
)
|
||||||
|
self.set_track(track.get_track())
|
||||||
|
self.set_length(track.get_length())
|
||||||
|
self.set_date(track.get_date())
|
||||||
self._id = int(id)
|
self._id = int(id)
|
||||||
self._pos = int(pos)
|
self._pos = int(pos)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue