Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #2790 - acousticbrainz: Really small float values are stored as strings #3238

Merged
merged 11 commits into from
Apr 30, 2019
11 changes: 11 additions & 0 deletions beets/dbcore/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,17 @@ def format(self, value):
return u'{0:.1f}'.format(value or 0.0)


class PaddedFloat(Float):
rain0r marked this conversation as resolved.
Show resolved Hide resolved
"""A float field that is formatted with a given number of digits,
padded with zeroes.
"""
def __init__(self, digits):
self.digits = digits

def format(self, value):
return u'{0:.{1}f}'.format(value or 0, self.digits)


class NullFloat(Float):
"""Same as `Float`, but does not normalize `None` to `0.0`.
"""
Expand Down
46 changes: 36 additions & 10 deletions beetsplug/acousticbrainz.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
"""
from __future__ import division, absolute_import, print_function

from collections import defaultdict

import requests

from collections import defaultdict
from beets import plugins, ui
from beets.dbcore import types

ACOUSTIC_BASE = "https://acousticbrainz.org/"
LEVELS = ["/low-level", "/high-level"]
Expand Down Expand Up @@ -104,6 +106,29 @@


class AcousticPlugin(plugins.BeetsPlugin):
item_types = {
'average_loudness': types.PaddedFloat(8),
'chords_changes_rate': types.PaddedFloat(8),
'chords_key': types.STRING,
'chords_number_rate': types.PaddedFloat(8),
'chords_scale': types.STRING,
'danceable': types.PaddedFloat(8),
'gender': types.STRING,
'genre_rosamerica': types.STRING,
'initial_key': types.STRING,
'key_strength': types.PaddedFloat(8),
'mood_acoustic': types.PaddedFloat(8),
'mood_aggressive': types.PaddedFloat(8),
'mood_electronic': types.PaddedFloat(8),
'mood_happy': types.PaddedFloat(8),
'mood_party': types.PaddedFloat(8),
'mood_relaxed': types.PaddedFloat(8),
'mood_sad': types.PaddedFloat(8),
'rhythm': types.PaddedFloat(8),
'tonal': types.PaddedFloat(8),
'voice_instrumental': types.STRING,
}

def __init__(self):
super(AcousticPlugin, self).__init__()

Expand Down Expand Up @@ -151,7 +176,7 @@ def _get_data(self, mbid):
return {}

if res.status_code == 404:
self._log.info(u'recording ID {} not found', mbid)
self._log.info(u'recording ID \'{}\' not found', mbid)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason to add these single quotes in these log statements? For the sake of the cleanliness of this PR, it would be awesome to leave that to a later change if you don't mind—and keep this change focused on just the fix you have here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this while debugging ... the output was easier to read for me this way.

Example:

acousticbrainz: attribute 'gender' of 'M83 -  - Ludivine' set to 'male'

But, anyway, you are correct. I can revert it if you like.

Copy link
Member

@jackwilsdon jackwilsdon Apr 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably best to revert this (and the other log statements too) if not just because it adds unrelated changes/noise to this PR.

Copy link
Contributor Author

@rain0r rain0r Apr 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted them.
@sampsyo

return {}

try:
Expand All @@ -174,26 +199,27 @@ def _fetch_info(self, items, write, force):
if not force:
mood_str = item.get('mood_acoustic', u'')
if mood_str:
self._log.info(u'data already present for: {}', item)
self._log.info(u'data already present for: \'{}\'', item)
continue

# We can only fetch data for tracks with MBIDs.
if not item.mb_trackid:
continue

self._log.info(u'getting data for: {}', item)
self._log.info(u'getting data for: \'{}\'', item)
data = self._get_data(item.mb_trackid)
if data:
for attr, val in self._map_data_to_scheme(data, ABSCHEME):
if not tags or attr in tags:
self._log.debug(u'attribute {} of {} set to {}',
attr,
item,
val)
self._log.debug(
u'attribute \'{}\' of \'{}\' set to \'{}\'',
attr,
item,
val)
setattr(item, attr, val)
else:
self._log.debug(u'skipping attribute {} of {}'
u' (value {}) due to config',
self._log.debug(u'skipping attribute \'{}\' of \'{}\''
u' (value \'{}\') due to config',
attr,
item,
val)
Expand Down