diff --git a/beetsplug/echonest_tempo.py b/beetsplug/echonest_tempo.py index bdfc8ac21..b010c8763 100644 --- a/beetsplug/echonest_tempo.py +++ b/beetsplug/echonest_tempo.py @@ -15,10 +15,10 @@ """Gets tempo (bpm) for imported music from the EchoNest API. Requires the pyechonest library (https://github.com/echonest/pyechonest). """ +import time import logging from beets.plugins import BeetsPlugin from beets import ui -from beets.ui import commands from beets import config import pyechonest.config import pyechonest.song @@ -26,6 +26,9 @@ import pyechonest.song # Global logger. log = logging.getLogger('beets') +RETRY_INTERVAL = 10 # Seconds. +RETRIES = 10 + def fetch_item_tempo(lib, loglevel, item, write): """Fetch and store tempo for a single item. If ``write``, then the tempo will also be written to the file itself in the bpm field. The @@ -54,12 +57,27 @@ def fetch_item_tempo(lib, loglevel, item, write): def get_tempo(artist, title): """Get the tempo for a song.""" - # Unfortunately, all we can do is search by artist and title. EchoNest - # supports foreign ids from MusicBrainz, but currently only for artists, - # not individual tracks/recordings. - results = pyechonest.song.search( - artist=artist, title=title, results=1, buckets=['audio_summary'] - ) + for i in range(RETRIES): + try: + # Unfortunately, all we can do is search by artist and title. + # EchoNest supports foreign ids from MusicBrainz, but currently + # only for artists, not individual tracks/recordings. + results = pyechonest.song.search( + artist=artist, title=title, results=1, buckets=['audio_summary'] + ) + except pyechonest.util.EchoNestAPIError as e: + if e.code == 3: + # Rate limit exceeded. + if i >= RETRIES - 1: + # Waited too many times already. + log.debug(u'echonest_tempo: exceeded retries') + return None + else: + # Wait and try again. + time.sleep(RETRY_INTERVAL) + else: + break + if len(results) > 0: return results[0].audio_summary['tempo'] else: diff --git a/docs/changelog.rst b/docs/changelog.rst index 8863e7ed0..40a682774 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -9,6 +9,8 @@ This release entirely revamps beets' configuration system. 1.0rc2 (in development) ----------------------- +* :doc:`/plugins/echonest_tempo`: If the Echo Nest API limit is exceeded, the + plugin now waits and tries again instead of crashing. Thanks to Zach Denton. * :doc:`/plugins/fetchart`: Fix a regression that caused crashes when art was not available from some sources. * Fix a regression on Windows that caused all relative paths to be "not found".