mpd key-corrections

This commit is contained in:
coderkun 2013-05-30 01:36:09 +02:00
commit 78f0cdd953
2 changed files with 152 additions and 69 deletions

View file

@ -797,7 +797,7 @@ class CoverPanel(mcg.MCGBase, Gtk.VBox):
def set_album(self, album): def set_album(self, album):
self._album_title_label.set_markup("<b><big>{}</big></b>".format(album.get_title())) self._album_title_label.set_markup("<b><big>{}</big></b>".format(album.get_title()))
self._album_date_label.set_markup("<big>{}</big>".format(album.get_date())) self._album_date_label.set_markup("<big>{}</big>".format(', '.join(album.get_dates())))
self._album_artist_label.set_markup("<big>{}</big>".format(', '.join(album.get_artists()))) self._album_artist_label.set_markup("<big>{}</big>".format(', '.join(album.get_artists())))
self._set_cover(album) self._set_cover(album)
self._set_tracks(album) self._set_tracks(album)
@ -807,7 +807,7 @@ class CoverPanel(mcg.MCGBase, Gtk.VBox):
if self._timer is not None: if self._timer is not None:
GObject.source_remove(self._timer) GObject.source_remove(self._timer)
for index in range(0, pos): for index in range(0, pos):
time = time + self._current_album.get_tracks()[index].get_time() time = time + self._current_album.get_tracks()[index].get_length()
self._songs_scale.set_value(time+1) self._songs_scale.set_value(time+1)
self._timer = GObject.timeout_add(1000, self._playing) self._timer = GObject.timeout_add(1000, self._playing)
@ -854,7 +854,7 @@ class CoverPanel(mcg.MCGBase, Gtk.VBox):
length = 0 length = 0
for track in album.get_tracks(): for track in album.get_tracks():
self._songs_scale.add_mark(length, Gtk.PositionType.RIGHT, track.get_title()) self._songs_scale.add_mark(length, Gtk.PositionType.RIGHT, track.get_title())
length = length + track.get_time() length = length + track.get_length()
self._songs_scale.add_mark(length, Gtk.PositionType.RIGHT, "{0[0]:02d}:{0[1]:02d} minutes".format(divmod(length, 60))) self._songs_scale.add_mark(length, Gtk.PositionType.RIGHT, "{0[0]:02d}:{0[1]:02d} minutes".format(divmod(length, 60)))
@ -958,7 +958,6 @@ class PlaylistPanel(mcg.MCGBase, Gtk.VBox):
def set_playlist(self, host, playlist): def set_playlist(self, host, playlist):
self._host = host self._host = host
self._playlist = playlist
self._playlist_stop.set() self._playlist_stop.set()
threading.Thread(target=self._set_playlist, args=(host, playlist, self._config.item_size,)).start() threading.Thread(target=self._set_playlist, args=(host, playlist, self._config.item_size,)).start()
@ -966,6 +965,7 @@ class PlaylistPanel(mcg.MCGBase, Gtk.VBox):
def _set_playlist(self, host, playlist, size): def _set_playlist(self, host, playlist, size):
self._playlist_lock.acquire() self._playlist_lock.acquire()
self._playlist_stop.clear() self._playlist_stop.clear()
self._playlist = playlist
Gdk.threads_enter() Gdk.threads_enter()
self._playlist_grid.set_model(None) self._playlist_grid.set_model(None)
self._playlist_grid.freeze_child_notify() self._playlist_grid.freeze_child_notify()
@ -987,7 +987,7 @@ class PlaylistPanel(mcg.MCGBase, Gtk.VBox):
pixbuf, pixbuf,
GObject.markup_escape_text("\n".join([ GObject.markup_escape_text("\n".join([
album.get_title(), album.get_title(),
album.get_date(), ', '.join(album.get_dates()),
', '.join(album.get_artists()) ', '.join(album.get_artists())
])), ])),
album.get_hash() album.get_hash()
@ -1021,7 +1021,7 @@ class LibraryPanel(mcg.MCGBase, Gtk.VBox):
Gtk.VBox.__init__(self) Gtk.VBox.__init__(self)
self._config = config self._config = config
self._host = None self._host = None
self._albums = [] self._albums = {}
self._filter_string = "" self._filter_string = ""
self._grid_pixbufs = {} self._grid_pixbufs = {}
self._old_ranges = {} self._old_ranges = {}
@ -1186,7 +1186,6 @@ class LibraryPanel(mcg.MCGBase, Gtk.VBox):
def set_albums(self, host, albums): def set_albums(self, host, albums):
self._host = host self._host = host
self._albums = albums
self._library_stop.set() self._library_stop.set()
threading.Thread(target=self._set_albums, args=(host, albums, self._config.item_size,)).start() threading.Thread(target=self._set_albums, args=(host, albums, self._config.item_size,)).start()
@ -1203,6 +1202,7 @@ class LibraryPanel(mcg.MCGBase, Gtk.VBox):
def _set_albums(self, host, albums, size): def _set_albums(self, host, albums, size):
self._library_lock.acquire() self._library_lock.acquire()
self._library_stop.clear() self._library_stop.clear()
self._albums = albums
self.remove(self._library_toolbar) self.remove(self._library_toolbar)
self._progress_bar.set_fraction(0.0) self._progress_bar.set_fraction(0.0)
self.pack_start(self._progress_bar, False, True, 5) self.pack_start(self._progress_bar, False, True, 5)
@ -1232,7 +1232,7 @@ class LibraryPanel(mcg.MCGBase, Gtk.VBox):
pixbuf, pixbuf,
GObject.markup_escape_text("\n".join([ GObject.markup_escape_text("\n".join([
album.get_title(), album.get_title(),
album.get_date(), ', '.join(album.get_dates()),
', '.join(album.get_artists()) ', '.join(album.get_artists())
])), ])),
hash hash

205
mcg.py
View file

@ -86,6 +86,7 @@ class MCGClient(MCGBase, mpd.MPDClient):
self._actions = queue.Queue() self._actions = queue.Queue()
self._worker = None self._worker = None
self._albums = {} self._albums = {}
self._playlist = []
self._host = None self._host = None
self._image_dir = "" self._image_dir = ""
@ -254,35 +255,62 @@ class MCGClient(MCGBase, mpd.MPDClient):
def _get_status(self): def _get_status(self):
"""Action: Perform the real status determination.""" """Action: Perform the real status determination."""
# current status # current status
#self._call('noidle')
status = self._call('status') status = self._call('status')
if 'state' not in status:
return
state = status['state'] state = status['state']
time = 0 time = 0
if 'time' in status: if 'time' in status:
time = int(status['time'].split(':')[0]) time = int(status['time'].split(':')[0])
volume = int(status['volume']) volume = 0
if 'volume' in status:
volume = int(status['volume'])
error = None error = None
if 'error' in status: if 'error' in status:
error = status['error'] error = status['error']
# current song # current song
#self._call('noidle')
song = self._call('currentsong') song = self._call('currentsong')
album = None album = None
pos = None pos = None
if song: if song:
try: # Track
hash = MCGAlbum.hash(song['album'], song['date']) if 'artist' not in song:
if hash in self._albums: return
album = self._albums[hash] if 'title' not in song:
pos = song['track'] return
if type(pos) is list: if 'track' not in song:
pos = pos[0] song['track'] = None
if '/' in pos: if 'time' not in song:
pos = pos[0: pos.index('/')] song['time'] = 0
pos = int(pos) - 1 if 'date' not in song:
except KeyError: song['date'] = None
pass if 'file' not in song:
return
track = MCGTrack(song['artist'], song['title'], song['track'], song['time'], song['date'], song['file'])
# Album
if 'album' not in song:
song['album'] = 'Various'
hash = MCGAlbum.hash(song['album'])
if hash not in self._albums:
return
album = self._albums[hash]
# Position
if track.get_track() is not None:
pos = track.get_track() -1
else:
for album in self._playlist:
pos = -1
for ptrack in album.get_tracks():
pos = pos + 1
if ptrack == track:
break
if pos < len(album.get_tracks())-1:
break
if pos is None:
pos = 0
self._state = state self._state = state
self._callback(MCGClient.SIGNAL_STATUS, state, album, pos, time, volume, error) self._callback(MCGClient.SIGNAL_STATUS, state, album, pos, time, volume, error)
@ -324,23 +352,37 @@ class MCGClient(MCGBase, mpd.MPDClient):
# Playlist commands # Playlist commands
def _load_playlist(self): def _load_playlist(self):
playlist = [] self._playlist = []
for song in self._call('playlistinfo'): for song in self._call('playlistinfo'):
try: try:
hash = MCGAlbum.hash(song['album'], song['date']) # Track
if len(playlist) == 0 or playlist[len(playlist)-1].get_hash() != hash: if 'artist' not in song:
date = "" continue
if 'date' in song: if 'title' not in song:
date = song['date'] continue
album = MCGAlbum(song['album'], date, self._host, self._image_dir) if 'track' not in song:
playlist.append(album) song['track'] = None
if 'time' not in song:
song['time'] = 0
if 'date' not in song:
song['date'] = None
if 'file' not in song:
continue
track = MCGTrack(song['artist'], song['title'], song['track'], song['time'], song['date'], song['file'])
# Album
if 'album' not in song:
song['album'] = 'Various'
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)
else: else:
album = playlist[len(playlist)-1] album = self._playlist[len(self._playlist)-1]
track = MCGTrack(song['artist'], song['title'], song['track'], song['time'], song['file'])
album.add_track(track) album.add_track(track)
except KeyError: except KeyError:
pass pass
self._callback(MCGClient.SIGNAL_LOAD_PLAYLIST, playlist, None) self._callback(MCGClient.SIGNAL_LOAD_PLAYLIST, self._playlist, None)
def _clear_playlist(self): def _clear_playlist(self):
@ -354,23 +396,34 @@ class MCGClient(MCGBase, mpd.MPDClient):
"""Action: Perform the real update.""" """Action: Perform the real update."""
self._albums = {} self._albums = {}
for song in self._call('listallinfo'): for song in self._call('listallinfo'):
try: if 'directory' in song:
if 'album' not in song: continue
song['album'] = 'Various'
song['date'] = 'none' # Track
hash = MCGAlbum.hash(song['album'], song['date']) if 'artist' not in song:
if hash in self._albums.keys(): continue
album = self._albums[hash] if 'title' not in song:
else: continue
date = "" if 'track' not in song:
if 'date' in song: song['track'] = None
date = song['date'] if 'time' not in song:
album = MCGAlbum(song['album'], date, self._host, self._image_dir) song['time'] = 0
self._albums[album.get_hash()] = album if 'date' not in song:
track = MCGTrack(song['artist'], song['title'], song['track'], song['time'], song['file']) song['date'] = None
album.add_track(track) if 'file' not in song:
except KeyError: continue
pass track = MCGTrack(song['artist'], song['title'], song['track'], song['time'], song['date'], song['file'])
# Album
if 'album' not in song:
song['album'] = 'Various'
hash = MCGAlbum.hash(song['album'])
if hash in self._albums.keys():
album = self._albums[hash]
else:
album = MCGAlbum(song['album'], self._host, self._image_dir)
self._albums[album.get_hash()] = album
album.add_track(track)
self._callback(MCGClient.SIGNAL_LOAD_ALBUMS, self._albums, None) self._callback(MCGClient.SIGNAL_LOAD_ALBUMS, self._albums, None)
@ -415,15 +468,13 @@ class MCGAlbum:
_FILE_EXTS = ['jpg', 'png', 'jpeg'] _FILE_EXTS = ['jpg', 'png', 'jpeg']
def __init__(self, title, date, host, image_dir): def __init__(self, title, host, image_dir):
self._artists = [] self._artists = []
self._pathes = [] self._pathes = []
if type(title) is list: if type(title) is list:
title = title[0] title = title[0]
self._title = title self._title = title
if type(date) is list: self._dates = []
date = date[0]
self._date = date
self._host = host self._host = host
self._image_dir = image_dir self._image_dir = image_dir
self._tracks = [] self._tracks = []
@ -441,8 +492,14 @@ class MCGAlbum:
return self._title return self._title
def get_dates(self):
return self._dates
def get_date(self): def get_date(self):
return self._date if len(self._dates) == 0:
return None
return self._dates[0]
def get_path(self): def get_path(self):
@ -452,10 +509,12 @@ class MCGAlbum:
def add_track(self, track): def add_track(self, track):
if track not in self._tracks: if track not in self._tracks:
self._tracks.append(track) self._tracks.append(track)
self._length = self._length + track.get_time() self._length = self._length + track.get_length()
for artist in track.get_artists(): for artist in track.get_artists():
if artist not in self._artists: if artist not in self._artists:
self._artists.append(artist) self._artists.append(artist)
if track.get_date() is not None and track.get_date() not in self._dates:
self._dates.append(track.get_date())
path = os.path.dirname(track.get_file()) path = os.path.dirname(track.get_file())
if path not in self._pathes: if path not in self._pathes:
self._pathes.append(path) self._pathes.append(path)
@ -475,12 +534,10 @@ class MCGAlbum:
return self._cover return self._cover
def hash(title, date): def hash(title):
if type(title) is list: if type(title) is list:
title = title[0] title = title[0]
if type(date) is list: return md5(title.encode('utf-8')).hexdigest()
date = date[0]
return md5(title.encode('utf-8')+date.encode('utf-8')).hexdigest()
def get_hash(self): def get_hash(self):
@ -488,7 +545,7 @@ class MCGAlbum:
def filter(self, filter_string): def filter(self, filter_string):
values = self._artists + [self._title, self._date] values = self._artists + [self._title]
values.extend(map(lambda track: track.get_title(), self._tracks)) values.extend(map(lambda track: track.get_title(), self._tracks))
for value in values: for value in values:
if filter_string.lower() in value.lower(): if filter_string.lower() in value.lower():
@ -506,16 +563,24 @@ class MCGAlbum:
elif criterion == MCGAlbum.SORT_BY_YEAR: elif criterion == MCGAlbum.SORT_BY_YEAR:
value_function = "get_date" value_function = "get_date"
if getattr(album1, value_function)() < getattr(album2, value_function)(): value1 = getattr(album1, value_function)()
value2 = getattr(album2, value_function)()
if value1 is None and value2 is None:
return 0
elif value1 is None:
return -1 return -1
elif getattr(album1, value_function)() == getattr(album2, value_function)(): elif value2 is None:
return 1
if value1 < value2:
return -1
elif value1 == value2:
return 0 return 0
else: else:
return 1 return 1
def _set_hash(self): def _set_hash(self):
self._hash = MCGAlbum.hash(self._title, self._date) self._hash = MCGAlbum.hash(self._title)
def _find_cover(self): def _find_cover(self):
@ -570,7 +635,7 @@ class MCGAlbum:
class MCGTrack: class MCGTrack:
def __init__(self, artists, title, track, time, file): def __init__(self, artists, title, track, length, date, file):
if type(artists) is not list: if type(artists) is not list:
artists = [artists] artists = [artists]
self._artists = artists self._artists = artists
@ -579,11 +644,24 @@ class MCGTrack:
self._title = title self._title = title
if type(track) is list: if type(track) is list:
track = track[0] track = track[0]
if track is not None and '/' in track:
track = track[0: track.index('/')]
if track is not None:
track = int(track)
self._track = track self._track = track
self._time = int(time) self._length = int(length)
if type(date) is list:
date = date[0]
self._date = date
if type(file) is list:
file = file[0]
self._file = file self._file = file
def __eq__(self, other):
return self._file == other.get_file()
def get_artists(self): def get_artists(self):
return self._artists return self._artists
@ -596,8 +674,12 @@ class MCGTrack:
return self._track return self._track
def get_time(self): def get_length(self):
return self._time return self._length
def get_date(self):
return self._date
def get_file(self): def get_file(self):
@ -780,3 +862,4 @@ class MCGCache():
except Exception as e: except Exception as e:
print("clear:", e) print("clear:", e)