Skip to content

Commit

Permalink
Merge pull request #5 from adamjakab/devel
Browse files Browse the repository at this point in the history
Hotfix: invalid json from extractor
  • Loading branch information
adamjakab committed Mar 26, 2020
2 parents 13271e5 + 0ce3b76 commit f261742
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 37 deletions.
11 changes: 7 additions & 4 deletions beetsplug/xtractor/command.py
Expand Up @@ -3,6 +3,7 @@
# Author: Adam Jakab <adam at jakab dot pro>
# Created: 3/13/20, 12:17 AM
# License: See LICENSE.txt

import hashlib
import json
import logging
Expand All @@ -14,12 +15,11 @@

import yaml
from beets import dbcore
from beets.dbcore import types
from beets.library import Library, Item, parse_query_string
from beets.ui import Subcommand, decargs
from confuse import Subview

from beetsplug.xtractor import helper as bpmHelper
from beetsplug.xtractor import helper

# Module methods
log = logging.getLogger('beets.xtractor')
Expand Down Expand Up @@ -225,7 +225,7 @@ def _run_analysis_high_level(self, item):

try:
target_map = self.config["high_level_targets"]
audiodata = bpmHelper.extract_from_output(output_path, target_map)
audiodata = helper.extract_from_output(output_path, target_map)
log.debug("Audiodata(High): {}".format(audiodata))
except FileNotFoundError as e:
self._say("File not found: {0}".format(e))
Expand Down Expand Up @@ -258,7 +258,7 @@ def _run_analysis_low_level(self, item):

try:
target_map = self.config["low_level_targets"]
audiodata = bpmHelper.extract_from_output(output_path, target_map)
audiodata = helper.extract_from_output(output_path, target_map)
log.debug("Audiodata(Low): {}".format(audiodata))
except FileNotFoundError as e:
self._say("File not found: {0}".format(e))
Expand Down Expand Up @@ -287,6 +287,9 @@ def _run_essentia_extractor(self, extractor_path, input_path, output_path, profi
log.debug("Process stdout: {0}".format(stdout.decode()))
log.debug("Process stderr: {0}".format(stderr.decode()))

# Make sure file is encoded correctly (sometimes media files have funky tags)
helper.asciify_file_content(output_path)

def _execute_on_each_items(self, items, func):
total = len(items)
finished = 0
Expand Down
44 changes: 12 additions & 32 deletions beetsplug/xtractor/helper.py
Expand Up @@ -7,38 +7,7 @@
import json
import os

from confuse import Subview

_module_path = os.path.dirname(__file__)

"""Checklist from Acousticbrainz plugin:
ITEM
bpm OK
initial_key X ???
ATTRIBUTE:
average_loudness OK
beets_count OK (extra)
chords_changes_rate X
chords_key X
chords_number_rate X
chords_scale X
danceable OK
danceability OK (extra)
gender OK (!!!)
genre_rosamerica OK
key_strength X
mood_acoustic OK
mood_aggressive OK
mood_electronic OK
mood_happy OK
mood_party OK
mood_relaxed OK
mood_sad OK
rhythm X
tonal X
voice_instrumental OK
"""
from beets.util.confit import Subview


def extract_from_output(output_path, target_map: Subview):
Expand Down Expand Up @@ -81,3 +50,14 @@ def extract_value_from_audiodata(audiodata, target_map_item: Subview):
value = audiodata

return value


def asciify_file_content(file_path):
if os.path.isfile(file_path):
with open(file_path, 'r', encoding="utf-8") as content_file:
content_orig = content_file.read()

content_enc = content_orig.encode('ascii', 'ignore').decode('ascii')
if content_orig != content_enc:
with open(file_path, 'w', encoding="ascii") as content_file:
content_file.write(content_enc)
2 changes: 1 addition & 1 deletion beetsplug/xtractor/version.py
Expand Up @@ -4,4 +4,4 @@
# Created: 3/13/20, 12:17 AM
# License: See LICENSE.txt

__version__ = '0.2.1'
__version__ = '0.2.2'

0 comments on commit f261742

Please sign in to comment.