mirror of
https://github.com/rembo10/headphones.git
synced 2026-07-20 07:54:01 +01:00
update Beets
This commit is contained in:
+86
-55
@@ -22,16 +22,14 @@ import json
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
from distutils.spawn import find_executable
|
||||
|
||||
import requests
|
||||
|
||||
from beets import plugins
|
||||
from beets import util
|
||||
from beets import ui
|
||||
from beets import plugins, ui, util
|
||||
|
||||
# We use this field to check whether AcousticBrainz info is present.
|
||||
PROBE_FIELD = 'mood_acoustic'
|
||||
PROBE_FIELD = "mood_acoustic"
|
||||
|
||||
|
||||
class ABSubmitError(Exception):
|
||||
@@ -47,39 +45,39 @@ def call(args):
|
||||
return util.command_output(args).stdout
|
||||
except subprocess.CalledProcessError as e:
|
||||
raise ABSubmitError(
|
||||
'{} exited with status {}'.format(args[0], e.returncode)
|
||||
"{} exited with status {}".format(args[0], e.returncode)
|
||||
)
|
||||
|
||||
|
||||
class AcousticBrainzSubmitPlugin(plugins.BeetsPlugin):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
self.config.add({
|
||||
'extractor': '',
|
||||
'force': False,
|
||||
'pretend': False
|
||||
})
|
||||
self._log.warning("This plugin is deprecated.")
|
||||
|
||||
self.extractor = self.config['extractor'].as_str()
|
||||
self.config.add(
|
||||
{"extractor": "", "force": False, "pretend": False, "base_url": ""}
|
||||
)
|
||||
|
||||
self.extractor = self.config["extractor"].as_str()
|
||||
if self.extractor:
|
||||
self.extractor = util.normpath(self.extractor)
|
||||
# Expicit path to extractor
|
||||
# Explicit path to extractor
|
||||
if not os.path.isfile(self.extractor):
|
||||
raise ui.UserError(
|
||||
'Extractor command does not exist: {0}.'.
|
||||
format(self.extractor)
|
||||
"Extractor command does not exist: {0}.".format(
|
||||
self.extractor
|
||||
)
|
||||
)
|
||||
else:
|
||||
# Implicit path to extractor, search for it in path
|
||||
self.extractor = 'streaming_extractor_music'
|
||||
self.extractor = "streaming_extractor_music"
|
||||
try:
|
||||
call([self.extractor])
|
||||
except OSError:
|
||||
raise ui.UserError(
|
||||
'No extractor command found: please install the extractor'
|
||||
' binary from https://acousticbrainz.org/download'
|
||||
"No extractor command found: please install the extractor"
|
||||
" binary from https://essentia.upf.edu/"
|
||||
)
|
||||
except ABSubmitError:
|
||||
# Extractor found, will exit with an error if not called with
|
||||
@@ -92,36 +90,58 @@ class AcousticBrainzSubmitPlugin(plugins.BeetsPlugin):
|
||||
|
||||
# Calculate extractor hash.
|
||||
self.extractor_sha = hashlib.sha1()
|
||||
with open(self.extractor, 'rb') as extractor:
|
||||
with open(self.extractor, "rb") as extractor:
|
||||
self.extractor_sha.update(extractor.read())
|
||||
self.extractor_sha = self.extractor_sha.hexdigest()
|
||||
|
||||
base_url = 'https://acousticbrainz.org/api/v1/{mbid}/low-level'
|
||||
self.url = ""
|
||||
base_url = self.config["base_url"].as_str()
|
||||
if base_url:
|
||||
if not base_url.startswith("http"):
|
||||
raise ui.UserError(
|
||||
"AcousticBrainz server base URL must start "
|
||||
"with an HTTP scheme"
|
||||
)
|
||||
elif base_url[-1] != "/":
|
||||
base_url = base_url + "/"
|
||||
self.url = base_url + "{mbid}/low-level"
|
||||
|
||||
def commands(self):
|
||||
cmd = ui.Subcommand(
|
||||
'absubmit',
|
||||
help='calculate and submit AcousticBrainz analysis'
|
||||
"absubmit", help="calculate and submit AcousticBrainz analysis"
|
||||
)
|
||||
cmd.parser.add_option(
|
||||
'-f', '--force', dest='force_refetch',
|
||||
action='store_true', default=False,
|
||||
help='re-download data when already present'
|
||||
"-f",
|
||||
"--force",
|
||||
dest="force_refetch",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help="re-download data when already present",
|
||||
)
|
||||
cmd.parser.add_option(
|
||||
'-p', '--pretend', dest='pretend_fetch',
|
||||
action='store_true', default=False,
|
||||
help='pretend to perform action, but show \
|
||||
only files which would be processed'
|
||||
"-p",
|
||||
"--pretend",
|
||||
dest="pretend_fetch",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help="pretend to perform action, but show \
|
||||
only files which would be processed",
|
||||
)
|
||||
cmd.func = self.command
|
||||
return [cmd]
|
||||
|
||||
def command(self, lib, opts, args):
|
||||
# Get items from arguments
|
||||
items = lib.items(ui.decargs(args))
|
||||
self.opts = opts
|
||||
util.par_map(self.analyze_submit, items)
|
||||
if not self.url:
|
||||
raise ui.UserError(
|
||||
"This plugin is deprecated since AcousticBrainz no longer "
|
||||
"accepts new submissions. See the base_url configuration "
|
||||
"option."
|
||||
)
|
||||
else:
|
||||
# Get items from arguments
|
||||
items = lib.items(ui.decargs(args))
|
||||
self.opts = opts
|
||||
util.par_map(self.analyze_submit, items)
|
||||
|
||||
def analyze_submit(self, item):
|
||||
analysis = self._get_analysis(item)
|
||||
@@ -129,28 +149,29 @@ only files which would be processed'
|
||||
self._submit_data(item, analysis)
|
||||
|
||||
def _get_analysis(self, item):
|
||||
mbid = item['mb_trackid']
|
||||
mbid = item["mb_trackid"]
|
||||
|
||||
# Avoid re-analyzing files that already have AB data.
|
||||
if not self.opts.force_refetch and not self.config['force']:
|
||||
if not self.opts.force_refetch and not self.config["force"]:
|
||||
if item.get(PROBE_FIELD):
|
||||
return None
|
||||
|
||||
# If file has no MBID, skip it.
|
||||
if not mbid:
|
||||
self._log.info('Not analysing {}, missing '
|
||||
'musicbrainz track id.', item)
|
||||
self._log.info(
|
||||
"Not analysing {}, missing " "musicbrainz track id.", item
|
||||
)
|
||||
return None
|
||||
|
||||
if self.opts.pretend_fetch or self.config['pretend']:
|
||||
self._log.info('pretend action - extract item: {}', item)
|
||||
if self.opts.pretend_fetch or self.config["pretend"]:
|
||||
self._log.info("pretend action - extract item: {}", item)
|
||||
return None
|
||||
|
||||
# Temporary file to save extractor output to, extractor only works
|
||||
# if an output file is given. Here we use a temporary file to copy
|
||||
# the data into a python object and then remove the file from the
|
||||
# system.
|
||||
tmp_file, filename = tempfile.mkstemp(suffix='.json')
|
||||
tmp_file, filename = tempfile.mkstemp(suffix=".json")
|
||||
try:
|
||||
# Close the file, so the extractor can overwrite it.
|
||||
os.close(tmp_file)
|
||||
@@ -158,15 +179,17 @@ only files which would be processed'
|
||||
call([self.extractor, util.syspath(item.path), filename])
|
||||
except ABSubmitError as e:
|
||||
self._log.warning(
|
||||
'Failed to analyse {item} for AcousticBrainz: {error}',
|
||||
item=item, error=e
|
||||
"Failed to analyse {item} for AcousticBrainz: {error}",
|
||||
item=item,
|
||||
error=e,
|
||||
)
|
||||
return None
|
||||
with open(filename) as tmp_file:
|
||||
analysis = json.load(tmp_file)
|
||||
# Add the hash to the output.
|
||||
analysis['metadata']['version']['essentia_build_sha'] = \
|
||||
self.extractor_sha
|
||||
analysis["metadata"]["version"][
|
||||
"essentia_build_sha"
|
||||
] = self.extractor_sha
|
||||
return analysis
|
||||
finally:
|
||||
try:
|
||||
@@ -177,20 +200,28 @@ only files which would be processed'
|
||||
raise
|
||||
|
||||
def _submit_data(self, item, data):
|
||||
mbid = item['mb_trackid']
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
response = requests.post(self.base_url.format(mbid=mbid),
|
||||
json=data, headers=headers)
|
||||
mbid = item["mb_trackid"]
|
||||
headers = {"Content-Type": "application/json"}
|
||||
response = requests.post(
|
||||
self.url.format(mbid=mbid),
|
||||
json=data,
|
||||
headers=headers,
|
||||
timeout=10,
|
||||
)
|
||||
# Test that request was successful and raise an error on failure.
|
||||
if response.status_code != 200:
|
||||
try:
|
||||
message = response.json()['message']
|
||||
message = response.json()["message"]
|
||||
except (ValueError, KeyError) as e:
|
||||
message = f'unable to get error message: {e}'
|
||||
message = f"unable to get error message: {e}"
|
||||
self._log.error(
|
||||
'Failed to submit AcousticBrainz analysis of {item}: '
|
||||
'{message}).', item=item, message=message
|
||||
"Failed to submit AcousticBrainz analysis of {item}: "
|
||||
"{message}).",
|
||||
item=item,
|
||||
message=message,
|
||||
)
|
||||
else:
|
||||
self._log.debug('Successfully submitted AcousticBrainz analysis '
|
||||
'for {}.', item)
|
||||
self._log.debug(
|
||||
"Successfully submitted AcousticBrainz analysis " "for {}.",
|
||||
item,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user