diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index 3fd473db3..cf90facbd 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -14,6 +14,7 @@ # included in all copies or substantial portions of the Software. from __future__ import division, absolute_import, print_function + import six """Gets genres for imported music based on Last.fm tags. @@ -380,28 +381,46 @@ class LastGenrePlugin(plugins.BeetsPlugin): u'-s', u'--source', dest='source', type='string', help=u'genre source: artist, album, or track' ) + lastgenre_cmd.parser.add_option( + u'-A', u'--items', action='store_false', dest='album', + help=u'match items instead of albums') + lastgenre_cmd.parser.add_option( + u'-a', u'--albums', action='store_true', dest='album', + help=u'match albums instead of items') + lastgenre_cmd.parser.set_defaults(album=True) def lastgenre_func(lib, opts, args): write = ui.should_write() self.config.set_args(opts) - for album in lib.albums(ui.decargs(args)): - album.genre, src = self._get_genre(album) - self._log.info(u'genre for album {0} ({1}): {0.genre}', - album, src) - album.store() + if opts.album: + # Fetch genres for whole albums + for album in lib.albums(ui.decargs(args)): + album.genre, src = self._get_genre(album) + self._log.info(u'genre for album {0} ({1}): {0.genre}', + album, src) + album.store() - for item in album.items(): - # If we're using track-level sources, also look up each - # track on the album. - if 'track' in self.sources: - item.genre, src = self._get_genre(item) - item.store() - self._log.info(u'genre for track {0} ({1}): {0.genre}', - item, src) + for item in album.items(): + # If we're using track-level sources, also look up each + # track on the album. + if 'track' in self.sources: + item.genre, src = self._get_genre(item) + item.store() + self._log.info( + u'genre for track {0} ({1}): {0.genre}', + item, src) - if write: - item.try_write() + if write: + item.try_write() + else: + # Just query singletons, i.e. items that are not part of + # an album + for item in lib.items(ui.decargs(args)): + item.genre, src = self._get_genre(item) + self._log.debug(u'added last.fm item genre ({0}): {1}', + src, item.genre) + item.store() lastgenre_cmd.func = lastgenre_func return [lastgenre_cmd] diff --git a/docs/changelog.rst b/docs/changelog.rst index 2ccfbadb5..7dd455125 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,6 +6,7 @@ Changelog New features: +* :doc:`/plugins/lastgenre`: Added option ``-A`` to match individual tracks and singletons. * The disambiguation string for identifying albums in the importer now shows the catalog number. Thanks to :user:`8h2a`. diff --git a/docs/plugins/lastgenre.rst b/docs/plugins/lastgenre.rst index 5e3235bd7..c7d04fe25 100644 --- a/docs/plugins/lastgenre.rst +++ b/docs/plugins/lastgenre.rst @@ -155,7 +155,10 @@ Running Manually In addition to running automatically on import, the plugin can also be run manually from the command line. Use the command ``beet lastgenre [QUERY]`` to fetch -genres for albums matching a certain query. +genres for albums or items matching a certain query. + +By default, ``beet lastgenre`` matches albums. If you would like to match +individual tracks or singletons, use the ``-A`` switch: ``beet lastgenre -A [QUERY]``. To disable automatic genre fetching on import, set the ``auto`` config option to false.