diff --git a/beetsplug/lastgenre.py b/beetsplug/lastgenre.py index 4c6af79b1..f75615889 100644 --- a/beetsplug/lastgenre.py +++ b/beetsplug/lastgenre.py @@ -18,7 +18,7 @@ import logging import pylast -from beets import plugins +from beets import plugins, ui log = logging.getLogger('beets') LASTFM = pylast.LastFMNetwork(api_key=plugins.LASTFM_KEY) @@ -44,17 +44,29 @@ def _tags_for(obj): return tags def _tags_to_genre(tags): - """Given a tag list, returns a genre. Returns None if no tag is - suitable. This should be smarter, but at the moment it just takes - the top tag and puts it in Title Case. + """Given a tag list, returns a genre. Returns the first tag that is present + in genres white list or None if no tag is suitable. """ if not tags: return None - return tags[0].title() + elif not options['genres_whitelist']: + return tags[0].title() + + for tag in tags : + if tag.lower() in options['genres_whitelist'] : + return tag.title() + + return None +options = { + 'genres_whitelist': None, +} class LastGenrePlugin(plugins.BeetsPlugin): - pass - + def configure(self, config): + genres_whitelist = ui.config_val(config, 'lastgenre', + 'genres_whitelist', None) + options['genres_whitelist'] = genres_whitelist.lower().split(',') + @LastGenrePlugin.listen('album_imported') def album_imported(lib, album): tags = _tags_for(LASTFM.get_album(album.albumartist, album.album)) @@ -67,8 +79,10 @@ def album_imported(lib, album): @LastGenrePlugin.listen('item_imported') def item_imported(lib, item): tags = _tags_for(LASTFM.get_track(item.artist, item.title)) + genre = _tags_to_genre(tags) if genre: log.debug(u'adding last.fm item genre: %s' % genre) item.genre = genre lib.save() +