diff --git a/beetsplug/echonest_tempo.py b/beetsplug/echonest_tempo.py index 73e46d40b..f93842e38 100644 --- a/beetsplug/echonest_tempo.py +++ b/beetsplug/echonest_tempo.py @@ -30,6 +30,9 @@ log = logging.getLogger('beets') # the user. ECHONEST_APIKEY = 'NY2KTZHQ0QDSHBAP6' +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 @@ -58,18 +61,26 @@ 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. - try: - 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 - wait 10 seconds and try again. - time.sleep(10) - return get_tempo(artist, title) + 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'] diff --git a/docs/changelog.rst b/docs/changelog.rst index 9737406b8..b4a74770e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -4,6 +4,8 @@ Changelog 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".