mirror of
https://github.com/beetbox/beets.git
synced 2025-12-30 20:42:37 +01:00
Merge master back in to fork
This commit is contained in:
commit
ffa2fdd278
9 changed files with 331 additions and 20 deletions
|
|
@ -51,7 +51,7 @@ def replace_ext(path, ext):
|
|||
|
||||
|
||||
def get_format(fmt=None):
|
||||
"""Return the command tempate and the extension from the config.
|
||||
"""Return the command template and the extension from the config.
|
||||
"""
|
||||
if not fmt:
|
||||
fmt = config['convert']['format'].get(unicode).lower()
|
||||
|
|
|
|||
151
beetsplug/export.py
Normal file
151
beetsplug/export.py
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# This file is part of beets.
|
||||
#
|
||||
# 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.
|
||||
|
||||
"""Exports data from beets
|
||||
"""
|
||||
|
||||
from __future__ import division, absolute_import, print_function
|
||||
|
||||
import sys
|
||||
import json
|
||||
import codecs
|
||||
|
||||
from datetime import datetime, date
|
||||
from beets.plugins import BeetsPlugin
|
||||
from beets import ui
|
||||
from beets import mediafile
|
||||
from beetsplug.info import make_key_filter, library_data, tag_data
|
||||
|
||||
|
||||
class ExportEncoder(json.JSONEncoder):
|
||||
"""Deals with dates because JSON doesn't have a standard"""
|
||||
def default(self, o):
|
||||
if isinstance(o, datetime) or isinstance(o, date):
|
||||
return o.isoformat()
|
||||
return json.JSONEncoder.default(self, o)
|
||||
|
||||
|
||||
class ExportPlugin(BeetsPlugin):
|
||||
|
||||
def __init__(self):
|
||||
super(ExportPlugin, self).__init__()
|
||||
|
||||
self.config.add({
|
||||
'default_format': 'json',
|
||||
'json': {
|
||||
# json module formatting options
|
||||
'formatting': {
|
||||
'ensure_ascii': False,
|
||||
'indent': 4,
|
||||
'separators': (',', ': '),
|
||||
'sort_keys': True
|
||||
}
|
||||
},
|
||||
# TODO: Use something like the edit plugin
|
||||
# 'item_fields': []
|
||||
})
|
||||
|
||||
def commands(self):
|
||||
# TODO: Add option to use albums
|
||||
|
||||
cmd = ui.Subcommand('export', help=u'export data from beets')
|
||||
cmd.func = self.run
|
||||
cmd.parser.add_option(
|
||||
u'-l', u'--library', action='store_true',
|
||||
help=u'show library fields instead of tags',
|
||||
)
|
||||
cmd.parser.add_option(
|
||||
u'--append', action='store_true', default=False,
|
||||
help=u'if should append data to the file',
|
||||
)
|
||||
cmd.parser.add_option(
|
||||
u'-i', u'--include-keys', default=[],
|
||||
action='append', dest='included_keys',
|
||||
help=u'comma separated list of keys to show',
|
||||
)
|
||||
cmd.parser.add_option(
|
||||
u'-o', u'--output',
|
||||
help=u'path for the output file. If not given, will print the data'
|
||||
)
|
||||
return [cmd]
|
||||
|
||||
def run(self, lib, opts, args):
|
||||
|
||||
file_path = opts.output
|
||||
file_format = self.config['default_format'].get(str)
|
||||
file_mode = 'a' if opts.append else 'w'
|
||||
format_options = self.config[file_format]['formatting'].get(dict)
|
||||
|
||||
export_format = ExportFormat.factory(
|
||||
file_format, **{
|
||||
'file_path': file_path,
|
||||
'file_mode': file_mode
|
||||
}
|
||||
)
|
||||
|
||||
items = []
|
||||
data_collector = library_data if opts.library else tag_data
|
||||
|
||||
included_keys = []
|
||||
for keys in opts.included_keys:
|
||||
included_keys.extend(keys.split(','))
|
||||
key_filter = make_key_filter(included_keys)
|
||||
|
||||
for data_emitter in data_collector(lib, ui.decargs(args)):
|
||||
try:
|
||||
data, item = data_emitter()
|
||||
except (mediafile.UnreadableFileError, IOError) as ex:
|
||||
self._log.error(u'cannot read file: {0}', ex)
|
||||
continue
|
||||
|
||||
data = key_filter(data)
|
||||
items += [data]
|
||||
|
||||
export_format.export(items, **format_options)
|
||||
|
||||
|
||||
class ExportFormat(object):
|
||||
"""The output format type"""
|
||||
|
||||
@classmethod
|
||||
def factory(self, type, **kwargs):
|
||||
if type == "json":
|
||||
if kwargs['file_path']:
|
||||
return JsonFileFormat(**kwargs)
|
||||
else:
|
||||
return JsonPrintFormat()
|
||||
raise NotImplementedError()
|
||||
|
||||
def export(self, data, **kwargs):
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
class JsonPrintFormat(ExportFormat):
|
||||
"""Outputs to the console"""
|
||||
|
||||
def export(self, data, **kwargs):
|
||||
json.dump(data, sys.stdout, cls=ExportEncoder, **kwargs)
|
||||
|
||||
|
||||
class JsonFileFormat(ExportFormat):
|
||||
"""Saves in a json file"""
|
||||
|
||||
def __init__(self, file_path, file_mode=u'w', encoding=u'utf-8'):
|
||||
self.path = file_path
|
||||
self.mode = file_mode
|
||||
self.encoding = encoding
|
||||
|
||||
def export(self, data, **kwargs):
|
||||
with codecs.open(self.path, self.mode, self.encoding) as f:
|
||||
json.dump(data, f, cls=ExportEncoder, **kwargs)
|
||||
|
|
@ -30,6 +30,7 @@ from beets import ui
|
|||
from beets import util
|
||||
from beets import config
|
||||
from beets.util.artresizer import ArtResizer
|
||||
from beets.util import confit
|
||||
|
||||
try:
|
||||
import itunes
|
||||
|
|
@ -91,6 +92,9 @@ class Candidate(object):
|
|||
u'`enforce_ratio` may be violated.')
|
||||
return self.CANDIDATE_EXACT
|
||||
|
||||
short_edge = min(self.size)
|
||||
long_edge = max(self.size)
|
||||
|
||||
# Check minimum size.
|
||||
if extra['minwidth'] and self.size[0] < extra['minwidth']:
|
||||
self._log.debug(u'image too small ({} < {})',
|
||||
|
|
@ -98,10 +102,26 @@ class Candidate(object):
|
|||
return self.CANDIDATE_BAD
|
||||
|
||||
# Check aspect ratio.
|
||||
if extra['enforce_ratio'] and self.size[0] != self.size[1]:
|
||||
self._log.debug(u'image is not square ({} != {})',
|
||||
self.size[0], self.size[1])
|
||||
return self.CANDIDATE_BAD
|
||||
edge_diff = long_edge - short_edge
|
||||
if extra['enforce_ratio']:
|
||||
if extra['margin_px']:
|
||||
if edge_diff > extra['margin_px']:
|
||||
self._log.debug(u'image is not close enough to being '
|
||||
u'square, ({} - {} > {})',
|
||||
long_edge, short_edge, extra['margin_px'])
|
||||
return self.CANDIDATE_BAD
|
||||
elif extra['margin_percent']:
|
||||
margin_px = extra['margin_percent'] * long_edge
|
||||
if edge_diff > margin_px:
|
||||
self._log.debug(u'image is not close enough to being '
|
||||
u'square, ({} - {} > {})',
|
||||
long_edge, short_edge, margin_px)
|
||||
return self.CANDIDATE_BAD
|
||||
elif edge_diff:
|
||||
# also reached for margin_px == 0 and margin_percent == 0.0
|
||||
self._log.debug(u'image is not square ({} != {})',
|
||||
self.size[0], self.size[1])
|
||||
return self.CANDIDATE_BAD
|
||||
|
||||
# Check maximum size.
|
||||
if extra['maxwidth'] and self.size[0] > extra['maxwidth']:
|
||||
|
|
@ -634,9 +654,16 @@ SOURCE_NAMES = {v: k for k, v in ART_SOURCES.items()}
|
|||
|
||||
|
||||
class FetchArtPlugin(plugins.BeetsPlugin, RequestMixin):
|
||||
PAT_PX = r"(0|[1-9][0-9]*)px"
|
||||
PAT_PERCENT = r"(100(\.00?)?|[1-9]?[0-9](\.[0-9]{1,2})?)%"
|
||||
|
||||
def __init__(self):
|
||||
super(FetchArtPlugin, self).__init__()
|
||||
|
||||
# Holds paths to downloaded images between fetching them and
|
||||
# placing them in the filesystem.
|
||||
self.art_paths = {}
|
||||
|
||||
self.config.add({
|
||||
'auto': True,
|
||||
'minwidth': 0,
|
||||
|
|
@ -653,13 +680,25 @@ class FetchArtPlugin(plugins.BeetsPlugin, RequestMixin):
|
|||
self.config['google_key'].redact = True
|
||||
self.config['fanarttv_key'].redact = True
|
||||
|
||||
# Holds paths to downloaded images between fetching them and
|
||||
# placing them in the filesystem.
|
||||
self.art_paths = {}
|
||||
|
||||
self.minwidth = self.config['minwidth'].get(int)
|
||||
self.maxwidth = self.config['maxwidth'].get(int)
|
||||
self.enforce_ratio = self.config['enforce_ratio'].get(bool)
|
||||
|
||||
# allow both pixel and percentage-based margin specifications
|
||||
self.enforce_ratio = self.config['enforce_ratio'].get(
|
||||
confit.OneOf([bool,
|
||||
confit.String(pattern=self.PAT_PX),
|
||||
confit.String(pattern=self.PAT_PERCENT)]))
|
||||
self.margin_px = None
|
||||
self.margin_percent = None
|
||||
if type(self.enforce_ratio) is unicode:
|
||||
if self.enforce_ratio[-1] == u'%':
|
||||
self.margin_percent = float(self.enforce_ratio[:-1]) / 100
|
||||
elif self.enforce_ratio[-2:] == u'px':
|
||||
self.margin_px = int(self.enforce_ratio[:-2])
|
||||
else:
|
||||
# shouldn't happen
|
||||
raise confit.ConfigValueError()
|
||||
self.enforce_ratio = True
|
||||
|
||||
cover_names = self.config['cover_names'].as_str_seq()
|
||||
self.cover_names = map(util.bytestring_path, cover_names)
|
||||
|
|
@ -681,13 +720,6 @@ class FetchArtPlugin(plugins.BeetsPlugin, RequestMixin):
|
|||
available_sources.remove(u'google')
|
||||
sources_name = plugins.sanitize_choices(
|
||||
self.config['sources'].as_str_seq(), available_sources)
|
||||
if not self.config['fanarttv_key'].get() and \
|
||||
u'fanarttv' in sources_name:
|
||||
self._log.warn(
|
||||
u'fanart.tv source enabled, but no personal API given. This '
|
||||
u'works as of now, however, fanart.tv prefers users to '
|
||||
u'register a personal key. Additionaly this makes new art '
|
||||
u'available shorter after its upload. See the documentation.')
|
||||
if 'remote_priority' in self.config:
|
||||
self._log.warning(
|
||||
u'The `fetch_art.remote_priority` configuration option has '
|
||||
|
|
@ -765,6 +797,8 @@ class FetchArtPlugin(plugins.BeetsPlugin, RequestMixin):
|
|||
'cover_names': self.cover_names,
|
||||
'cautious': self.cautious,
|
||||
'enforce_ratio': self.enforce_ratio,
|
||||
'margin_px': self.margin_px,
|
||||
'margin_percent': self.margin_percent,
|
||||
'minwidth': self.minwidth,
|
||||
'maxwidth': self.maxwidth}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,12 @@ New features:
|
|||
for a Microsoft Azure Marketplace free account. Thanks to :user:`Kraymer`.
|
||||
* :doc:`/plugins/fetchart`: Album art can now be fetched from `fanart.tv`_.
|
||||
Albums are matched using the ``mb_releasegroupid`` tag.
|
||||
|
||||
* :doc:`/plugins/fetchart`: The ``enforce_ratio`` option was enhanced and now
|
||||
allows specifying a certain deviation that a valid image may have from being
|
||||
exactly square.
|
||||
* :doc:`/plugins/export`: A new plugin to export the data from queries to a
|
||||
json format. Thanks to :user:`GuilhermeHideki`.
|
||||
* New :doc:`/plugins/hook` that allows commands to be executed when an event is
|
||||
emitted by beets. :bug:`1561` :bug:`1603`
|
||||
|
||||
|
|
|
|||
70
docs/plugins/export.rst
Normal file
70
docs/plugins/export.rst
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
Export Plugin
|
||||
=============
|
||||
|
||||
The ``export`` plugin lets you get data from the items and export the content to
|
||||
a ``json`` file.
|
||||
|
||||
Configuration
|
||||
-------------
|
||||
To configure the plugin, make a ``export:`` section in your configuration
|
||||
file. The default options are::
|
||||
|
||||
export:
|
||||
default_format: json
|
||||
json:
|
||||
formatting:
|
||||
ensure_ascii: False
|
||||
indent: 4
|
||||
separators: [',' , ': ']
|
||||
sort_keys: true
|
||||
|
||||
- **default_format**: Choose the format of the exported content.
|
||||
Supports json only for now.
|
||||
|
||||
Each format have their own options.
|
||||
|
||||
The ``json`` formatting uses the `json`_ standard library options.
|
||||
Using custom options overwrites all options at the same level.
|
||||
The default options used here are:
|
||||
|
||||
- **ensure_ascii**: All non-ASCII characters are escaped with `\uXXXX`, if true.
|
||||
|
||||
- **indent**: The number of spaces for indentation.
|
||||
|
||||
- **separators**: A ``(item_separator, dict_separator)`` tuple
|
||||
|
||||
- **sort_keys**: Sorts the keys of the json
|
||||
|
||||
.. _json: https://docs.python.org/2/library/json.html#basic-usage
|
||||
|
||||
Using
|
||||
-----
|
||||
|
||||
Enable the ``export`` plugin (see :ref:`using-plugins` for help) and then add a
|
||||
``export`` section to your :doc:`configuration file </reference/config>`
|
||||
|
||||
To use, you can enter a :doc:`query </reference/query>` to get the data from
|
||||
your library::
|
||||
|
||||
$ beet export beatles
|
||||
|
||||
If you just want to see specific properties you can use the
|
||||
``--include-keys`` option to filter them. The argument is a
|
||||
comma-separated list of simple glob patterns where ``*`` matches any
|
||||
string. For example::
|
||||
|
||||
$ beet export -i 'title,mb*' beatles
|
||||
|
||||
Will only show the ``title`` property and all properties starting with
|
||||
``mb``. You can add the ``-i`` option multiple times to the command
|
||||
line.
|
||||
|
||||
Additional command-line options include:
|
||||
|
||||
* ``--library`` or ``-l``: Show data from the library database instead of the
|
||||
files' tags.
|
||||
|
||||
* ``--output`` or ``-o``: Path for an output file. If not informed, will print
|
||||
the data in the console.
|
||||
|
||||
* ``--append``: Appends the data to the file instead of writing.
|
||||
|
|
@ -42,7 +42,11 @@ file. The available options are:
|
|||
too big. The resize operation reduces image width to at most ``maxwidth``
|
||||
pixels. The height is recomputed so that the aspect ratio is preserved.
|
||||
- **enforce_ratio**: Only images with a width:height ratio of 1:1 are
|
||||
considered as valid album art candidates. Default: ``no``.
|
||||
considered as valid album art candidates if set to ``yes``.
|
||||
It is also possible to specify a certain deviation to the exact ratio to
|
||||
still be considered valid. This can be done either in pixels
|
||||
(``enforce_ratio: 10px``) or as a percentage of the longer edge
|
||||
(``enforce_ratio: 0.5%``). Default: ``no``.
|
||||
- **sources**: List of sources to search for images. An asterisk `*` expands
|
||||
to all available sources.
|
||||
Default: ``filesystem coverart itunes amazon albumart``, i.e., everything but
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ Each plugin has its own set of options that can be defined in a section bearing
|
|||
edit
|
||||
embedart
|
||||
embyupdate
|
||||
export
|
||||
fetchart
|
||||
fromfilename
|
||||
ftintitle
|
||||
|
|
@ -101,7 +102,7 @@ Metadata
|
|||
* :doc:`bpm`: Measure tempo using keystrokes.
|
||||
* :doc:`echonest`: Automatically fetch `acoustic attributes`_ from
|
||||
`the Echo Nest`_ (tempo, energy, danceability, ...).
|
||||
* :doc:`edit`: Edit metadata from a texteditor.
|
||||
* :doc:`edit`: Edit metadata from a text editor.
|
||||
* :doc:`embedart`: Embed album art images into files' metadata.
|
||||
* :doc:`fetchart`: Fetch album cover art from various sources.
|
||||
* :doc:`ftintitle`: Move "featured" artists from the artist field to the title
|
||||
|
|
@ -162,6 +163,7 @@ Miscellaneous
|
|||
* :doc:`convert`: Transcode music and embed album art while exporting to
|
||||
a different directory.
|
||||
* :doc:`duplicates`: List duplicate tracks or albums.
|
||||
* :doc:`export`: Export data from queries to a format.
|
||||
* :doc:`fuzzy`: Search albums and tracks with fuzzy string matching.
|
||||
* :doc:`hook`: Run a command when an event is emitted by beets.
|
||||
* :doc:`ihate`: Automatically skip albums and tracks during the import process.
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ from beets import importer
|
|||
from beets import logging
|
||||
from beets import util
|
||||
from beets.util.artresizer import ArtResizer, WEBPROXY
|
||||
from beets.util import confit
|
||||
|
||||
|
||||
logger = logging.getLogger('beets.test_art')
|
||||
|
|
@ -534,6 +535,26 @@ class ArtForAlbumTest(UseThePlugin):
|
|||
self.plugin.enforce_ratio = False
|
||||
self._assertImageIsValidArt(self.IMG_500x490, True)
|
||||
|
||||
def test_respect_enforce_ratio_px_above(self):
|
||||
self.plugin.enforce_ratio = True
|
||||
self.plugin.margin_px = 5
|
||||
self._assertImageIsValidArt(self.IMG_500x490, False)
|
||||
|
||||
def test_respect_enforce_ratio_px_below(self):
|
||||
self.plugin.enforce_ratio = True
|
||||
self.plugin.margin_px = 15
|
||||
self._assertImageIsValidArt(self.IMG_500x490, True)
|
||||
|
||||
def test_respect_enforce_ratio_percent_above(self):
|
||||
self.plugin.enforce_ratio = True
|
||||
self.plugin.margin_percent = (500 - 490) / 500 * 0.5
|
||||
self._assertImageIsValidArt(self.IMG_500x490, False)
|
||||
|
||||
def test_respect_enforce_ratio_percent_below(self):
|
||||
self.plugin.enforce_ratio = True
|
||||
self.plugin.margin_percent = (500 - 490) / 500 * 1.5
|
||||
self._assertImageIsValidArt(self.IMG_500x490, True)
|
||||
|
||||
def test_resize_if_necessary(self):
|
||||
self._require_backend()
|
||||
self.plugin.maxwidth = 300
|
||||
|
|
@ -559,6 +580,29 @@ class DeprecatedConfigTest(_common.TestCase):
|
|||
self.assertEqual(type(self.plugin.sources[-1]), fetchart.FileSystem)
|
||||
|
||||
|
||||
class EnforceRatioConfigTest(_common.TestCase):
|
||||
"""Throw some data at the regexes."""
|
||||
|
||||
def _load_with_config(self, values, should_raise):
|
||||
if should_raise:
|
||||
for v in values:
|
||||
config['fetchart']['enforce_ratio'] = v
|
||||
with self.assertRaises(confit.ConfigValueError):
|
||||
fetchart.FetchArtPlugin()
|
||||
else:
|
||||
for v in values:
|
||||
config['fetchart']['enforce_ratio'] = v
|
||||
fetchart.FetchArtPlugin()
|
||||
|
||||
def test_px(self):
|
||||
self._load_with_config(u'0px 4px 12px 123px'.split(), False)
|
||||
self._load_with_config(u'00px stuff5px'.split(), True)
|
||||
|
||||
def test_percent(self):
|
||||
self._load_with_config(u'0% 0.00% 5.1% 5% 100%'.split(), False)
|
||||
self._load_with_config(u'00% 1.234% foo5% 100.1%'.split(), True)
|
||||
|
||||
|
||||
def suite():
|
||||
return unittest.TestLoader().loadTestsFromName(__name__)
|
||||
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ class ModifyFileMocker(object):
|
|||
"""
|
||||
|
||||
def __init__(self, contents=None, replacements=None):
|
||||
""" `self.contents` and `self.replacements` are initalized here, in
|
||||
""" `self.contents` and `self.replacements` are initialized here, in
|
||||
order to keep the rest of the functions of this class with the same
|
||||
signature as `EditPlugin.get_editor()`, making mocking easier.
|
||||
- `contents`: string with the contents of the file to be used for
|
||||
|
|
|
|||
Loading…
Reference in a new issue