Stream-friendly smart playlists

This commit introduced a way to generate a stream-frienldy playlists.
This commit is contained in:
Kirill A. Korinsky 2021-08-14 02:41:22 +02:00
parent 58cfa7357d
commit b0f7418372
No known key found for this signature in database
GPG key ID: 98D8D9867759226E
3 changed files with 20 additions and 1 deletions

View file

@ -28,6 +28,12 @@ from beets.dbcore.query import MultipleSort, ParsingError
import os import os
import six import six
try:
from urllib.request import pathname2url
except ImportError:
# python2 is a bit different
from urllib import pathname2url
class SmartPlaylistPlugin(BeetsPlugin): class SmartPlaylistPlugin(BeetsPlugin):
@ -39,8 +45,11 @@ class SmartPlaylistPlugin(BeetsPlugin):
'auto': True, 'auto': True,
'playlists': [], 'playlists': [],
'forward_slash': False, 'forward_slash': False,
'prefix': u'',
'urlencode': False,
}) })
self.config['prefix'].redact = True # May contain username/password.
self._matched_playlists = None self._matched_playlists = None
self._unmatched_playlists = None self._unmatched_playlists = None
@ -201,6 +210,7 @@ class SmartPlaylistPlugin(BeetsPlugin):
if item_path not in m3us[m3u_name]: if item_path not in m3us[m3u_name]:
m3us[m3u_name].append(item_path) m3us[m3u_name].append(item_path)
prefix = bytestring_path(self.config['prefix'].as_str())
# Write all of the accumulated track lists to files. # Write all of the accumulated track lists to files.
for m3u in m3us: for m3u in m3us:
m3u_path = normpath(os.path.join(playlist_dir, m3u_path = normpath(os.path.join(playlist_dir,
@ -210,6 +220,8 @@ class SmartPlaylistPlugin(BeetsPlugin):
for path in m3us[m3u]: for path in m3us[m3u]:
if self.config['forward_slash'].get(): if self.config['forward_slash'].get():
path = path_as_posix(path) path = path_as_posix(path)
f.write(path + b'\n') if self.config['urlencode']:
path = bytestring_path(pathname2url(path))
f.write(prefix + path + b'\n')
self._log.info(u"{0} playlists updated", len(self._matched_playlists)) self._log.info(u"{0} playlists updated", len(self._matched_playlists))

View file

@ -2166,6 +2166,9 @@ And many little fixes and improvements:
* When there's a parse error in a query (for example, when you type a * When there's a parse error in a query (for example, when you type a
malformed date in a :ref:`date query <datequery>`), beets now stops with an malformed date in a :ref:`date query <datequery>`), beets now stops with an
error instead of silently ignoring the query component. error instead of silently ignoring the query component.
* :doc:`/plugins/smartplaylist`: Stream-friendly smart playlists.
The ``splupdate`` command can now also add a URL-encodable prefix to every
path in the playlist file.
For developers: For developers:

View file

@ -101,3 +101,7 @@ other configuration options are:
If you intend to use this plugin to generate playlists for MPD on If you intend to use this plugin to generate playlists for MPD on
Windows, set this to yes. Windows, set this to yes.
Default: Use system separator. Default: Use system separator.
- **prefix**: Prepend this string to every path in the playlist file. For
example, you could use the URL for a server where the music is stored.
Default: empty string.
- **urlencoded**: URL-encode all paths. Default: ``no``.