merge echonest_tempo fix from master

This commit is contained in:
Adrian Sampson 2012-12-23 14:34:08 -08:00
commit 6b000983c3
2 changed files with 27 additions and 7 deletions

View file

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

View file

@ -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".