Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lastgenre plugin cli options to query explicit for albums and / or tracks. #3220

Merged
merged 6 commits into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions beetsplug/lastgenre/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -380,28 +381,46 @@ def commands(self):
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()

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

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]
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
5 changes: 4 additions & 1 deletion docs/plugins/lastgenre.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.