mirror of
https://github.com/beetbox/beets.git
synced 2026-01-07 00:15:20 +01:00
add a config parameter to lastgenre plugin allowing to define a genres whitelist
This commit is contained in:
parent
74015f3954
commit
35a06e6b62
1 changed files with 21 additions and 7 deletions
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue