improved tag handling: support for multiple tags of same type

This commit is contained in:
coderkun 2013-02-27 19:19:27 +01:00
commit c45c10a49d
2 changed files with 110 additions and 59 deletions

97
mcg.py
View file

@ -236,7 +236,7 @@ class MCGClient(MCGBase, mpd.MPDClient):
album = None album = None
pos = None pos = None
if song: if song:
hash = MCGAlbum.hash(song['artist'], song['album']) hash = MCGAlbum.hash(song['album'], song['date'])
if hash in self._albums: if hash in self._albums:
album = self._albums[hash] album = self._albums[hash]
pos = int(song['pos']) pos = int(song['pos'])
@ -279,6 +279,9 @@ class MCGClient(MCGBase, mpd.MPDClient):
track_ids.append(track_id) track_ids.append(track_id)
self._call('moveid', track_id, len(track_ids)-1) self._call('moveid', track_id, len(track_ids)-1)
self._call('playid', track_ids[0]) self._call('playid', track_ids[0])
# TODO CommandError
# except mpd.CommandError as e:
# _callback(SIGNAL_ERROR)
except mpd.ConnectionError as e: except mpd.ConnectionError as e:
self._set_connction_status(False, e) self._set_connction_status(False, e)
@ -297,19 +300,16 @@ class MCGClient(MCGBase, mpd.MPDClient):
playlist = [] playlist = []
for song in self._call('playlistinfo'): for song in self._call('playlistinfo'):
try: try:
hash = MCGAlbum.hash(song['artist'], song['album']) hash = MCGAlbum.hash(song['album'], song['date'])
if len(playlist) == 0 or playlist[len(playlist)-1].get_hash() != hash: if len(playlist) == 0 or playlist[len(playlist)-1].get_hash() != hash:
date = "" date = ""
if 'date' in song: if 'date' in song:
date = song['date'] date = song['date']
path = "" album = MCGAlbum(song['album'], date, self._host, self._image_dir)
if 'file' in song:
path = os.path.dirname(song['file'])
album = MCGAlbum(song['artist'], song['album'], date, path, self._host, self._image_dir)
playlist.append(album) playlist.append(album)
else: else:
album = playlist[len(playlist)-1] album = playlist[len(playlist)-1]
track = MCGTrack(song['title'], song['track'], song['time'], song['file']) 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
@ -326,19 +326,16 @@ class MCGClient(MCGBase, mpd.MPDClient):
try: try:
for song in self._call('listallinfo'): for song in self._call('listallinfo'):
try: try:
hash = MCGAlbum.hash(song['artist'], song['album']) hash = MCGAlbum.hash(song['album'], song['date'])
if hash in self._albums.keys(): if hash in self._albums.keys():
album = self._albums[hash] album = self._albums[hash]
else: else:
date = "" date = ""
if 'date' in song: if 'date' in song:
date = song['date'] date = song['date']
path = "" album = MCGAlbum(song['album'], date, self._host, self._image_dir)
if 'file' in song:
path = os.path.dirname(song['file'])
album = MCGAlbum(song['artist'], song['album'], date, path, self._host, self._image_dir)
self._albums[album.get_hash()] = album self._albums[album.get_hash()] = album
track = MCGTrack(song['title'], song['track'], song['time'], song['file']) 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
@ -395,17 +392,19 @@ class MCGAlbum:
SORT_BY_ARTIST = 'artist' SORT_BY_ARTIST = 'artist'
SORT_BY_TITLE = 'title' SORT_BY_TITLE = 'title'
SORT_BY_YEAR = 'year' SORT_BY_YEAR = 'year'
_file_names = ['folder', 'cover'] _FILE_NAMES = ['folder', 'cover']
_file_exts = ['jpg', 'jpeg', 'png'] _FILE_EXTS = ['jpg', 'png', 'jpeg']
def __init__(self, artist, title, date, path, host, image_dir): def __init__(self, title, date, host, image_dir):
self._artist = artist self._artists = []
if type(self._artist) is list: self._pathes = []
self._artist = self._artist[0] if type(title) is list:
title = title[0]
self._title = title self._title = title
if type(date) is list:
date = date[0]
self._date = date self._date = date
self._path = path
self._host = host self._host = host
self._image_dir = image_dir self._image_dir = image_dir
self._tracks = [] self._tracks = []
@ -414,8 +413,8 @@ class MCGAlbum:
self._set_hash() self._set_hash()
def get_artist(self): def get_artists(self):
return self._artist return self._artists
def get_title(self): def get_title(self):
@ -433,6 +432,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)
for artist in track.get_artists():
if artist not in self._artists:
self._artists.append(artist)
path = os.path.dirname(track.get_file())
if path not in self._pathes:
self._pathes.append(path)
def get_tracks(self): def get_tracks(self):
@ -445,10 +450,12 @@ class MCGAlbum:
return self._cover return self._cover
def hash(artist, title): def hash(title, date):
if type(artist) is list: if type(title) is list:
artist = artist[0] title = title[0]
return md5(artist.encode('utf-8')+title.encode('utf-8')).hexdigest() if type(date) is list:
date = date[0]
return md5(title.encode('utf-8')+date.encode('utf-8')).hexdigest()
def get_hash(self): def get_hash(self):
@ -456,7 +463,7 @@ class MCGAlbum:
def filter(self, filter_string): def filter(self, filter_string):
values = [self._artist, self._title, self._date] values = self._artists + [self._title, self._date]
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():
@ -467,12 +474,11 @@ class MCGAlbum:
def compare(album1, album2, criterion=None): def compare(album1, album2, criterion=None):
if criterion == None: if criterion == None:
criterion = MCGAlbum.SORT_BY_TITLE criterion = MCGAlbum.SORT_BY_TITLE
if criterion == MCGAlbum.SORT_BY_ARTIST: if criterion == MCGAlbum.SORT_BY_ARTIST:
value_function = "get_artist" value_function = "get_artists"
if criterion == MCGAlbum.SORT_BY_TITLE: elif criterion == MCGAlbum.SORT_BY_TITLE:
value_function = "get_title" value_function = "get_title"
if 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)(): if getattr(album1, value_function)() < getattr(album2, value_function)():
@ -484,13 +490,13 @@ class MCGAlbum:
def _set_hash(self): def _set_hash(self):
self._hash = MCGAlbum.hash(self._artist, self._title) self._hash = MCGAlbum.hash(self._title, self._date)
def _find_cover(self): def _find_cover(self):
names = list(self._file_names) names = list(MCGAlbum._FILE_NAMES)
names.append(self._title) names.append(self._title)
names.append(' - '.join([self._artist, self._title])) names.append(' - '.join([self._artists[0], self._title]))
if self._host == "localhost" or self._host == "127.0.0.1": if self._host == "localhost" or self._host == "127.0.0.1":
self._cover = self._find_cover_local(names) self._cover = self._find_cover_local(names)
@ -500,12 +506,13 @@ class MCGAlbum:
def _find_cover_web(self, names): def _find_cover_web(self, names):
for path in self._pathes:
for name in names: for name in names:
for ext in self._file_exts: for ext in self._FILE_EXTS:
url = '/'.join([ url = '/'.join([
'http:/', 'http:/',
self._host, self._host,
urllib.request.quote(self._path), urllib.request.quote(path),
urllib.request.quote('.'.join([name, ext])) urllib.request.quote('.'.join([name, ext]))
]) ])
request = urllib.request.Request(url) request = urllib.request.Request(url)
@ -517,9 +524,10 @@ class MCGAlbum:
def _find_cover_local(self, names): def _find_cover_local(self, names):
for path in self._pathes:
for name in names: for name in names:
for ext in self._file_exts: for ext in self._FILE_EXTS:
filename = os.path.join(self._image_dir, self._path, '.'.join([name, ext])) filename = os.path.join(self._image_dir, path, '.'.join([name, ext]))
if os.path.isfile(filename): if os.path.isfile(filename):
return filename return filename
@ -527,13 +535,24 @@ class MCGAlbum:
class MCGTrack: class MCGTrack:
def __init__(self, title, track, time, file): def __init__(self, artists, title, track, time, file):
if type(artists) is not list:
artists = [artists]
self._artists = artists
if type(title) is list:
title = title[0]
self._title = title self._title = title
if type(track) is list:
track = track[0]
self._track = track self._track = track
self._time = time self._time = time
self._file = file self._file = file
def get_artists(self):
return self._artists
def get_title(self): def get_title(self):
return self._title return self._title

View file

@ -960,7 +960,14 @@ class CoverPanel(mcg.MCGBase, Gtk.HPaned):
cache = mcg.MCGCache(host, size) cache = mcg.MCGCache(host, size)
for album in playlist: for album in playlist:
for track in album.get_tracks(): for track in album.get_tracks():
self._playlist_list_model.append([album.get_artist(), album.get_title(), track.get_track(), track.get_title(), album.get_date(), album.get_hash()]) self._playlist_list_model.append([
', '.join(track.get_artists()),
album.get_title(),
track.get_track(),
track.get_title(),
album.get_date(),
album.get_hash()
])
pixbuf = None pixbuf = None
if album.get_cover() is not None: if album.get_cover() is not None:
try: try:
@ -970,7 +977,16 @@ class CoverPanel(mcg.MCGBase, Gtk.HPaned):
if pixbuf is None: if pixbuf is None:
pixbuf = self._playlist_grid.render_icon_pixbuf(Gtk.STOCK_MISSING_IMAGE, Gtk.IconSize.DIALOG) pixbuf = self._playlist_grid.render_icon_pixbuf(Gtk.STOCK_MISSING_IMAGE, Gtk.IconSize.DIALOG)
if pixbuf is not None: if pixbuf is not None:
self._playlist_grid_model.append([pixbuf, album.get_title(), GObject.markup_escape_text("\n".join([album.get_title(), album.get_date(), album.get_artist()])), album.get_hash()]) self._playlist_grid_model.append([
pixbuf,
album.get_title(),
GObject.markup_escape_text("\n".join([
album.get_title(),
album.get_date(),
', '.join(album.get_artists())
])),
album.get_hash()
])
Gdk.threads_enter() Gdk.threads_enter()
self._playlist_grid.set_model(self._playlist_grid_filter) self._playlist_grid.set_model(self._playlist_grid_filter)
@ -1001,7 +1017,14 @@ class CoverPanel(mcg.MCGBase, Gtk.HPaned):
album = albums[hash] album = albums[hash]
pixbuf = None pixbuf = None
for track in album.get_tracks(): for track in album.get_tracks():
self._library_list_model.append([album.get_artist(), album.get_title(), track.get_track(), track.get_title(), album.get_date(), hash]) self._library_list_model.append([
', '.join(track.get_artists()),
album.get_title(),
track.get_track(),
track.get_title(),
album.get_date(),
hash
])
try: try:
pixbuf = self._load_thumbnail(cache, album, size) pixbuf = self._load_thumbnail(cache, album, size)
except Exception as e: except Exception as e:
@ -1010,7 +1033,16 @@ class CoverPanel(mcg.MCGBase, Gtk.HPaned):
pixbuf = self._library_grid.render_icon_pixbuf(Gtk.STOCK_MISSING_IMAGE, Gtk.IconSize.DIALOG) pixbuf = self._library_grid.render_icon_pixbuf(Gtk.STOCK_MISSING_IMAGE, Gtk.IconSize.DIALOG)
if pixbuf is not None: if pixbuf is not None:
self._grid_pixbufs[album.get_hash()] = pixbuf self._grid_pixbufs[album.get_hash()] = pixbuf
self._library_grid_model.append([pixbuf, album.get_title(), GObject.markup_escape_text("\n".join([album.get_title(), album.get_date(), album.get_artist()])), hash]) self._library_grid_model.append([
pixbuf,
album.get_title(),
GObject.markup_escape_text("\n".join([
album.get_title(),
album.get_date(),
', '.join(album.get_artists())
])),
hash
])
i += 1 i += 1
GObject.idle_add(self._progress_bar.set_fraction, i/n) GObject.idle_add(self._progress_bar.set_fraction, i/n)