mirror of
https://github.com/beetbox/beets.git
synced 2025-12-22 16:43:25 +01:00
lastgenre and lyrics: use new pluggable import stages
This solves a problem where files were copied before the genre field was updated, resulting in problems when $genre was used in a path (GC-357).
This commit is contained in:
parent
48ffa08928
commit
c5424dce05
3 changed files with 36 additions and 38 deletions
|
|
@ -124,7 +124,12 @@ options = {
|
|||
'branches': None,
|
||||
'c14n': False,
|
||||
}
|
||||
fallback_str = None
|
||||
class LastGenrePlugin(plugins.BeetsPlugin):
|
||||
def __init__(self):
|
||||
super(LastGenrePlugin, self).__init__()
|
||||
self.import_stages = [self.imported]
|
||||
|
||||
def configure(self, config):
|
||||
global fallback_str
|
||||
|
||||
|
|
@ -161,35 +166,27 @@ class LastGenrePlugin(plugins.BeetsPlugin):
|
|||
|
||||
fallback_str = ui.config_val(config, 'lastgenre', 'fallback_str', None)
|
||||
|
||||
def imported(self, config, task):
|
||||
if task.is_album:
|
||||
album = config.lib.get_album(task.album_id)
|
||||
lastfm_obj = LASTFM.get_album(album.albumartist, album.album)
|
||||
else:
|
||||
item = task.item
|
||||
lastfm_obj = LASTFM.get_track(item.artist, item.title)
|
||||
|
||||
@LastGenrePlugin.listen('album_imported')
|
||||
def album_imported(lib, album, config):
|
||||
global fallback_str
|
||||
tags = _tags_for(LASTFM.get_album(album.albumartist, album.album))
|
||||
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
|
||||
tags = _tags_for(lastfm_obj)
|
||||
genre = _tags_to_genre(tags)
|
||||
|
||||
if config.write:
|
||||
for item in album.items():
|
||||
item.write()
|
||||
if not genre and fallback_str != None:
|
||||
genre = fallback_str
|
||||
log.debug(u'no last.fm genre found: fallback to %s' % genre)
|
||||
|
||||
@LastGenrePlugin.listen('item_imported')
|
||||
def item_imported(lib, item, config):
|
||||
global fallback_str
|
||||
tags = _tags_for(LASTFM.get_track(item.artist, item.title))
|
||||
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 item genre: %s' % genre)
|
||||
item.genre = genre
|
||||
lib.store(item)
|
||||
if genre is not None:
|
||||
log.debug(u'adding last.fm album genre: %s' % genre)
|
||||
|
||||
if config.write:
|
||||
item.write()
|
||||
if task.is_album:
|
||||
album = config.lib.get_album(task.album_id)
|
||||
album.genre = genre
|
||||
else:
|
||||
item.genre = genre
|
||||
config.lib.store(item)
|
||||
|
|
|
|||
|
|
@ -182,6 +182,10 @@ def fetch_item_lyrics(lib, loglevel, item, write):
|
|||
|
||||
AUTOFETCH = True
|
||||
class LyricsPlugin(BeetsPlugin):
|
||||
def __init__(self):
|
||||
super(LyricsPlugin, self).__init__()
|
||||
self.import_stages = [self.imported]
|
||||
|
||||
def commands(self):
|
||||
cmd = ui.Subcommand('lyrics', help='fetch song lyrics')
|
||||
cmd.parser.add_option('-p', '--print', dest='printlyr',
|
||||
|
|
@ -203,13 +207,8 @@ class LyricsPlugin(BeetsPlugin):
|
|||
global AUTOFETCH
|
||||
AUTOFETCH = ui.config_val(config, 'lyrics', 'autofetch', True, bool)
|
||||
|
||||
# Auto-fetch lyrics on import.
|
||||
@LyricsPlugin.listen('album_imported')
|
||||
def album_imported(lib, album, config):
|
||||
if AUTOFETCH:
|
||||
for item in album.items():
|
||||
fetch_item_lyrics(lib, logging.DEBUG, item, config.write)
|
||||
@LyricsPlugin.listen('item_imported')
|
||||
def item_imported(lib, item, config):
|
||||
if AUTOFETCH:
|
||||
fetch_item_lyrics(lib, logging.DEBUG, item, config.write)
|
||||
# Auto-fetch lyrics on import.
|
||||
def imported(self, config, task):
|
||||
if AUTOFETCH:
|
||||
for item in task.all_items():
|
||||
fetch_item_lyrics(config.lib, logging.DEBUG, item, False)
|
||||
|
|
|
|||
|
|
@ -26,6 +26,8 @@ Changelog
|
|||
avoid internal SQLite contention, which should avoid this error.
|
||||
* Plugins can now add parallel stages to the import pipeline. See
|
||||
:ref:`writing-plugins`.
|
||||
* :doc:`/plugins/lastgenre`: Fixed a problem where path formats containing
|
||||
`$genre` would use the old genre instead of the newly discovered one.
|
||||
* New plugin event: ``import_task_choice`` is called after an import task has an
|
||||
action assigned.
|
||||
* New plugin event: ``library_opened`` is called when beets starts up and
|
||||
|
|
|
|||
Loading…
Reference in a new issue