From 26679f891e24b85c8d2b7de2132f66a6d49691c4 Mon Sep 17 00:00:00 2001 From: Ohm Patel Date: Fri, 1 Jan 2016 01:01:14 -0600 Subject: [PATCH 01/12] ABrainz: Added acousticbrainz to autotagger and updated docs --- beetsplug/acousticbrainz.py | 12 ++++++++++++ docs/plugins/acousticbrainz.rst | 18 +++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index 934531a3c..8f7bc6f5a 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -30,6 +30,11 @@ class AcousticPlugin(plugins.BeetsPlugin): def __init__(self): super(AcousticPlugin, self).__init__() + self.config.add({'auto': True}) + if self.config['auto']: + self.register_listener('import_task_files', + self.import_task_files) + def commands(self): cmd = ui.Subcommand('acousticbrainz', help="fetch metadata from AcousticBrainz") @@ -41,6 +46,13 @@ class AcousticPlugin(plugins.BeetsPlugin): cmd.func = func return [cmd] + def import_task_files(self, session, task): + """Automatically tag imported files + """ + + items = task.imported_items() + fetch_info(self._log, items) + def fetch_info(log, items): """Currently outputs MBID and corresponding request status code diff --git a/docs/plugins/acousticbrainz.rst b/docs/plugins/acousticbrainz.rst index 8e15716a5..3cb0717e2 100644 --- a/docs/plugins/acousticbrainz.rst +++ b/docs/plugins/acousticbrainz.rst @@ -19,4 +19,20 @@ these fields: * ``mood_party``: Predicts the probability this track should be played at a party. -These three fields are all numbers between 0.0 and 1.0. +Automatic Tagging +----------------- + +To automatically tag files using AcousticBrainz data during import, just +enable the ``acousticbrainz`` plugin (see :ref:`using-plugins`). When importing +new files (with ``import.write`` turned on) or modifying files' tags with the +``beet modify`` command, beets will query the AcousticBrainz API using MBID and +set the appropriate metadata. + +Configuration +------------- + +To configure the plugin, make a ``acousticbrainz:`` section in your +configuration file. There is one option: + +- **auto**: Enable AcousticBrainz import during import. + Default: ``yes``. From 82709f1c1b1069b6aaba11170eefc54347113085 Mon Sep 17 00:00:00 2001 From: Ohm Patel Date: Fri, 1 Jan 2016 01:28:17 -0600 Subject: [PATCH 02/12] ABrainz: Added more metadata for plugin to fetch and updated docs --- beetsplug/acousticbrainz.py | 52 ++++++++++++++++++++++++++++++++- docs/plugins/acousticbrainz.rst | 17 ++++++++--- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index 8f7bc6f5a..c75849b12 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -87,6 +87,31 @@ def fetch_info(log, items): data, ["highlevel", "danceability", "all", "danceable"], ) + item.gender = get_value( + log, + data, + ["highlevel", "gender", "value"], + ) + item.genre_rosamerica = get_value( + log, + data, + ["highlevel", "genre_rosamerica", "value"], + ) + item.mood_acoustic = get_value( + log, + data, + ["highlevel", "mood_acoustic", "all", "acoustic"], + ) + item.mood_aggressive = get_value( + log, + data, + ["highlevel", "mood_aggresive", "all", "aggresive"], + ) + item.mood_electronic = get_value( + log, + data, + ["highlevel", "mood_electronic", "all", "electronic"], + ) item.mood_happy = get_value( log, data, @@ -97,7 +122,32 @@ def fetch_info(log, items): data, ["highlevel", "mood_party", "all", "party"], ) - + item.mood_relaxed = get_value( + log, + data, + ["highlevel", "mood_relaxed", "all", "relaxed"], + ) + item.mood_sad = get_value( + log, + data, + ["highlevel", "mood_sad", "all", "sad"], + ) + item.rhythm = get_value( + log, + data + ["highlevel", "ismir04_rhythm", "value"], + ) + item.tonal = get_value( + log, + data, + ["highlevel", "tonal_atonal", "all", "tonal"], + ) + item.voice_instrumental = get_value( + log, + data, + ["highlevel", "voice_instrumental", "value"], + ) + # Store the data. We only update flexible attributes, so we # don't call `item.try_write()` here. item.store() diff --git a/docs/plugins/acousticbrainz.rst b/docs/plugins/acousticbrainz.rst index 3cb0717e2..b5f07d884 100644 --- a/docs/plugins/acousticbrainz.rst +++ b/docs/plugins/acousticbrainz.rst @@ -14,10 +14,19 @@ Enable the ``acousticbrainz`` plugin in your configuration (see :ref:`using-plug For all tracks with a MusicBrainz recording ID, the plugin currently sets these fields: -* ``danceable``: Predicts how easy the track is to dance to. -* ``mood_happy``: Predicts the probability this track will evoke happiness. -* ``mood_party``: Predicts the probability this track should be played at a - party. +* ``danceable`` +* ``gender`` +* ``genre_rosamerica`` +* ``mood_acoustic`` +* ``mood_aggressive`` +* ``mood_electronic`` +* ``mood_happy`` +* ``mood_party`` +* ``mood_relaxed`` +* ``mood_sad`` +* ``rhythm`` +* ``tonal`` +* ``voice_instrumental`` Automatic Tagging ----------------- From 52de2380768f9a5a35f40f535943949150d9fb39 Mon Sep 17 00:00:00 2001 From: Ohm Patel Date: Fri, 1 Jan 2016 01:32:42 -0600 Subject: [PATCH 03/12] ABrainz: Removed PEP8 Whitespace violation --- beetsplug/acousticbrainz.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index c75849b12..8ec719383 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -147,7 +147,7 @@ def fetch_info(log, items): data, ["highlevel", "voice_instrumental", "value"], ) - + # Store the data. We only update flexible attributes, so we # don't call `item.try_write()` here. item.store() From c5f25429e962f67c2150fa5483147ac472002cf7 Mon Sep 17 00:00:00 2001 From: Ohm Patel Date: Fri, 1 Jan 2016 18:29:36 -0600 Subject: [PATCH 04/12] ABrainz: Fixed docstrings and documentation --- beetsplug/acousticbrainz.py | 10 +++++----- docs/plugins/acousticbrainz.rst | 5 ++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index 8ec719383..f398fe8f2 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -13,7 +13,7 @@ # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. -""" Fetch various AcousticBrainz metadata using MBID +"""Fetch various AcousticBrainz metadata using MBID. """ from __future__ import (division, absolute_import, print_function, unicode_literals) @@ -47,7 +47,7 @@ class AcousticPlugin(plugins.BeetsPlugin): return [cmd] def import_task_files(self, session, task): - """Automatically tag imported files + """Function is called upon beet import. """ items = task.imported_items() @@ -55,7 +55,7 @@ class AcousticPlugin(plugins.BeetsPlugin): def fetch_info(log, items): - """Currently outputs MBID and corresponding request status code + """Currently outputs MBID and corresponding request status code. """ for item in items: if item.mb_trackid: @@ -154,13 +154,13 @@ def fetch_info(log, items): def generate_url(mbid): - """Generates url of AcousticBrainz end point for given MBID + """Generates AcousticBrainz end point url for given MBID. """ return ACOUSTIC_URL + mbid + LEVEL def get_value(log, data, map_path): - """Allows traversal of dictionary with cleaner formatting + """Allows easier traversal of dictionary. """ try: return reduce(lambda d, k: d[k], map_path, data) diff --git a/docs/plugins/acousticbrainz.rst b/docs/plugins/acousticbrainz.rst index b5f07d884..24938473f 100644 --- a/docs/plugins/acousticbrainz.rst +++ b/docs/plugins/acousticbrainz.rst @@ -33,8 +33,7 @@ Automatic Tagging To automatically tag files using AcousticBrainz data during import, just enable the ``acousticbrainz`` plugin (see :ref:`using-plugins`). When importing -new files (with ``import.write`` turned on) or modifying files' tags with the -``beet modify`` command, beets will query the AcousticBrainz API using MBID and +new files, beets will query the AcousticBrainz API using MBID and set the appropriate metadata. Configuration @@ -43,5 +42,5 @@ Configuration To configure the plugin, make a ``acousticbrainz:`` section in your configuration file. There is one option: -- **auto**: Enable AcousticBrainz import during import. +- **auto**: Enable AcousticBrainz during ``beet import``. Default: ``yes``. From 17dd7496a2cb12cbff0591ac409df5331ef84e85 Mon Sep 17 00:00:00 2001 From: Ohm Patel Date: Sat, 2 Jan 2016 11:33:19 -0600 Subject: [PATCH 05/12] ABrainz: Added low-level data and updated docs --- beetsplug/acousticbrainz.py | 72 ++++++++++++++++++++++----------- docs/plugins/acousticbrainz.rst | 4 ++ 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index f398fe8f2..652f86ce7 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -22,8 +22,7 @@ import requests from beets import plugins, ui -ACOUSTIC_URL = "http://acousticbrainz.org/" -LEVEL = "/high-level" +ACOUSTIC_BASE = "http://acousticbrainz.org/" class AcousticPlugin(plugins.BeetsPlugin): @@ -62,101 +61,128 @@ def fetch_info(log, items): log.info('getting data for: {}', item) # Fetch the data from the AB API. - url = generate_url(item.mb_trackid) - log.debug('fetching URL: {}', url) + high_url = generate_url(item.mb_trackid, "/high-level") + low_url = generate_url(item.mb_trackid, "/low-level") + log.debug('fetching URL: {}', high_url) + log.debug('fetching URL: {}', low_url) try: - rs = requests.get(url) + high = requests.get(high_url) + low = requests.get(low_url) except requests.RequestException as exc: log.info('request error: {}', exc) continue # Check for missing tracks. - if rs.status_code == 404: + if high.status_code == 404 or low.status_code == 404: log.info('recording ID {} not found', item.mb_trackid) continue # Parse the JSON response. try: - data = rs.json() + high_data = high.json() except ValueError: - log.debug('Invalid Response: {}', rs.text) + log.debug('Invalid Response: {}', high.text) + try: + low_data = low.json() + except ValueError: + log.debug('Invalid Response: {}', low.text) # Get each field and assign it on the item. item.danceable = get_value( log, - data, + high_data, ["highlevel", "danceability", "all", "danceable"], ) item.gender = get_value( log, - data, + high_data, ["highlevel", "gender", "value"], ) item.genre_rosamerica = get_value( log, - data, + high_data, ["highlevel", "genre_rosamerica", "value"], ) item.mood_acoustic = get_value( log, - data, + high_data, ["highlevel", "mood_acoustic", "all", "acoustic"], ) item.mood_aggressive = get_value( log, - data, + high_data, ["highlevel", "mood_aggresive", "all", "aggresive"], ) item.mood_electronic = get_value( log, - data, + high_data, ["highlevel", "mood_electronic", "all", "electronic"], ) item.mood_happy = get_value( log, - data, + high_data, ["highlevel", "mood_happy", "all", "happy"], ) item.mood_party = get_value( log, - data, + high_data, ["highlevel", "mood_party", "all", "party"], ) item.mood_relaxed = get_value( log, - data, + high_data, ["highlevel", "mood_relaxed", "all", "relaxed"], ) item.mood_sad = get_value( log, - data, + high_data, ["highlevel", "mood_sad", "all", "sad"], ) item.rhythm = get_value( log, - data + high_data, ["highlevel", "ismir04_rhythm", "value"], ) item.tonal = get_value( log, - data, + high_data, ["highlevel", "tonal_atonal", "all", "tonal"], ) item.voice_instrumental = get_value( log, - data, + high_data, ["highlevel", "voice_instrumental", "value"], ) + item.average_loudness = get_value( + log, + low_data, + ["lowlevel", "average_loudness"], + ) + item.key_key = get_value( + log, + low_data, + ["tonal", "key_key"], + ) + item.key_scale = get_value( + log, + low_data, + ["tonal", "key_scale"], + ) + item.key_strength = get_value( + log, + low_data, + ["tonal", "key_stength"], + ) # Store the data. We only update flexible attributes, so we # don't call `item.try_write()` here. item.store() -def generate_url(mbid): +def generate_url(mbid, level): """Generates AcousticBrainz end point url for given MBID. """ - return ACOUSTIC_URL + mbid + LEVEL + return ACOUSTIC_BASE + mbid + level def get_value(log, data, map_path): diff --git a/docs/plugins/acousticbrainz.rst b/docs/plugins/acousticbrainz.rst index 24938473f..dcf35b493 100644 --- a/docs/plugins/acousticbrainz.rst +++ b/docs/plugins/acousticbrainz.rst @@ -14,9 +14,13 @@ Enable the ``acousticbrainz`` plugin in your configuration (see :ref:`using-plug For all tracks with a MusicBrainz recording ID, the plugin currently sets these fields: +* ``average_loudness``* * ``danceable`` * ``gender`` * ``genre_rosamerica`` +* ``key_key`` +* ``key_scale`` +* ``key_strength`` * ``mood_acoustic`` * ``mood_aggressive`` * ``mood_electronic`` From 3d64679be21517302b234bee408ec750f8b7520b Mon Sep 17 00:00:00 2001 From: Ohm Patel Date: Sat, 2 Jan 2016 11:38:54 -0600 Subject: [PATCH 06/12] ABrainz: Corrected docs error --- docs/plugins/acousticbrainz.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/plugins/acousticbrainz.rst b/docs/plugins/acousticbrainz.rst index dcf35b493..ddd29e7d8 100644 --- a/docs/plugins/acousticbrainz.rst +++ b/docs/plugins/acousticbrainz.rst @@ -14,7 +14,7 @@ Enable the ``acousticbrainz`` plugin in your configuration (see :ref:`using-plug For all tracks with a MusicBrainz recording ID, the plugin currently sets these fields: -* ``average_loudness``* +* ``average_loudness`` * ``danceable`` * ``gender`` * ``genre_rosamerica`` From 4184f9dd13dec511361d9aa089f36e7e78770b48 Mon Sep 17 00:00:00 2001 From: Ohm Patel Date: Sat, 2 Jan 2016 12:03:07 -0600 Subject: [PATCH 07/12] ABrainz: Chords metadata from low_level --- beetsplug/acousticbrainz.py | 20 ++++++++++++++++++++ docs/plugins/acousticbrainz.rst | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index 652f86ce7..3d03feaae 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -158,6 +158,26 @@ def fetch_info(log, items): low_data, ["lowlevel", "average_loudness"], ) + item.chords_changes_rate = get_value( + log, + low_data, + ["tonal", "chords_changes_rate"], + ) + item.chords_key = get_value( + log, + low_data, + ["tonal", "chords_key"], + ) + item.chords_number_rate = get_value( + log, + low_data, + ["tonal", "chords_number_rate"], + ) + item.chords_scale = get_value( + log, + low_data, + ["tonal", "chords_scale"], + ) item.key_key = get_value( log, low_data, diff --git a/docs/plugins/acousticbrainz.rst b/docs/plugins/acousticbrainz.rst index ddd29e7d8..24232437a 100644 --- a/docs/plugins/acousticbrainz.rst +++ b/docs/plugins/acousticbrainz.rst @@ -15,6 +15,10 @@ For all tracks with a MusicBrainz recording ID, the plugin currently sets these fields: * ``average_loudness`` +* ``chords_changes_rate`` +* ``chords_key`` +* ``chords_number_rate`` +* ``chords_scale`` * ``danceable`` * ``gender`` * ``genre_rosamerica`` From 1bb55c5c13e1994ad77c0ffaf218b04887d58d35 Mon Sep 17 00:00:00 2001 From: Ohm Patel Date: Sat, 2 Jan 2016 14:55:34 -0600 Subject: [PATCH 08/12] ABrainz: Used existing initial_key variable --- beets/library.py | 1 + beetsplug/acousticbrainz.py | 19 ++++++------------- docs/plugins/acousticbrainz.rst | 3 +-- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/beets/library.py b/beets/library.py index 474279b30..b42dd2c47 100644 --- a/beets/library.py +++ b/beets/library.py @@ -186,6 +186,7 @@ class MusicalKey(types.String): for flat, sharp in self.ENHARMONIC.items(): key = re.sub(flat, sharp, key) key = re.sub(r'[\W\s]+minor', 'm', key) + key = re.sub(r'[\W\s]+major', '', key) return key.capitalize() def normalize(self, key): diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index 3d03feaae..79bce75c3 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -63,8 +63,7 @@ def fetch_info(log, items): # Fetch the data from the AB API. high_url = generate_url(item.mb_trackid, "/high-level") low_url = generate_url(item.mb_trackid, "/low-level") - log.debug('fetching URL: {}', high_url) - log.debug('fetching URL: {}', low_url) + log.debug('fetching URLs: {} and {}', high_url, low_url) try: high = requests.get(high_url) low = requests.get(low_url) @@ -81,11 +80,11 @@ def fetch_info(log, items): try: high_data = high.json() except ValueError: - log.debug('Invalid Response: {}', high.text) + log.debug('Invalid Response from high-level: {}', high.text) try: low_data = low.json() except ValueError: - log.debug('Invalid Response: {}', low.text) + log.debug('Invalid Response from low-level: {}', low.text) # Get each field and assign it on the item. item.danceable = get_value( @@ -178,15 +177,9 @@ def fetch_info(log, items): low_data, ["tonal", "chords_scale"], ) - item.key_key = get_value( - log, - low_data, - ["tonal", "key_key"], - ) - item.key_scale = get_value( - log, - low_data, - ["tonal", "key_scale"], + item.initial_key = '{} {}'.format( + get_value(log, low_data, ["tonal", "key_key"]), + get_value(log, low_data, ["tonal", "key_scale"]) ) item.key_strength = get_value( log, diff --git a/docs/plugins/acousticbrainz.rst b/docs/plugins/acousticbrainz.rst index 24232437a..d782d3110 100644 --- a/docs/plugins/acousticbrainz.rst +++ b/docs/plugins/acousticbrainz.rst @@ -22,8 +22,7 @@ these fields: * ``danceable`` * ``gender`` * ``genre_rosamerica`` -* ``key_key`` -* ``key_scale`` +* ``initial_key`` * ``key_strength`` * ``mood_acoustic`` * ``mood_aggressive`` From 39b565d8d87d8145436a49c41b6fc318b5b91424 Mon Sep 17 00:00:00 2001 From: Ohm Patel Date: Sat, 2 Jan 2016 18:52:28 -0600 Subject: [PATCH 09/12] Refactor JesseW's suggestions --- beetsplug/acousticbrainz.py | 74 +++++++------------------------------ 1 file changed, 14 insertions(+), 60 deletions(-) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index 79bce75c3..c4b169577 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -56,134 +56,97 @@ class AcousticPlugin(plugins.BeetsPlugin): def fetch_info(log, items): """Currently outputs MBID and corresponding request status code. """ + def get_value(map_path): + try: + return reduce(lambda d, k: d[k], map_path, data) + except KeyError: + log.debug('Invalid Path: {}', map_path) + for item in items: if item.mb_trackid: log.info('getting data for: {}', item) # Fetch the data from the AB API. - high_url = generate_url(item.mb_trackid, "/high-level") - low_url = generate_url(item.mb_trackid, "/low-level") - log.debug('fetching URLs: {} and {}', high_url, low_url) + urls = [generate_url(item.mb_trackid, path) for path in ["/low-level", "/high-level"]] + log.debug('fetching URLs: {}', urls) try: - high = requests.get(high_url) - low = requests.get(low_url) + responses = [requests.get(url) for url in urls] except requests.RequestException as exc: log.info('request error: {}', exc) continue # Check for missing tracks. - if high.status_code == 404 or low.status_code == 404: + if any(r.status_code == 404 for r in responses): log.info('recording ID {} not found', item.mb_trackid) continue # Parse the JSON response. try: - high_data = high.json() - except ValueError: - log.debug('Invalid Response from high-level: {}', high.text) - try: - low_data = low.json() + data = responses[0].json() + data.update(responses[1].json()) except ValueError: log.debug('Invalid Response from low-level: {}', low.text) # Get each field and assign it on the item. item.danceable = get_value( - log, - high_data, ["highlevel", "danceability", "all", "danceable"], ) item.gender = get_value( - log, - high_data, ["highlevel", "gender", "value"], ) item.genre_rosamerica = get_value( - log, - high_data, ["highlevel", "genre_rosamerica", "value"], ) item.mood_acoustic = get_value( - log, - high_data, ["highlevel", "mood_acoustic", "all", "acoustic"], ) item.mood_aggressive = get_value( - log, - high_data, ["highlevel", "mood_aggresive", "all", "aggresive"], ) item.mood_electronic = get_value( - log, - high_data, ["highlevel", "mood_electronic", "all", "electronic"], ) item.mood_happy = get_value( - log, - high_data, ["highlevel", "mood_happy", "all", "happy"], ) item.mood_party = get_value( - log, - high_data, ["highlevel", "mood_party", "all", "party"], ) item.mood_relaxed = get_value( - log, - high_data, ["highlevel", "mood_relaxed", "all", "relaxed"], ) item.mood_sad = get_value( - log, - high_data, ["highlevel", "mood_sad", "all", "sad"], ) item.rhythm = get_value( - log, - high_data, ["highlevel", "ismir04_rhythm", "value"], ) item.tonal = get_value( - log, - high_data, ["highlevel", "tonal_atonal", "all", "tonal"], ) item.voice_instrumental = get_value( - log, - high_data, ["highlevel", "voice_instrumental", "value"], ) item.average_loudness = get_value( - log, - low_data, ["lowlevel", "average_loudness"], ) item.chords_changes_rate = get_value( - log, - low_data, ["tonal", "chords_changes_rate"], ) item.chords_key = get_value( - log, - low_data, ["tonal", "chords_key"], ) item.chords_number_rate = get_value( - log, - low_data, ["tonal", "chords_number_rate"], ) item.chords_scale = get_value( - log, - low_data, ["tonal", "chords_scale"], ) item.initial_key = '{} {}'.format( - get_value(log, low_data, ["tonal", "key_key"]), - get_value(log, low_data, ["tonal", "key_scale"]) + get_value(["tonal", "key_key"]), + get_value(["tonal", "key_scale"]) ) item.key_strength = get_value( - log, - low_data, ["tonal", "key_stength"], ) @@ -196,12 +159,3 @@ def generate_url(mbid, level): """Generates AcousticBrainz end point url for given MBID. """ return ACOUSTIC_BASE + mbid + level - - -def get_value(log, data, map_path): - """Allows easier traversal of dictionary. - """ - try: - return reduce(lambda d, k: d[k], map_path, data) - except KeyError: - log.debug('Invalid Path: {}', map_path) From e5c46cf6ac7a2048520bf823bb36041071f7b27a Mon Sep 17 00:00:00 2001 From: Ohm Patel Date: Sat, 2 Jan 2016 18:59:47 -0600 Subject: [PATCH 10/12] PEP-8 Cleanup --- beetsplug/acousticbrainz.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index c4b169577..187f10136 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -23,6 +23,7 @@ import requests from beets import plugins, ui ACOUSTIC_BASE = "http://acousticbrainz.org/" +LEVELS = ["/low-level", "/high-level"] class AcousticPlugin(plugins.BeetsPlugin): @@ -67,7 +68,7 @@ def fetch_info(log, items): log.info('getting data for: {}', item) # Fetch the data from the AB API. - urls = [generate_url(item.mb_trackid, path) for path in ["/low-level", "/high-level"]] + urls = [generate_url(item.mb_trackid, path) for path in LEVELS] log.debug('fetching URLs: {}', urls) try: responses = [requests.get(url) for url in urls] @@ -85,7 +86,8 @@ def fetch_info(log, items): data = responses[0].json() data.update(responses[1].json()) except ValueError: - log.debug('Invalid Response from low-level: {}', low.text) + log.debug('Invalid Response: {} & {}', + responses[0].text, responses[1].text) # Get each field and assign it on the item. item.danceable = get_value( From b5f71a88e87d07a8ed9e6bf99249e2aee7377de0 Mon Sep 17 00:00:00 2001 From: Ohm Patel Date: Sat, 2 Jan 2016 19:05:53 -0600 Subject: [PATCH 11/12] ABrainz: replace lambda with operator --- beetsplug/acousticbrainz.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index 187f10136..b0700c119 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -19,6 +19,7 @@ from __future__ import (division, absolute_import, print_function, unicode_literals) import requests +import operator from beets import plugins, ui @@ -57,9 +58,10 @@ class AcousticPlugin(plugins.BeetsPlugin): def fetch_info(log, items): """Currently outputs MBID and corresponding request status code. """ + def get_value(map_path): try: - return reduce(lambda d, k: d[k], map_path, data) + return reduce(operator.getitem, map_path, data) except KeyError: log.debug('Invalid Path: {}', map_path) From a1db349bf960563656ce58bd9d4b539390b36567 Mon Sep 17 00:00:00 2001 From: Ohm Patel Date: Sat, 2 Jan 2016 19:20:20 -0600 Subject: [PATCH 12/12] ABrainz: Variable parameters --- beetsplug/acousticbrainz.py | 55 ++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/beetsplug/acousticbrainz.py b/beetsplug/acousticbrainz.py index b0700c119..15584b03c 100644 --- a/beetsplug/acousticbrainz.py +++ b/beetsplug/acousticbrainz.py @@ -59,7 +59,7 @@ def fetch_info(log, items): """Currently outputs MBID and corresponding request status code. """ - def get_value(map_path): + def get_value(*map_path): try: return reduce(operator.getitem, map_path, data) except KeyError: @@ -73,85 +73,84 @@ def fetch_info(log, items): urls = [generate_url(item.mb_trackid, path) for path in LEVELS] log.debug('fetching URLs: {}', urls) try: - responses = [requests.get(url) for url in urls] + res = [requests.get(url) for url in urls] except requests.RequestException as exc: log.info('request error: {}', exc) continue # Check for missing tracks. - if any(r.status_code == 404 for r in responses): + if any(r.status_code == 404 for r in res): log.info('recording ID {} not found', item.mb_trackid) continue # Parse the JSON response. try: - data = responses[0].json() - data.update(responses[1].json()) + data = res[0].json() + data.update(res[1].json()) except ValueError: - log.debug('Invalid Response: {} & {}', - responses[0].text, responses[1].text) + log.debug('Invalid Response: {} & {}', [r.text for r in res]) # Get each field and assign it on the item. item.danceable = get_value( - ["highlevel", "danceability", "all", "danceable"], + "highlevel", "danceability", "all", "danceable", ) item.gender = get_value( - ["highlevel", "gender", "value"], + "highlevel", "gender", "value", ) item.genre_rosamerica = get_value( - ["highlevel", "genre_rosamerica", "value"], + "highlevel", "genre_rosamerica", "value" ) item.mood_acoustic = get_value( - ["highlevel", "mood_acoustic", "all", "acoustic"], + "highlevel", "mood_acoustic", "all", "acoustic" ) item.mood_aggressive = get_value( - ["highlevel", "mood_aggresive", "all", "aggresive"], + "highlevel", "mood_aggresive", "all", "aggresive" ) item.mood_electronic = get_value( - ["highlevel", "mood_electronic", "all", "electronic"], + "highlevel", "mood_electronic", "all", "electronic" ) item.mood_happy = get_value( - ["highlevel", "mood_happy", "all", "happy"], + "highlevel", "mood_happy", "all", "happy" ) item.mood_party = get_value( - ["highlevel", "mood_party", "all", "party"], + "highlevel", "mood_party", "all", "party" ) item.mood_relaxed = get_value( - ["highlevel", "mood_relaxed", "all", "relaxed"], + "highlevel", "mood_relaxed", "all", "relaxed" ) item.mood_sad = get_value( - ["highlevel", "mood_sad", "all", "sad"], + "highlevel", "mood_sad", "all", "sad" ) item.rhythm = get_value( - ["highlevel", "ismir04_rhythm", "value"], + "highlevel", "ismir04_rhythm", "value" ) item.tonal = get_value( - ["highlevel", "tonal_atonal", "all", "tonal"], + "highlevel", "tonal_atonal", "all", "tonal" ) item.voice_instrumental = get_value( - ["highlevel", "voice_instrumental", "value"], + "highlevel", "voice_instrumental", "value" ) item.average_loudness = get_value( - ["lowlevel", "average_loudness"], + "lowlevel", "average_loudness" ) item.chords_changes_rate = get_value( - ["tonal", "chords_changes_rate"], + "tonal", "chords_changes_rate" ) item.chords_key = get_value( - ["tonal", "chords_key"], + "tonal", "chords_key" ) item.chords_number_rate = get_value( - ["tonal", "chords_number_rate"], + "tonal", "chords_number_rate" ) item.chords_scale = get_value( - ["tonal", "chords_scale"], + "tonal", "chords_scale" ) item.initial_key = '{} {}'.format( - get_value(["tonal", "key_key"]), - get_value(["tonal", "key_scale"]) + get_value("tonal", "key_key"), + get_value("tonal", "key_scale") ) item.key_strength = get_value( - ["tonal", "key_stength"], + "tonal", "key_stength" ) # Store the data. We only update flexible attributes, so we