From a60935ed1c69993c0739c41d6c0afb3febbd03c5 Mon Sep 17 00:00:00 2001 From: Rainer Hihn Date: Mon, 22 Apr 2019 19:50:11 +0200 Subject: [PATCH 01/10] Prevent scientific notation --- beetsplug/acousticbrainz.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index f4960c301..bbbd098c7 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -17,9 +17,10 @@ """ from __future__ import division, absolute_import, print_function +from collections import defaultdict + import requests -from collections import defaultdict from beets import plugins, ui ACOUSTIC_BASE = "https://acousticbrainz.org/" @@ -101,6 +102,10 @@ ABSCHEME = { } } +FLOAT_FIELDS = ['danceable', 'mood_acoustic', 'mood_aggressive', + 'mood_electronic', 'mood_happy', 'mood_party', 'mood_relaxed', + 'mood_sad', 'tonal', 'average_loudness', 'chords_changes_rate', + 'chords_number_rate', 'key_strength'] class AcousticPlugin(plugins.BeetsPlugin): @@ -186,6 +191,8 @@ class AcousticPlugin(plugins.BeetsPlugin): if data: for attr, val in self._map_data_to_scheme(data, ABSCHEME): if not tags or attr in tags: + if attr in FLOAT_FIELDS: + val = '%f' % val self._log.debug(u'attribute {} of {} set to {}', attr, item, From 76e333c054c298acce89740a438909dbc8ccb8c6 Mon Sep 17 00:00:00 2001 From: Rainer Hihn Date: Tue, 23 Apr 2019 19:20:55 +0200 Subject: [PATCH 02/10] Added item_types --- beetsplug/acousticbrainz.py | 49 +++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index bbbd098c7..3a6db7d4e 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -22,6 +22,7 @@ from collections import defaultdict import requests from beets import plugins, ui +from beets.dbcore import types ACOUSTIC_BASE = "https://acousticbrainz.org/" LEVELS = ["/low-level", "/high-level"] @@ -102,13 +103,32 @@ ABSCHEME = { } } -FLOAT_FIELDS = ['danceable', 'mood_acoustic', 'mood_aggressive', - 'mood_electronic', 'mood_happy', 'mood_party', 'mood_relaxed', - 'mood_sad', 'tonal', 'average_loudness', 'chords_changes_rate', - 'chords_number_rate', 'key_strength'] class AcousticPlugin(plugins.BeetsPlugin): + item_types = { + 'average_loudness': types.FLOAT, + 'chords_changes_rate': types.FLOAT, + 'chords_key': types.STRING, + 'chords_number_rate': types.FLOAT, + 'chords_scale': types.STRING, + 'danceable': types.FLOAT, + 'gender': types.STRING, + 'genre_rosamerica': types.STRING, + 'initial_key': types.STRING, + 'key_strength': types.FLOAT, + 'mood_acoustic': types.FLOAT, + 'mood_aggressive': types.FLOAT, + 'mood_electronic': types.FLOAT, + 'mood_happy': types.FLOAT, + 'mood_party': types.FLOAT, + 'mood_relaxed': types.FLOAT, + 'mood_sad': types.FLOAT, + 'rhythm': types.FLOAT, + 'tonal': types.FLOAT, + 'voice_instrumental': types.STRING, + } + def __init__(self): super(AcousticPlugin, self).__init__() @@ -156,7 +176,7 @@ class AcousticPlugin(plugins.BeetsPlugin): return {} if res.status_code == 404: - self._log.info(u'recording ID {} not found', mbid) + self._log.info(u'recording ID \'{}\' not found', mbid) return {} try: @@ -179,28 +199,27 @@ class AcousticPlugin(plugins.BeetsPlugin): 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: - if attr in FLOAT_FIELDS: - val = '%f' % val - 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) From 3f3b10288506de68c5a181f25c31fb8eb4ee7ee8 Mon Sep 17 00:00:00 2001 From: Rainer Hihn Date: Wed, 24 Apr 2019 19:53:01 +0200 Subject: [PATCH 03/10] added PaddingFloat --- beets/dbcore/types.py | 11 +++++++++++ beetsplug/acousticbrainz.py | 14 +++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/beets/dbcore/types.py b/beets/dbcore/types.py index 935d03870..bc923f747 100644 --- a/beets/dbcore/types.py +++ b/beets/dbcore/types.py @@ -183,6 +183,17 @@ class Float(Type): return u'{0:.1f}'.format(value or 0.0) +class PaddedFloat(Integer): + """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:0{1}d}'.format(value or 0, self.digits) + + class NullFloat(Float): """Same as `Float`, but does not normalize `None` to `0.0`. """ diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index 3a6db7d4e..9c1eb7ce5 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -117,13 +117,13 @@ class AcousticPlugin(plugins.BeetsPlugin): 'genre_rosamerica': types.STRING, 'initial_key': types.STRING, 'key_strength': types.FLOAT, - 'mood_acoustic': types.FLOAT, - 'mood_aggressive': types.FLOAT, - 'mood_electronic': types.FLOAT, - 'mood_happy': types.FLOAT, - 'mood_party': types.FLOAT, - 'mood_relaxed': types.FLOAT, - 'mood_sad': types.FLOAT, + 'mood_acoustic': types.PaddedInt(6), + 'mood_aggressive': types.PaddedInt(6), + 'mood_electronic': types.PaddedInt(6), + 'mood_happy': types.PaddedInt(6), + 'mood_party': types.PaddedInt(6), + 'mood_relaxed': types.PaddedInt(6), + 'mood_sad': types.PaddedInt(6), 'rhythm': types.FLOAT, 'tonal': types.FLOAT, 'voice_instrumental': types.STRING, From 7676d2ae5a427d4fa1dd03c45a99bace78b1ee2e Mon Sep 17 00:00:00 2001 From: Rainer Hihn Date: Wed, 24 Apr 2019 19:54:12 +0200 Subject: [PATCH 04/10] PaddingInt -> PaddingFloat --- beetsplug/acousticbrainz.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index 9c1eb7ce5..5278416b9 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -117,13 +117,13 @@ class AcousticPlugin(plugins.BeetsPlugin): 'genre_rosamerica': types.STRING, 'initial_key': types.STRING, 'key_strength': types.FLOAT, - 'mood_acoustic': types.PaddedInt(6), - 'mood_aggressive': types.PaddedInt(6), - 'mood_electronic': types.PaddedInt(6), - 'mood_happy': types.PaddedInt(6), - 'mood_party': types.PaddedInt(6), - 'mood_relaxed': types.PaddedInt(6), - 'mood_sad': types.PaddedInt(6), + 'mood_acoustic': types.PaddedFloat(6), + 'mood_aggressive': types.PaddedFloat(6), + 'mood_electronic': types.PaddedFloat(6), + 'mood_happy': types.PaddedFloat(6), + 'mood_party': types.PaddedFloat(6), + 'mood_relaxed': types.PaddedFloat(6), + 'mood_sad': types.PaddedFloat(6), 'rhythm': types.FLOAT, 'tonal': types.FLOAT, 'voice_instrumental': types.STRING, From 07b617b307344259f5f62e4de81e6f8c20e71a16 Mon Sep 17 00:00:00 2001 From: Rainer Hihn Date: Wed, 24 Apr 2019 19:55:44 +0200 Subject: [PATCH 05/10] Converted all float types to PaddedFloat --- beetsplug/acousticbrainz.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index 5278416b9..11a67eadd 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -107,16 +107,16 @@ ABSCHEME = { class AcousticPlugin(plugins.BeetsPlugin): item_types = { - 'average_loudness': types.FLOAT, - 'chords_changes_rate': types.FLOAT, + 'average_loudness': types.PaddedFloat(6), + 'chords_changes_rate': types.PaddedFloat(6), 'chords_key': types.STRING, - 'chords_number_rate': types.FLOAT, + 'chords_number_rate': types.PaddedFloat(6), 'chords_scale': types.STRING, - 'danceable': types.FLOAT, + 'danceable': types.PaddedFloat(6), 'gender': types.STRING, 'genre_rosamerica': types.STRING, 'initial_key': types.STRING, - 'key_strength': types.FLOAT, + 'key_strength': types.PaddedFloat(6), 'mood_acoustic': types.PaddedFloat(6), 'mood_aggressive': types.PaddedFloat(6), 'mood_electronic': types.PaddedFloat(6), @@ -124,8 +124,8 @@ class AcousticPlugin(plugins.BeetsPlugin): 'mood_party': types.PaddedFloat(6), 'mood_relaxed': types.PaddedFloat(6), 'mood_sad': types.PaddedFloat(6), - 'rhythm': types.FLOAT, - 'tonal': types.FLOAT, + 'rhythm': types.PaddedFloat(6), + 'tonal': types.PaddedFloat(6), 'voice_instrumental': types.STRING, } From 7240a826bc7e2a038501b4c5672442590cdc451f Mon Sep 17 00:00:00 2001 From: Rainer Hihn Date: Wed, 24 Apr 2019 20:05:26 +0200 Subject: [PATCH 06/10] Fixed PaddedFloat-type and set precision to 8 --- beets/dbcore/types.py | 4 ++-- beetsplug/acousticbrainz.py | 28 ++++++++++++++-------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/beets/dbcore/types.py b/beets/dbcore/types.py index bc923f747..fe83f68b2 100644 --- a/beets/dbcore/types.py +++ b/beets/dbcore/types.py @@ -183,7 +183,7 @@ class Float(Type): return u'{0:.1f}'.format(value or 0.0) -class PaddedFloat(Integer): +class PaddedFloat(Float): """A float field that is formatted with a given number of digits, padded with zeroes. """ @@ -191,7 +191,7 @@ class PaddedFloat(Integer): self.digits = digits def format(self, value): - return u'{0:0{1}d}'.format(value or 0, self.digits) + return u'{0:.{1}f}'.format(value or 0, self.digits) class NullFloat(Float): diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index 11a67eadd..cdd6a6b83 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -107,25 +107,25 @@ ABSCHEME = { class AcousticPlugin(plugins.BeetsPlugin): item_types = { - 'average_loudness': types.PaddedFloat(6), - 'chords_changes_rate': types.PaddedFloat(6), + 'average_loudness': types.PaddedFloat(8), + 'chords_changes_rate': types.PaddedFloat(8), 'chords_key': types.STRING, - 'chords_number_rate': types.PaddedFloat(6), + 'chords_number_rate': types.PaddedFloat(8), 'chords_scale': types.STRING, - 'danceable': types.PaddedFloat(6), + 'danceable': types.PaddedFloat(8), 'gender': types.STRING, 'genre_rosamerica': types.STRING, 'initial_key': types.STRING, - 'key_strength': types.PaddedFloat(6), - 'mood_acoustic': types.PaddedFloat(6), - 'mood_aggressive': types.PaddedFloat(6), - 'mood_electronic': types.PaddedFloat(6), - 'mood_happy': types.PaddedFloat(6), - 'mood_party': types.PaddedFloat(6), - 'mood_relaxed': types.PaddedFloat(6), - 'mood_sad': types.PaddedFloat(6), - 'rhythm': types.PaddedFloat(6), - 'tonal': types.PaddedFloat(6), + '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, } From 55fe077e549899743352f1528b17646034f41606 Mon Sep 17 00:00:00 2001 From: Rainer Hihn Date: Wed, 24 Apr 2019 20:39:05 +0200 Subject: [PATCH 07/10] Removed PaddedFloat in favour of adding a constructor parameter --- beets/dbcore/types.py | 14 +++----------- beetsplug/acousticbrainz.py | 28 ++++++++++++++-------------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/beets/dbcore/types.py b/beets/dbcore/types.py index bc923f747..2a06c58fc 100644 --- a/beets/dbcore/types.py +++ b/beets/dbcore/types.py @@ -173,25 +173,17 @@ class Id(Integer): class Float(Type): - """A basic floating-point type. + """A basic floating-point type. Supports padding. """ sql = u'REAL' query = query.NumericQuery model_type = float - def format(self, value): - return u'{0:.1f}'.format(value or 0.0) - - -class PaddedFloat(Integer): - """A float field that is formatted with a given number of digits, - padded with zeroes. - """ - def __init__(self, digits): + def __init__(self, digits=1): self.digits = digits def format(self, value): - return u'{0:0{1}d}'.format(value or 0, self.digits) + return u'{0:0{1}f}'.format(value or 0, self.digits) class NullFloat(Float): diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index 11a67eadd..4c4315b80 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -107,25 +107,25 @@ ABSCHEME = { class AcousticPlugin(plugins.BeetsPlugin): item_types = { - 'average_loudness': types.PaddedFloat(6), - 'chords_changes_rate': types.PaddedFloat(6), + 'average_loudness': types.Float(6), + 'chords_changes_rate': types.Float(6), 'chords_key': types.STRING, - 'chords_number_rate': types.PaddedFloat(6), + 'chords_number_rate': types.Float(6), 'chords_scale': types.STRING, - 'danceable': types.PaddedFloat(6), + 'danceable': types.Float(6), 'gender': types.STRING, 'genre_rosamerica': types.STRING, 'initial_key': types.STRING, - 'key_strength': types.PaddedFloat(6), - 'mood_acoustic': types.PaddedFloat(6), - 'mood_aggressive': types.PaddedFloat(6), - 'mood_electronic': types.PaddedFloat(6), - 'mood_happy': types.PaddedFloat(6), - 'mood_party': types.PaddedFloat(6), - 'mood_relaxed': types.PaddedFloat(6), - 'mood_sad': types.PaddedFloat(6), - 'rhythm': types.PaddedFloat(6), - 'tonal': types.PaddedFloat(6), + 'key_strength': types.Float(6), + 'mood_acoustic': types.Float(6), + 'mood_aggressive': types.Float(6), + 'mood_electronic': types.Float(6), + 'mood_happy': types.Float(6), + 'mood_party': types.Float(6), + 'mood_relaxed': types.Float(6), + 'mood_sad': types.Float(6), + 'rhythm': types.Float(6), + 'tonal': types.Float(6), 'voice_instrumental': types.STRING, } From 36dc105dc9e9ac859b64fd4fabc6224462e4e539 Mon Sep 17 00:00:00 2001 From: Rainer Hihn Date: Sat, 27 Apr 2019 17:57:48 +0200 Subject: [PATCH 08/10] undid quotes in log messages --- beetsplug/acousticbrainz.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index 4c4315b80..8075788aa 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -176,7 +176,7 @@ class AcousticPlugin(plugins.BeetsPlugin): return {} if res.status_code == 404: - self._log.info(u'recording ID \'{}\' not found', mbid) + self._log.info(u'recording ID {} not found', mbid) return {} try: @@ -199,27 +199,27 @@ class AcousticPlugin(plugins.BeetsPlugin): 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 \'{}\'', + 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) From 941dd6e48f989ab1da2773f1a71e7bab5b34a70e Mon Sep 17 00:00:00 2001 From: Rainer Hihn Date: Sat, 27 Apr 2019 17:58:26 +0200 Subject: [PATCH 09/10] Formatting --- beetsplug/acousticbrainz.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index 8075788aa..02be2d7e8 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -211,8 +211,7 @@ class AcousticPlugin(plugins.BeetsPlugin): 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 {}', + self._log.debug(u'attribute {} of {} set to {}', attr, item, val) From 62c1d37bcc44e86341388e595bdc5c2ed81a8d57 Mon Sep 17 00:00:00 2001 From: Rainer Hihn Date: Sat, 27 Apr 2019 17:58:48 +0200 Subject: [PATCH 10/10] Formatting --- beetsplug/acousticbrainz.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index 02be2d7e8..01f3ac6ac 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -212,9 +212,9 @@ class AcousticPlugin(plugins.BeetsPlugin): 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) + attr, + item, + val) setattr(item, attr, val) else: self._log.debug(u'skipping attribute {} of {}'