You are not logged in.

#1 2009-11-18 16:46:39

x0rg
Member
From: Switzerland
Registered: 2009-07-12
Posts: 116

[SOLVED] python-mpd: get list of tracks from database

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

#2 2009-11-18 18:05:20

pseup
Member
Registered: 2008-06-06
Posts: 103

Re: [SOLVED] python-mpd: get list of tracks from database

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 tongue

Offline

#3 2009-11-20 02:23:35

N30N
Member
Registered: 2007-04-08
Posts: 273

Re: [SOLVED] python-mpd: get list of tracks from database

I've never used python-mpd so can't help with that but a more pythonic way of doing the following…

pseup wrote:
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. wink

Last edited by N30N (2009-11-20 02:26:20)

Offline

#4 2009-11-20 12:41:26

x0rg
Member
From: Switzerland
Registered: 2009-07-12
Posts: 116

Re: [SOLVED] python-mpd: get list of tracks from database

Thanks guys, I modified pseup's solution a bit and it works fine!

Offline

Board footer

Powered by FluxBB