Merge pull request #50 from yagebu/master

Added a command to the lastgenre plugin
This commit is contained in:
Adrian Sampson 2012-10-04 09:44:15 -07:00
commit c2b1c036e6

View file

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