diff --git a/MPDCoverGrid.py b/MPDCoverGrid.py index 8c54e65..8172a8c 100644 --- a/MPDCoverGrid.py +++ b/MPDCoverGrid.py @@ -4,16 +4,20 @@ from mpd import MPDClient +import os class MPDCoverGrid: + client = MPDClient() + albums = {} + + def __init__(self, host='localhost', port='6600', password=None): self._host = host self._port = port self._password = password - self.client = MPDClient() def connect(self): @@ -21,9 +25,6 @@ class MPDCoverGrid: self.client.connect(self._host, self._port) if self._password: self.client.password(self._password) - except CommandError as e: - # TODO Error - print(e) except IOError as e: # TODO Error print(e) @@ -37,3 +38,79 @@ class MPDCoverGrid: print(e) self.client = MPDClient() + + def getAlbums(self): + self.update() + return self.albums + + + def update(self): + for song in self.client.listallinfo(): + try: + if song['album'] not in self.albums: + self.albums[song['album']] = MCGAlbum(song['artist'], song['album'], os.path.dirname(song['file'])) + + album = self.albums[song['album']] + album.addTrack(song['title']) + except KeyError: + pass + + + + +class MCGAlbum(): + fileNames = ['folder', 'cover'] + fileExts = ['jpg', 'jpeg', 'png'] + + + def __init__(self, artist, title, path): + self.artist = artist + if type(self.artist) is list: + self.artist = self.artist[0] + self.title = title + self.path = path + self.tracks = [] + self.cover = None + self._findCover() + + + def getArtist(self): + return self.artist + + + def getTitle(self): + return self.title + + + def getPath(self): + return self.path + + + def addTrack(self, track): + self.tracks.append(track) + + + def getTracks(self): + return self.tracks + + + def getCover(self): + return self.cover + + + def _findCover(self): + names = list(self.fileNames) + names.append(self.title) + names.append(' - '.join((self.artist, self.title))) + + + for name in names: + for ext in self.fileExts: + filename = os.path.join('/home/oliver/Musik/', self.path, '.'.join([name, ext])) + if os.path.isfile(filename): + self.cover = filename + break + if self.cover is not None: + break + +