You are not logged in.
Hi Archers,
I'm quite a python newbie and I want to get a list of all the tracks in the mpd db using python-mpd.
I experimented a bit with the 'listall' and 'list' functions but I couldn't find a solution.
In particular I need list of all tracks in the format '<artist> - <track>'
As I said I'm a python noob, and the python-documentation isn't that good.
I would apreciate any answer.
btw: Sorry for my bad english...
typos fixed
Last edited by x0rg (2009-11-20 12:42:44)
Offline
I'm a python newbie too, and I have only touched a little on python-mpd (dzen status bar pipe). But I think the listallinfo function is what you want.
This should spit out a list of every song in the database as 'Artist - Title' if the tags are set, 'Title' if there is a title tag but no artist, or just the filename if there isnt a title tag.
library = client.listallinfo()
for song in library:
songstring = ''
if 'title' in song:
if 'artist' in song:
songstring += song['artist'] + ' - '
songstring += song['title']
else:
songstring = song.get('file', '')
print songstring
Of course there is probably a better way of doing it, but this will give you an idea at least
Offline
I've never used python-mpd so can't help with that but a more pythonic way of doing the following…
for song in library: songstring = '' if 'title' in song: if 'artist' in song: songstring += song['artist'] + ' - ' songstring += song['title'] else: songstring = song.get('file', '') print songstring
Would be to use try block(s) and the string formatting operator eg.
for for track in library:
try:
print "%(artist)s - %(title)s" % track
except KeyError:
print track.get("file", "Unknown")
Obviously not functionally identical.
Last edited by N30N (2009-11-20 02:26:20)
Offline
Thanks guys, I modified pseup's solution a bit and it works fine!
Offline