From bcae495c506ca7f9eb09aed5f00c24de2434b495 Mon Sep 17 00:00:00 2001 From: Georges Dubus Date: Fri, 3 May 2013 18:19:44 +0200 Subject: [PATCH] Added a -e option to random that makes the distribution even among artists --- beetsplug/random.py | 14 ++++++++++++++ docs/plugins/random.rst | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/beetsplug/random.py b/beetsplug/random.py index dbf1c0b73..d543cfd1b 100644 --- a/beetsplug/random.py +++ b/beetsplug/random.py @@ -19,6 +19,8 @@ from beets.plugins import BeetsPlugin from beets.ui import Subcommand, decargs, print_obj from beets.util.functemplate import Template import random +from operator import attrgetter +from itertools import groupby def random_item(lib, opts, args): query = decargs(args) @@ -32,9 +34,19 @@ def random_item(lib, opts, args): objs = list(lib.albums(query=query)) else: objs = list(lib.items(query=query)) + if opts.equal_chance: + key = attrgetter('albumartist') + objs.sort(key=key) + # Now the objs are list of albums or items + objs = [list(v) for k, v in groupby(objs, key)] + number = min(len(objs), opts.number) objs = random.sample(objs, number) + if opts.equal_chance: + # Select one random item from that artist + objs = map(random.choice, objs) + for item in objs: print_obj(item, lib, template) @@ -48,6 +60,8 @@ random_cmd.parser.add_option('-f', '--format', action='store', help='print with custom format', default=None) random_cmd.parser.add_option('-n', '--number', action='store', type="int", help='number of objects to choose', default=1) +random_cmd.parser.add_option('-e', '--equal-chance', action='store_true', + help='each artist has the same chance') random_cmd.func = random_item class Random(BeetsPlugin): diff --git a/docs/plugins/random.rst b/docs/plugins/random.rst index a7fac2579..ef8737b52 100644 --- a/docs/plugins/random.rst +++ b/docs/plugins/random.rst @@ -16,6 +16,10 @@ command (see :doc:`/reference/cli`). To choose an album instead of a single track, use ``-a``; to print paths to items instead of metadata, use ``-p``; and to use a custom format for printing, use ``-f FORMAT``. +If the ``-e`` option is passed, the random choice will be even among +artists (the albumartist field) This makes sure that your antholofgy +of Bob Dylan won't make you listen to Bob Dylan 50% of the time. + The ``-n NUMBER`` option controls the number of objects that are selected and printed (default 1). To select 5 tracks from your library, type ``beet random -n5``.