ABrainz: Added low-level data and updated docs

This commit is contained in:
Ohm Patel 2016-01-02 11:33:19 -06:00
parent c5f25429e9
commit 17dd7496a2
2 changed files with 53 additions and 23 deletions

View file

@ -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):

View file

@ -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``