Skip to content

Instantly share code, notes, and snippets.

@tremby
Created January 6, 2012 18:02
Show Gist options
  • Save tremby/1571674 to your computer and use it in GitHub Desktop.
Save tremby/1571674 to your computer and use it in GitHub Desktop.
albumsbylength -- print out all albums in MPD library prefixed with their total length
#!/usr/bin/env python
import mpd
HOST = "localhost"
PORT = 6600
PASS = None
client = mpd.MPDClient()
client.connect(HOST, PORT)
if PASS:
client.password(PASS)
client.iterate = True
albums = {}
for song in client.listallinfo():
try:
album = song["album"]
except KeyError:
continue
try:
artist = song["albumartist"]
except KeyError:
try:
artist = song["artist"]
except KeyError:
artist = "(unknown)"
try:
album = "%s -- %s (%s)" % (artist, album, song["date"][0:4])
except KeyError:
pass
if not album in albums:
albums[album] = 0
albums[album] += int(song["time"])
client.close()
client.disconnect()
for album, length in albums.iteritems():
print "%d:%02d\t%s" % (length / 60, length % 60, album)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment