From 248a433d1d640ae2c36cfc030309b0fbbede4eb9 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Thu, 19 Jan 2012 12:39:20 -0800 Subject: [PATCH] lyrics auto-fetch on import (#137) --- beetsplug/lyrics.py | 64 +++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/beetsplug/lyrics.py b/beetsplug/lyrics.py index 365415237..c8e4e1e7b 100644 --- a/beetsplug/lyrics.py +++ b/beetsplug/lyrics.py @@ -126,32 +126,34 @@ def get_lyrics(artist, title): log = logging.getLogger('beets') -def fetch_lyrics(lib, query, write): - """Fetch and store lyrics for each matched item. If ``write``, then - the lyrics will also be written to the file itself. +def fetch_item_lyrics(lib, loglevel, item, write): + """Fetch and store lyrics for a single item. If ``write``, then the + lyrics will also be written to the file itself. The ``loglevel`` + parameter controls the visibility of the function's status log + messages. """ - for item in lib.items(query): - # Skip if the item already has lyrics. - if item.lyrics: - log.info(u'lyrics already present: %s - %s' % - (item.artist, item.title)) - continue + # Skip if the item already has lyrics. + if item.lyrics: + log.log(loglevel, u'lyrics already present: %s - %s' % + (item.artist, item.title)) + return - # Fetch lyrics. - lyrics = get_lyrics(item.artist, item.title) - if not lyrics: - log.info(u'lyrics not found: %s - %s' % - (item.artist, item.title)) - continue + # Fetch lyrics. + lyrics = get_lyrics(item.artist, item.title) + if not lyrics: + log.log(loglevel, u'lyrics not found: %s - %s' % + (item.artist, item.title)) + return - log.info(u'fetched lyrics: %s - %s' % - (item.artist, item.title)) - item.lyrics = lyrics - if write: - item.write() - lib.store(item) - lib.save() + log.log(loglevel, u'fetched lyrics: %s - %s' % + (item.artist, item.title)) + item.lyrics = lyrics + if write: + item.write() + lib.store(item) + lib.save() +AUTOFETCH = True class LyricsPlugin(BeetsPlugin): def commands(self): cmd = ui.Subcommand('lyrics', help='fetch song lyrics') @@ -160,6 +162,22 @@ class LyricsPlugin(BeetsPlugin): # import_write config value. write = ui.config_val(config, 'beets', 'import_write', commands.DEFAULT_IMPORT_WRITE, bool) - fetch_lyrics(lib, ui.decargs(args), write) + for item in lib.items(ui.decargs(args)): + fetch_item_lyrics(lib, logging.INFO, item, write) cmd.func = func return [cmd] + + def configure(self, config): + global AUTOFETCH + AUTOFETCH = ui.config_val(config, 'lyrics', 'autofetch', True, bool) + +# Auto-fetch lyrics on import. +@LyricsPlugin.listen('album_imported') +def album_imported(lib, album, config): + if AUTOFETCH: + for item in album.items(): + fetch_item_lyrics(lib, logging.DEBUG, item, config.write) +@LyricsPlugin.listen('item_imported') +def item_imported(lib, item, config): + if AUTOFETCH: + fetch_item_lyrics(lib, logging.DEBUG, item, config.write)