From ab513f7bc2ba088ff393b4796cb9fe5ae9d9cf45 Mon Sep 17 00:00:00 2001 From: Bruno Tournay Date: Wed, 5 Mar 2014 21:36:28 +0100 Subject: [PATCH] Add the ability to gather the result of ^Cveral queries into a smartplaylist --- beetsplug/smartplaylist.py | 21 +++++++++++++++++---- docs/plugins/smartplaylist.rst | 8 ++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/beetsplug/smartplaylist.py b/beetsplug/smartplaylist.py index 51368275f..e1eb2075e 100644 --- a/beetsplug/smartplaylist.py +++ b/beetsplug/smartplaylist.py @@ -35,7 +35,18 @@ def update_playlists(lib): relative_to = normpath(relative_to) for playlist in playlists: - items = lib.items(library.get_query(playlist['query'], library.Item)) + # Default is to not keep_duplicate + keep_duplicate = playlist.has_key('keep_duplicate') and \ + playlist['keep_duplicate'] + + # Query attribute could be a single query or a list of queries + queries = playlist['query'] + if not isinstance(queries, (list, tuple)): + queries = [queries] + items = [] + for query in queries: + items.extend(lib.items(library.get_query(query, library.Item))) + m3us = {} basename = playlist['name'].encode('utf8') # As we allow tags in the m3u names, we'll need to iterate through @@ -44,10 +55,12 @@ def update_playlists(lib): m3u_name = item.evaluate_template(basename, True) if not (m3u_name in m3us): m3us[m3u_name] = [] + item_path = item.path if relative_to: - m3us[m3u_name].append(os.path.relpath(item.path, relative_to)) - else: - m3us[m3u_name].append(item.path) + item_path = os.path.relpath(item.path, relative_to) + # Check if we want to add the item. + if keep_duplicate or not item_path in m3us[m3u_name]: + m3us[m3u_name].append(item_path) # Now iterate through the m3us that we need to generate for m3u in m3us: m3u_path = normpath(os.path.join(playlist_dir, m3u)) diff --git a/docs/plugins/smartplaylist.rst b/docs/plugins/smartplaylist.rst index 206815c68..7b8dfb036 100644 --- a/docs/plugins/smartplaylist.rst +++ b/docs/plugins/smartplaylist.rst @@ -44,6 +44,14 @@ For more advanced usage, you can use template syntax (see This will query all the songs in 2010 and 2011 and generate the two playlist files `ReleasedIn2010.m3u` and `ReleasedIn2011.m3u` using those songs. +You can also gather the results of several queries by putting them in a list. By +default duplicates are removed, but you can keep all of them with the +``keep_duplicate: yes`` additional directive. For example:: + + - query: ['artist:beatles', 'genre:"beatles cover"'] + name: 'BeatlesUniverse.m3u' + keep_duplicate: yes + By default, all playlists are regenerated after every beets command that changes the library database. To force regeneration, you can invoke it manually from the command line::