mirror of
https://github.com/beetbox/beets.git
synced 2026-01-04 15:03:22 +01:00
rename 'm3uupdate' plugin to 'importfeeds'. Handles two more output formats: multi m3u files (one per item imported) and symlinks. Update plugin docs.
Conflicts: docs/plugins/index.rst
This commit is contained in:
parent
8d773e27a2
commit
78fbe6d836
5 changed files with 130 additions and 106 deletions
101
beetsplug/importfeeds.py
Normal file
101
beetsplug/importfeeds.py
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
# This file is part of beets.
|
||||
# Copyright 2012, Fabrice Laporte.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining
|
||||
# a copy of this software and associated documentation files (the
|
||||
# "Software"), to deal in the Software without restriction, including
|
||||
# without limitation the rights to use, copy, modify, merge, publish,
|
||||
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||
# permit persons to whom the Software is furnished to do so, subject to
|
||||
# the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be
|
||||
# included in all copies or substantial portions of the Software.
|
||||
|
||||
"""Write paths of imported files in various formats to ease later import in a
|
||||
music player.
|
||||
"""
|
||||
|
||||
from __future__ import with_statement
|
||||
import datetime
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
|
||||
from beets import ui
|
||||
from beets.plugins import BeetsPlugin
|
||||
from beets.util import normpath, copy
|
||||
|
||||
M3U_DEFAULT_NAME = 'imported.m3u'
|
||||
|
||||
class ImportFeedsPlugin(BeetsPlugin):
|
||||
def configure(self, config):
|
||||
global _feeds_formats, _feeds_dir, _m3u_name
|
||||
|
||||
_feeds_formats = ui.config_val(config, 'importfeeds', 'feeds_formats',
|
||||
'').split()
|
||||
_feeds_dir = ui.config_val(config, 'importfeeds', 'feeds_dir', None)
|
||||
_m3u_name = ui.config_val(config, 'importfeeds', 'm3u_name',
|
||||
M3U_DEFAULT_NAME)
|
||||
|
||||
if _feeds_dir and not os.path.exists(_feeds_dir):
|
||||
os.makedirs(_feeds_dir)
|
||||
|
||||
def _get_feeds_dir(lib):
|
||||
"""Given a Library object, return the path to the feeds directory to be
|
||||
used (either in the library directory or an explicitly configured
|
||||
path). Ensures that the directory exists.
|
||||
"""
|
||||
# Inside library directory.
|
||||
dirpath = lib.directory
|
||||
|
||||
# Ensure directory exists.
|
||||
if not os.path.exists(dirpath):
|
||||
os.makedirs(dirpath)
|
||||
return dirpath
|
||||
|
||||
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(_feeds_dir, date+'_'+basename+'.m3u'))
|
||||
return path
|
||||
|
||||
def _write_m3u(m3u_path, items_paths):
|
||||
"""Append relative paths to items into m3u file.
|
||||
"""
|
||||
with open(m3u_path, 'w+') as f:
|
||||
for path in items_paths:
|
||||
f.write(path + '\n')
|
||||
|
||||
def _record_items(lib, basename, items):
|
||||
"""Records relative paths to the given items for each feed format
|
||||
"""
|
||||
global _feeds_dir
|
||||
if not _feeds_dir:
|
||||
_feeds_dir = _get_feeds_dir(lib)
|
||||
|
||||
paths = []
|
||||
for item in items:
|
||||
paths.append(os.path.relpath(item.path, _feeds_dir))
|
||||
|
||||
if 'm3u' in _feeds_formats:
|
||||
m3u_path = os.path.join(_feeds_dir, _m3u_name)
|
||||
_write_m3u(m3u_path, paths)
|
||||
|
||||
if 'm3u_multi' in _feeds_formats:
|
||||
m3u_path = _build_m3u_filename(basename)
|
||||
_write_m3u(m3u_path, paths)
|
||||
|
||||
if 'link' in _feeds_formats:
|
||||
for path in paths:
|
||||
os.symlink(path, os.path.join(_feeds_dir, os.path.basename(path)))
|
||||
|
||||
@ImportFeedsPlugin.listen('album_imported')
|
||||
def album_imported(lib, album, config):
|
||||
_record_items(lib, album.album, album.items())
|
||||
@ImportFeedsPlugin.listen('item_imported')
|
||||
def item_imported(lib, item, config):
|
||||
_record_items(lib, item.title, [item])
|
||||
|
|
@ -1,85 +0,0 @@
|
|||
# This file is part of beets.
|
||||
# Copyright 2012, Fabrice Laporte.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining
|
||||
# a copy of this software and associated documentation files (the
|
||||
# "Software"), to deal in the Software without restriction, including
|
||||
# without limitation the rights to use, copy, modify, merge, publish,
|
||||
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||
# permit persons to whom the Software is furnished to do so, subject to
|
||||
# the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be
|
||||
# included in all copies or substantial portions of the Software.
|
||||
|
||||
"""Write paths of imported files in a m3u file to ease later import in a
|
||||
music player.
|
||||
"""
|
||||
|
||||
from __future__ import with_statement
|
||||
import datetime
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
|
||||
from beets import ui
|
||||
from beets.plugins import BeetsPlugin
|
||||
from beets.util import normpath
|
||||
|
||||
_m3u_dirpath = None # If unspecified, use file in library directory.
|
||||
|
||||
class m3uPlugin(BeetsPlugin):
|
||||
def configure(self, config):
|
||||
global _m3u_dirpath, _m3u_fixedname
|
||||
_m3u_dirpath = ui.config_val(config, 'm3uupdate', 'm3u_dirpath', None)
|
||||
|
||||
if _m3u_dirpath:
|
||||
_m3u_dirpath = normpath(_m3u_dirpath)
|
||||
|
||||
_m3u_fixedname = ui.config_val(config, 'm3uupdate', 'm3u_fixedname', None)
|
||||
|
||||
def _get_m3u_dirpath(lib):
|
||||
"""Given a Library object, return the path to the M3U file to be
|
||||
used (either in the library directory or an explicitly configured
|
||||
path. Ensures that the containing directory exists.
|
||||
"""
|
||||
if _m3u_dirpath:
|
||||
# Explicitly specified.
|
||||
dirpath = _m3u_dirpath
|
||||
else:
|
||||
# Inside library directory.
|
||||
dirpath = lib.directory
|
||||
|
||||
# Ensure directory exists.
|
||||
if not os.path.exists(dirpath):
|
||||
os.makedirs(dirpath)
|
||||
|
||||
return dirpath
|
||||
|
||||
def _build_filename(lib, basename):
|
||||
"""Builds unique basename for the M3U file."""
|
||||
if _m3u_fixedname:
|
||||
path = os.path.join(_get_m3u_dirpath(lib), _m3u_fixedname)
|
||||
else :
|
||||
m3u_dirpath = _get_m3u_dirpath(lib)
|
||||
basename = re.sub(r"[\s,'\"]", '_', basename)
|
||||
date = datetime.datetime.now().strftime("%Y%m%d_%Hh%M")
|
||||
path = normpath(os.path.join(m3u_dirpath, date+'_'+basename+'.m3u'))
|
||||
return path
|
||||
|
||||
def _record_items(m3u_path, items):
|
||||
"""Records relative paths to the given items in the appropriate M3U
|
||||
file.
|
||||
"""
|
||||
with open(m3u_path, 'a') as f:
|
||||
for item in items:
|
||||
path = os.path.relpath(item.path, os.path.dirname(m3u_path))
|
||||
f.write(path + '\n')
|
||||
|
||||
@m3uPlugin.listen('album_imported')
|
||||
def album_imported(lib, album, config):
|
||||
_record_items(_build_filename(lib, album.album), album.items())
|
||||
|
||||
@m3uPlugin.listen('item_imported')
|
||||
def item_imported(lib, item, config):
|
||||
_record_items(_build_filename(lib, item.title), [item])
|
||||
24
docs/plugins/importfeeds.rst
Normal file
24
docs/plugins/importfeeds.rst
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
ImportFeeds Plugin
|
||||
==================
|
||||
|
||||
The ``importfeeds`` plugin helps you keep track of newly imported music in your library.
|
||||
|
||||
To use the plugin, just put ``importfeeds`` on the ``plugins`` line in your
|
||||
:doc:`/reference/config`::
|
||||
|
||||
[beets]
|
||||
plugins: importfeeds
|
||||
|
||||
The ``feeds_dir`` parameter can be set to specify another folder than the default library directory.
|
||||
Three different types of outputs coexist, specify the ones you want to use by setting the ``feeds_formats`` parameter:
|
||||
|
||||
- ``m3u``: catalog the imports in a centralized playlist. By default, the playlist is named ``imported.m3u``. To use a different file, just set the ``m3u_name`` parameter inside the ``importfeeds`` config section.
|
||||
- ``m3u_multi``: create a new playlist for each import (uniquely named by appending the date and track/album name).
|
||||
- ``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 ``feeds_dir`` folder on the iTunes dock icon.
|
||||
|
||||
An example of ``importfeeds``configuration::
|
||||
|
||||
[importfeeds]
|
||||
feeds_formats: m3u link
|
||||
feeds_dir: ~/imports/
|
||||
m3u_name: newfiles.m3u
|
||||
|
|
@ -46,9 +46,13 @@ disabled by default, but you can turn them on as described above.
|
|||
inline
|
||||
scrub
|
||||
rewrite
|
||||
<<<<<<< HEAD
|
||||
m3uupdate
|
||||
rdm
|
||||
mbcollection
|
||||
=======
|
||||
importfeeds
|
||||
>>>>>>> 837b91b... rename 'm3uupdate' plugin to 'importfeeds'. Handles two more output formats: multi m3u files (one per item imported) and symlinks. Update plugin docs.
|
||||
|
||||
Autotagger Extensions
|
||||
''''''''''''''''''''''
|
||||
|
|
@ -77,7 +81,7 @@ Interoperability
|
|||
|
||||
* :doc:`mpdupdate`: Automatically notifies `MPD`_ whenever the beets library
|
||||
changes.
|
||||
* :doc:`m3uupdate`: Catalog imported files in an ``.m3u`` playlist file.
|
||||
* :doc:`importfeeds`: Keep track of imported files via ``.m3u`` playlist file(s) or symlinks.
|
||||
|
||||
Miscellaneous
|
||||
'''''''''''''
|
||||
|
|
|
|||
|
|
@ -1,20 +0,0 @@
|
|||
m3uUpdate Plugin
|
||||
================
|
||||
|
||||
The ``m3uupdate`` plugin keeps track of newly imported music in a central
|
||||
``.m3u`` playlist file. This file can be used to add new music to other players,
|
||||
such as iTunes.
|
||||
|
||||
To use the plugin, just put ``m3uupdate`` on the ``plugins`` line in your
|
||||
:doc:`/reference/config`::
|
||||
|
||||
[beets]
|
||||
plugins: m3uupdate
|
||||
|
||||
Every time an album or singleton item is imported, new paths will be written to
|
||||
the playlist file. By default, the plugin uses a file called ``imported.m3u``
|
||||
inside your beets library directory. To use a different file, just set the
|
||||
``m3u`` parameter inside the ``m3uupdate`` config section, like so::
|
||||
|
||||
[m3uupdate]
|
||||
m3u: ~/music.m3u
|
||||
Loading…
Reference in a new issue