From fc95fb86a10b12ddeda368c05c2de11627bb1909 Mon Sep 17 00:00:00 2001 From: rain0r Date: Wed, 17 Apr 2019 20:57:58 +0200 Subject: [PATCH 1/6] Update __init__.py Also fetch genres for single tracks via query. --- beetsplug/lastgenre/__init__.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index 3fd473db32..a340c1e193 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -402,7 +402,12 @@ def lastgenre_func(lib, opts, args): if write: item.try_write() - + + 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] From 0b2334f8e8fc1e5b4f0528546e6b7d9a63d8c7f8 Mon Sep 17 00:00:00 2001 From: Rainer Hihn Date: Thu, 18 Apr 2019 22:31:49 +0200 Subject: [PATCH 2/6] Added command line options to query explicit for albums and / or tracks. --- beetsplug/lastgenre/__init__.py | 55 ++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index a340c1e193..a26b1fd82a 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -380,34 +380,45 @@ 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'--tracks', action='store_true', default=False, + help=u'match tracks instead of albums' + ) + lastgenre_cmd.parser.add_option( + u'-a', u'--albums', action='store_true', default=True, + help=u'match albums instead of tracks' + ) 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.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() + + if opts.tracks: + 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() - 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() - - 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] From e4b2e7b4760e33e92d788ef93e41829c49987515 Mon Sep 17 00:00:00 2001 From: Rainer Hihn Date: Fri, 19 Apr 2019 20:58:13 +0200 Subject: [PATCH 3/6] Made -a and -A mutually exclusive. --- beetsplug/lastgenre/__init__.py | 33 +++++++++++++++++++++------------ docs/changelog.rst | 1 + docs/plugins/lastgenre.rst | 2 +- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index a26b1fd82a..2f90cc1c29 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -14,6 +14,8 @@ # included in all copies or substantial portions of the Software. from __future__ import division, absolute_import, print_function + +import ipdb import six """Gets genres for imported music based on Last.fm tags. @@ -381,19 +383,24 @@ def commands(self): help=u'genre source: artist, album, or track' ) lastgenre_cmd.parser.add_option( - u'-A', u'--tracks', action='store_true', default=False, - help=u'match tracks instead of albums' - ) + u'-A', u'--items', action='store_true', + help=u'match items instead of albums') lastgenre_cmd.parser.add_option( - u'-a', u'--albums', action='store_true', default=True, - help=u'match albums instead of tracks' - ) + u'-a', u'--albums', action='store_true', + help=u'match albums instead of items') + lastgenre_cmd.parser.set_defaults(query_type='albums') + def lastgenre_func(lib, opts, args): write = ui.should_write() self.config.set_args(opts) + if opts.albums and opts.items: + self._log.error(u'options -a and -A are mutually exclusive') + return + if opts.albums: + # 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}', @@ -406,17 +413,19 @@ def lastgenre_func(lib, opts, args): 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) + self._log.info( + u'genre for track {0} ({1}): {0.genre}', + item, src) if write: item.try_write() - - if opts.tracks: + elif opts.items: + # 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) + self._log.debug(u'added last.fm item genre ({0}): {1}', + src, item.genre) item.store() lastgenre_cmd.func = lastgenre_func diff --git a/docs/changelog.rst b/docs/changelog.rst index 4ad0f057a5..e0df4793c9 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,6 +6,7 @@ Changelog New features: +* LastGenre can now be used to fetch genres for 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 5e3235bd73..9604cf8aa5 100644 --- a/docs/plugins/lastgenre.rst +++ b/docs/plugins/lastgenre.rst @@ -155,7 +155,7 @@ 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. To disable automatic genre fetching on import, set the ``auto`` config option to false. From 5c643a8f164ff137954386c5f717a8ce77fd7708 Mon Sep 17 00:00:00 2001 From: Rainer Hihn Date: Fri, 19 Apr 2019 21:08:26 +0200 Subject: [PATCH 4/6] Removed ipdb import --- beetsplug/lastgenre/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index 2f90cc1c29..b29d7d01ed 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -15,7 +15,6 @@ from __future__ import division, absolute_import, print_function -import ipdb import six """Gets genres for imported music based on Last.fm tags. From ddd7b4b3b4a30d060f127a4b6042008d8e655b76 Mon Sep 17 00:00:00 2001 From: Rainer Hihn Date: Fri, 19 Apr 2019 21:17:15 +0200 Subject: [PATCH 5/6] Removed empty line --- beetsplug/lastgenre/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index b29d7d01ed..f13b62f1ed 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -389,7 +389,6 @@ def commands(self): help=u'match albums instead of items') lastgenre_cmd.parser.set_defaults(query_type='albums') - def lastgenre_func(lib, opts, args): write = ui.should_write() self.config.set_args(opts) From b7d3ef62740aa2af90035fc9a6c7845541457e39 Mon Sep 17 00:00:00 2001 From: Rainer Hihn Date: Sun, 21 Apr 2019 18:32:41 +0200 Subject: [PATCH 6/6] - Improved doc and changelog - Cleaner implementation of mutual excursion of the command line arguments. --- beetsplug/lastgenre/__init__.py | 14 +++++--------- docs/changelog.rst | 2 +- docs/plugins/lastgenre.rst | 3 +++ 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index f13b62f1ed..cf90facbda 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -382,22 +382,18 @@ def commands(self): help=u'genre source: artist, album, or track' ) lastgenre_cmd.parser.add_option( - u'-A', u'--items', action='store_true', + 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', + u'-a', u'--albums', action='store_true', dest='album', help=u'match albums instead of items') - lastgenre_cmd.parser.set_defaults(query_type='albums') + lastgenre_cmd.parser.set_defaults(album=True) def lastgenre_func(lib, opts, args): write = ui.should_write() self.config.set_args(opts) - if opts.albums and opts.items: - self._log.error(u'options -a and -A are mutually exclusive') - return - - if opts.albums: + if opts.album: # Fetch genres for whole albums for album in lib.albums(ui.decargs(args)): album.genre, src = self._get_genre(album) @@ -417,7 +413,7 @@ def lastgenre_func(lib, opts, args): if write: item.try_write() - elif opts.items: + else: # Just query singletons, i.e. items that are not part of # an album for item in lib.items(ui.decargs(args)): diff --git a/docs/changelog.rst b/docs/changelog.rst index e0df4793c9..d5e9a04df8 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,7 +6,7 @@ Changelog New features: -* LastGenre can now be used to fetch genres for singletons. +* :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 9604cf8aa5..c7d04fe259 100644 --- a/docs/plugins/lastgenre.rst +++ b/docs/plugins/lastgenre.rst @@ -157,5 +157,8 @@ In addition to running automatically on import, the plugin can also be run manua from the command line. Use the command ``beet lastgenre [QUERY]`` to fetch 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.