Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

play: add --random option #2350

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions beetsplug/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from beets import util
from os.path import relpath
from tempfile import NamedTemporaryFile
from beetsplug import random
import subprocess

# Indicate where arguments should be inserted into the command string.
Expand Down Expand Up @@ -81,6 +82,29 @@ def commands(self):
action='store',
help=u'add additional arguments to the command',
)
play_command.parser.add_option(
u'-r', u'--random',
action='store_true',
help=u'play random item',
)
play_command.parser.add_option(
u'-n', u'--number',
action='store',
type="int",
help=u'with -r: number of random items to choose',
default=1,
)
play_command.parser.add_option(
u'-e', u'--equal-chance',
action='store_true',
help=u'with -r: each artist has the same chance',
)
play_command.parser.add_option(
u'-t', u'--time',
action='store',
type="float",
help=u'with -r: total time in minutes of random items to choose',
)
play_command.func = self._play_command
return [play_command]

Expand All @@ -95,9 +119,15 @@ def _play_command(self, lib, opts, args):
# Perform search by album and add folders rather than tracks to
# playlist.
if opts.album:
selection = lib.albums(ui.decargs(args))
paths = []

# Send query to random plugin.
if opts.random:
selection = random.random_func(lib, opts, args,
print_list=False)
else:
selection = lib.albums(ui.decargs(args))

paths = []
sort = lib.get_default_album_sort()
for album in selection:
if use_folders:
Expand All @@ -109,7 +139,12 @@ def _play_command(self, lib, opts, args):

# Perform item query and add tracks to playlist.
else:
selection = lib.items(ui.decargs(args))
if opts.random:
selection = random.random_func(lib, opts, args,
print_list=False)
else:
selection = lib.items(ui.decargs(args))

paths = [item.path for item in selection]
if relative_to:
paths = [relpath(path, relative_to) for path in paths]
Expand Down
11 changes: 8 additions & 3 deletions beetsplug/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def random_objs(objs, album, number=1, time=None, equal_chance=False):
return _take(perm, number)


def random_func(lib, opts, args):
def random_func(lib, opts, args, print_list=True):
"""Select some random items or albums and print the results.
"""
# Fetch all the objects matching the query into a list.
Expand All @@ -125,9 +125,14 @@ def random_func(lib, opts, args):
# Print a random subset.
objs = random_objs(objs, opts.album, opts.number, opts.time,
opts.equal_chance)
for obj in objs:
print_(format(obj))

if print_list:
for obj in objs:
print_(format(obj))

# Return random subset to be used by other plugins.
else:
return objs

random_cmd = Subcommand('random',
help=u'choose a random track or album')
Expand Down
6 changes: 6 additions & 0 deletions docs/plugins/play.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ example::
indicates that you need to insert extra arguments before specifying the
playlist.

The ``--random`` (or ``-r``) flag will choose a random song (or album, when
used with ``-a``) from your library and play it. This option uses the
``random`` plugin, so options available to the ``random`` plugin are also
available to the ``play`` plugin (``--number`` (``-n``), ``--time`` (``-t``),
and ``--equal-chance`` (``-e``), see :doc:`random`).

Note on the Leakage of the Generated Playlists
----------------------------------------------

Expand Down