diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index b23a0b2c5..62632d32e 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -124,7 +124,12 @@ options = { 'branches': None, 'c14n': False, } +fallback_str = None class LastGenrePlugin(plugins.BeetsPlugin): + def __init__(self): + super(LastGenrePlugin, self).__init__() + self.import_stages = [self.imported] + def configure(self, config): global fallback_str @@ -161,35 +166,27 @@ class LastGenrePlugin(plugins.BeetsPlugin): fallback_str = ui.config_val(config, 'lastgenre', 'fallback_str', None) + def imported(self, config, task): + if task.is_album: + album = config.lib.get_album(task.album_id) + lastfm_obj = LASTFM.get_album(album.albumartist, album.album) + else: + item = task.item + lastfm_obj = LASTFM.get_track(item.artist, item.title) -@LastGenrePlugin.listen('album_imported') -def album_imported(lib, album, config): - global fallback_str - tags = _tags_for(LASTFM.get_album(album.albumartist, album.album)) - genre = _tags_to_genre(tags) - if not genre and fallback_str != None : - genre = fallback_str - log.debug(u'no last.fm genre found: fallback to %s' % genre) - if genre is not None: - log.debug(u'adding last.fm album genre: %s' % genre) - album.genre = genre + tags = _tags_for(lastfm_obj) + genre = _tags_to_genre(tags) - if config.write: - for item in album.items(): - item.write() + if not genre and fallback_str != None: + genre = fallback_str + log.debug(u'no last.fm genre found: fallback to %s' % genre) -@LastGenrePlugin.listen('item_imported') -def item_imported(lib, item, config): - global fallback_str - tags = _tags_for(LASTFM.get_track(item.artist, item.title)) - genre = _tags_to_genre(tags) - if not genre and fallback_str != None : - genre = fallback_str - log.debug(u'no last.fm genre found: fallback to %s' % genre) - if genre is not None: - log.debug(u'adding last.fm item genre: %s' % genre) - item.genre = genre - lib.store(item) + if genre is not None: + log.debug(u'adding last.fm album genre: %s' % genre) - if config.write: - item.write() + if task.is_album: + album = config.lib.get_album(task.album_id) + album.genre = genre + else: + item.genre = genre + config.lib.store(item) diff --git a/beetsplug/lyrics.py b/beetsplug/lyrics.py index 97de23cbe..24a13c35e 100644 --- a/beetsplug/lyrics.py +++ b/beetsplug/lyrics.py @@ -182,6 +182,10 @@ def fetch_item_lyrics(lib, loglevel, item, write): AUTOFETCH = True class LyricsPlugin(BeetsPlugin): + def __init__(self): + super(LyricsPlugin, self).__init__() + self.import_stages = [self.imported] + def commands(self): cmd = ui.Subcommand('lyrics', help='fetch song lyrics') cmd.parser.add_option('-p', '--print', dest='printlyr', @@ -203,13 +207,8 @@ class LyricsPlugin(BeetsPlugin): 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) + # Auto-fetch lyrics on import. + def imported(self, config, task): + if AUTOFETCH: + for item in task.all_items(): + fetch_item_lyrics(config.lib, logging.DEBUG, item, False) diff --git a/docs/changelog.rst b/docs/changelog.rst index a70ee453d..0b6da3cce 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -26,6 +26,8 @@ Changelog avoid internal SQLite contention, which should avoid this error. * Plugins can now add parallel stages to the import pipeline. See :ref:`writing-plugins`. +* :doc:`/plugins/lastgenre`: Fixed a problem where path formats containing + `$genre` would use the old genre instead of the newly discovered one. * New plugin event: ``import_task_choice`` is called after an import task has an action assigned. * New plugin event: ``library_opened`` is called when beets starts up and