add a config parameter to lastgenre plugin allowing to define a genres whitelist

This commit is contained in:
kraymer 2011-09-24 23:31:15 +02:00
parent 74015f3954
commit 35a06e6b62

View file

@ -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()