Skip to content

Commit

Permalink
Merge pull request #4863 from JOJ0/importfeeds_per_session
Browse files Browse the repository at this point in the history
Add m3u per session feature to importfeeds plugin
  • Loading branch information
JOJ0 authored Jul 28, 2023
2 parents e919c48 + 342421a commit e7dec4e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
24 changes: 23 additions & 1 deletion beetsplug/importfeeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,21 @@
M3U_DEFAULT_NAME = 'imported.m3u'


def _build_m3u_session_filename(basename):
"""Builds unique m3u filename by putting current date between given
basename and file ending."""
date = datetime.datetime.now().strftime("%Y%m%d_%Hh%M")
basename = re.sub(r"(\.m3u|\.M3U)", '', basename)
path = normpath(os.path.join(
config['importfeeds']['dir'].as_filename(),
f'{basename}_{date}.m3u'
))
return path


def _build_m3u_filename(basename):
"""Builds unique m3u filename by appending given basename to current
date."""

basename = re.sub(r"[\s,/\\'\"]", '_', basename)
date = datetime.datetime.now().strftime("%Y%m%d_%Hh%M")
path = normpath(os.path.join(
Expand Down Expand Up @@ -70,6 +81,7 @@ def __init__(self):

self.register_listener('album_imported', self.album_imported)
self.register_listener('item_imported', self.item_imported)
self.register_listener('import_begin', self.import_begin)

def get_feeds_dir(self):
feeds_dir = self.config['dir'].get()
Expand Down Expand Up @@ -105,6 +117,10 @@ def _record_items(self, lib, basename, items):
m3u_path = os.path.join(feedsdir, m3u_basename)
_write_m3u(m3u_path, paths)

if 'm3u_session' in formats:
m3u_path = os.path.join(feedsdir, self.m3u_session)
_write_m3u(m3u_path, paths)

if 'm3u_multi' in formats:
m3u_path = _build_m3u_filename(basename)
_write_m3u(m3u_path, paths)
Expand All @@ -125,3 +141,9 @@ def album_imported(self, lib, album):

def item_imported(self, lib, item):
self._record_items(lib, item.title, [item])

def import_begin(self, session):
formats = self.config['formats'].as_str_seq()
if 'm3u_session' in formats:
self.m3u_session = _build_m3u_session_filename(
self.config['m3u_name'].as_str())
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ New features:
* :doc:`plugins/subsonicupdate`: Updates are now triggered whenever either the
beets database is changed or a smart playlist is created/updated.
:bug: `4862`
* :doc:`plugins/importfeeds`: Add a new output format allowing to save a
playlist once per import session.
:bug: `4863`

Bug fixes:

Expand Down
6 changes: 5 additions & 1 deletion docs/plugins/importfeeds.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ configuration file. The available options are:
- **m3u**: Catalog the imports in a centralized playlist.
- **m3u_multi**: Create a new playlist for each import (uniquely named by
appending the date and track/album name).
- **m3u_session**: Create a new playlist for each import session. The file
is named as ``m3u_name`` appending the date and time the import session
was started.
- **link**: Create a symlink for each imported item. This is the
recommended setting to propagate beets imports to your iTunes library:
just drag and drop the ``dir`` folder on the iTunes dock icon.
- **echo**: Do not write a playlist file at all, but echo a list of new
file paths to the terminal.

Default: None.
- **m3u_name**: Playlist name used by the ``m3u`` format.
- **m3u_name**: Playlist name used by the ``m3u`` format and as a prefix used
by the ``m3u_session`` format.
Default: ``imported.m3u``.
- **relative_to**: Make the m3u paths relative to another
folder than where the playlist is being written. If you're using importfeeds
Expand Down
18 changes: 18 additions & 0 deletions test/test_importfeeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tempfile
import shutil
import unittest
import datetime

from beets import config
from beets.library import Item, Album, Library
Expand Down Expand Up @@ -54,6 +55,23 @@ def test_playlist_in_subdir(self):
self.assertTrue(os.path.isdir(playlist_subdir))
self.assertTrue(os.path.isfile(playlist))

def test_playlist_per_session(self):
config['importfeeds']['formats'] = 'm3u_session'
config['importfeeds']['m3u_name'] = 'imports.m3u'
album = Album(album='album/name', id=1)
item_path = os.path.join('path', 'to', 'item')
item = Item(title='song', album_id=1, path=item_path)
self.lib.add(album)
self.lib.add(item)

self.importfeeds.import_begin(self)
self.importfeeds.album_imported(self.lib, album)
date = datetime.datetime.now().strftime("%Y%m%d_%Hh%M")
playlist = os.path.join(self.feeds_dir, 'imports_' + date + '.m3u')
self.assertTrue(os.path.isfile(playlist))
with open(playlist) as playlist_contents:
self.assertIn(item_path, playlist_contents.read())


def suite():
return unittest.TestLoader().loadTestsFromName(__name__)
Expand Down

0 comments on commit e7dec4e

Please sign in to comment.