From ba140a3f9797c1c333fa287c7f3d5850073b652e Mon Sep 17 00:00:00 2001 From: Jakob Schnitzer Date: Thu, 4 Oct 2012 10:47:51 +0200 Subject: [PATCH] Added command to the lastgenre plugin --- beetsplug/lastgenre/__init__.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index 4d5cec51a..b915f815c 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -31,6 +31,7 @@ import os from beets import plugins from beets import ui from beets.util import normpath +from beets.ui import commands log = logging.getLogger('beets') @@ -166,6 +167,35 @@ class LastGenrePlugin(plugins.BeetsPlugin): fallback_str = ui.config_val(config, 'lastgenre', 'fallback_str', None) + def commands(self): + lastgenre_cmd = ui.Subcommand('lastgenre', help='fetch genres') + def lastgenre_func(lib, config, opts, args): + # The "write to files" option corresponds to the + # import_write config value. + write = ui.config_val(config, 'beets', 'import_write', + commands.DEFAULT_IMPORT_WRITE, bool) + for album in lib.albums(ui.decargs(args)): + tags = [] + lastfm_obj = LASTFM.get_album(album.albumartist, album.album) + if album.genre: + tags.append(album.genre) + + tags.extend(_tags_for(lastfm_obj)) + 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 + if write: + for item in album.items(): + item.write() + lastgenre_cmd.func = lastgenre_func + return [lastgenre_cmd] + def imported(self, config, task): tags = [] if task.is_album: