Added a -e option to random that makes the distribution even among artists

This commit is contained in:
Georges Dubus 2013-05-03 18:19:44 +02:00
parent 9d6e25175e
commit bcae495c50
2 changed files with 18 additions and 0 deletions

View file

@ -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):

View file

@ -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``.