summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Bachelier <laurent@bachelier.name>2012-11-23 20:30:15 +0100
committerLaurent Bachelier <laurent@bachelier.name>2012-11-23 20:30:32 +0100
commit621f81cbeaf51f9a29a9b626ea054d42940c80eb (patch)
treec9000877bdce53f26ba94bf9e5c6c39a370a2213
parentAdd get_playlist_time() (diff)
downloadmpdat-621f81cbeaf51f9a29a9b626ea054d42940c80eb.tar.xz
Better playlist time, albums time, example
-rw-r--r--mpdrast/client.py18
-rwxr-xr-xpltime_example.py25
2 files changed, 43 insertions, 0 deletions
diff --git a/mpdrast/client.py b/mpdrast/client.py
index a0f2fd2..ff5fbbb 100644
--- a/mpdrast/client.py
+++ b/mpdrast/client.py
@@ -93,6 +93,24 @@ class MPDrastClient(mpd.MPDClient):
return sum([song["time"] for song in pl])
+ def get_playlist_albums(self):
+ pl = (process.process_song(item) for item in self.playlistinfo())
+ d = None
+ songs = []
+ for song in pl:
+ if song["dir"] != d:
+ d = song["dir"]
+ if songs:
+ yield songs
+ songs = []
+ songs.append(song)
+
+
+ def get_playlist_albums_time(self):
+ for album in self.get_playlist_albums():
+ yield (sum([song["time"] for song in album]), album)
+
+
def _find_changing_pos(self, number, type):
pl = (process.process_song(item) for item in self.playlistinfo())
value = None
diff --git a/pltime_example.py b/pltime_example.py
new file mode 100755
index 0000000..ae0316f
--- /dev/null
+++ b/pltime_example.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python
+from mpdrast import MPDrastClient
+from os import environ
+
+import datetime
+
+
+def humandelta(seconds):
+ return str(datetime.timedelta(seconds=seconds))
+
+BRIGHT = '\x1b[1m'
+NORMAL = '\x1b[22m'
+
+mpd_host = environ.get("MPD_HOST", "localhost")
+mpd_port = environ.get("MPD_PORT", 6600)
+
+m = MPDrastClient()
+m.connect_from_env(mpd_host, mpd_port)
+
+
+for time, album in m.get_playlist_albums_time():
+ print '%s: %s%s%s' % (album[0]["dir"], BRIGHT, humandelta(time), NORMAL)
+
+print
+print 'TOTAL: %s%s%s' % (BRIGHT, humandelta(m.get_playlist_time()), NORMAL)