From da69c391ec765a7f9c1271dcb236ef5ae43f6a7d Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Tue, 8 Jun 2010 17:08:23 -0700 Subject: [PATCH] add "beet stats" command for a few entertaining statistics --- beet | 8 ++++++ beets/ui.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/beet b/beet index 845629e96..97bdf3ccd 100755 --- a/beet +++ b/beet @@ -166,6 +166,14 @@ class BeetsApp(cmdln.Cmdln): """ ui.device_add(self.lib, make_query(criteria), name) + def do_stats(self, subcmd, opts, *criteria): + """${cmd_name}: show statistics about the library or a query + + ${cmd_usage} + ${cmd_option_list} + """ + ui.show_stats(self.lib, make_query(criteria)) + if __name__ == '__main__': app = BeetsApp() sys.exit(app.main()) diff --git a/beets/ui.py b/beets/ui.py index 1a2afd446..83a5cac12 100644 --- a/beets/ui.py +++ b/beets/ui.py @@ -82,6 +82,41 @@ def _input_yn(prompt, require=False): ) return (sel == 'y') +def _human_bytes(size): + """Formats size, a number of bytes, in a human-readable way.""" + suffices = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'HB'] + for suffix in suffices: + if size < 1024: + return "%3.1f %s" % (size, suffix) + size /= 1024.0 + return "big" + +def _human_seconds(interval): + """Formats interval, a number of seconds, as a human-readable time + interval. + """ + units = [ + (1, 'second'), + (60, 'minute'), + (60, 'hour'), + (24, 'day'), + (7, 'week'), + (52, 'year'), + (10, 'decade'), + ] + for i in range(len(units)-1): + increment, suffix = units[i] + next_increment, _ = units[i+1] + interval /= float(increment) + if interval < next_increment: + break + else: + # Last unit. + increment, suffix = units[-1] + interval /= float(increment) + + return "%3.1f %ss" % (interval, suffix) + # Autotagging interface. def show_change(cur_artist, cur_album, items, info, dist): @@ -330,7 +365,7 @@ def device_add(lib, query, name): """Add items matching query from lib to a device with the given name. """ - items = self.lib.items(query=query) + items = lib.items(query=query) from beets import device pod = device.PodLibrary.by_name(name) @@ -352,3 +387,36 @@ def start_bpd(lib, host, port, password, debug): print 'Install "python-gst0.10", "py26-gst-python", or similar ' \ 'package to use BPD.' return + +def show_stats(lib, query): + """Shows some statistics about the matched items.""" + items = lib.items(query=query) + + total_size = 0 + total_time = 0.0 + total_items = 0 + artists = set() + albums = set() + + for item in items: + #fixme This is approximate, so people might complain that + # this total size doesn't match "du -sh". Could fix this + # by putting total file size in the database. + total_size += int(item.length * item.bitrate / 8) + total_time += item.length + total_items += 1 + artists.add(item.artist) + albums.add(item.album) + + print """Tracks: %i +Total time: %s +Total size: %s +Artists: %i +Albums: %i""" % ( + total_items, + _human_seconds(total_time), + _human_bytes(total_size), + len(artists), len(albums) + ) + +