Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract out songs/playlists to make managing the MPD library easier. The refresh needs to become asynchronous and in a thread.
- Loading branch information
Zach Goldberg
committed
Feb 16, 2010
1 parent
e45f785
commit 8a7fce2
Showing
13 changed files
with
1,385 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from mpdobjects.playlist import MPDPlaylist | ||
from mpdobjects.song import MPDSong | ||
|
||
class MPDLibrary(object): | ||
def __init__(self, client, credentials): | ||
self.client = client | ||
self.creds = credentials | ||
|
||
def clear(self): | ||
self.playlists = [] | ||
self.songs = [] | ||
self.songs_by_file = {} | ||
self.id_inc = 0 | ||
self.items_by_id = {} | ||
|
||
|
||
def connect(self): | ||
self.client.connect(**self.creds) | ||
|
||
def disconnect(self): | ||
self.client.disconnect() | ||
|
||
def refresh(self): | ||
self.connect() | ||
|
||
self.clear() | ||
|
||
print "Downloading MPD Playlists / Library" | ||
for p in self.client.listplaylists(): | ||
print "Loading %s" % p['playlist'] | ||
songs = self.client.listplaylistinfo(p['playlist']) | ||
mpdsongs = [] | ||
for song in songs: | ||
if not song['file'] in self.songs_by_file: | ||
self.songs_by_file[song['file']] = MPDSong(song['file'], song.get('artist', 'Unknown'), | ||
song.get('album', 'Unknown'), song.get('title', 'Unknown') | ||
) | ||
|
||
self.register_song(self.songs_by_file[song['file']]) | ||
|
||
mpdsongs.append(self.songs_by_file[song['file']]) | ||
|
||
playlist = MPDPlaylist(p['playlist'], mpdsongs) | ||
self.register_playlist(playlist) | ||
|
||
cursongs = self.client.playlistinfo() | ||
self.disconnect() | ||
|
||
curmpdsongs = [] | ||
for song in cursongs: | ||
if not song['file'] in self.songs_by_file: | ||
self.songs_by_file[song['file']] = MPDSong(song['file'], song.get('artist', 'Unknown'), | ||
song.get('album', 'Unknown'), song.get('title', 'Unknown')) | ||
self.register_song(self.songs_by_file[song['file']]) | ||
curmpdsongs.append(self.songs_by_file[song['file']]) | ||
|
||
self.register_playlist(MPDPlaylist('Current Playlist', curmpdsongs)) | ||
self.register_playlist(MPDPlaylist('All Songs', self.songs)) | ||
self.playlists.reverse() | ||
|
||
def get_by_id(self, id): | ||
return self.items_by_id.get(id, None) | ||
|
||
|
||
def next_id(self): | ||
self.id_inc += 1 | ||
return self.id_inc | ||
|
||
def register_item(self, obj, list): | ||
obj.set_id(self.next_id()) | ||
self.items_by_id[obj.get_id()] = obj | ||
list.append(obj) | ||
|
||
def register_playlist(self, playlist): | ||
self.register_item(playlist, self.playlists) | ||
|
||
def register_song(self, song): | ||
self.register_item(song, self.songs) | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class MPDObject(object): | ||
def __init__(self): | ||
self.id = None | ||
|
||
def set_id(self, id): | ||
self.id = id | ||
|
||
def get_id(self): | ||
return self.id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from mpdobjects.mpdobject import MPDObject | ||
|
||
class MPDPlaylist(MPDObject): | ||
def __init__(self, name, songs): | ||
self.name = name | ||
self.songs = songs | ||
|
||
def writeself(self, writer): | ||
container = writer.add_container() | ||
container.set_title(self.name) | ||
container.set_child_count(len(self.songs)) | ||
container.set_id(str(self.get_id())) | ||
|
||
def writeall(self, writer): | ||
for song in self.songs: | ||
song.writeself(writer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from mpdobjects.mpdobject import MPDObject | ||
|
||
class MPDSong(MPDObject): | ||
def __init__(self, file, artist, album, title): | ||
self.file = file | ||
self.artist = artist | ||
self.album = album | ||
self.title = title | ||
|
||
def writeself(self, writer): | ||
item = writer.add_item() | ||
item.set_title(self.title) | ||
item.set_artist(self.artist) | ||
item.set_album(self.album) | ||
item.set_id(str(self.get_id())) | ||
|
||
res = item.add_resource() | ||
res.set_uri("mpd://%s" % self.get_id()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.