diff --git a/beets/metadata_plugins.py b/beets/metadata_plugins.py index 7e333a783..be858bdef 100644 --- a/beets/metadata_plugins.py +++ b/beets/metadata_plugins.py @@ -348,13 +348,13 @@ class SearchApiMetadataSourcePlugin( self, query_type: Literal["album", "track"], filters: SearchFilter, - keywords: str = "", + query_string: str = "", ) -> Sequence[R]: """Perform a search on the API. :param query_type: The type of query to perform. :param filters: A dictionary of filters to apply to the search. - :param keywords: Additional keywords to include in the search. + :param query_string: Additional query to include in the search. Should return a list of identifiers for the requested type (album or track). """ @@ -382,7 +382,9 @@ class SearchApiMetadataSourcePlugin( def item_candidates( self, item: Item, artist: str, title: str ) -> Iterable[TrackInfo]: - results = self._search_api("track", {"artist": artist}, keywords=title) + results = self._search_api( + "track", {"artist": artist}, query_string=title + ) if not results: return [] @@ -392,7 +394,7 @@ class SearchApiMetadataSourcePlugin( ) def _construct_search_query( - self, filters: SearchFilter, keywords: str = "" + self, filters: SearchFilter, query_string: str ) -> str: """Construct a query string with the specified filters and keywords to be provided to the Spotify (or similar) Search API. @@ -402,17 +404,12 @@ class SearchApiMetadataSourcePlugin( - Deezer (https://developers.deezer.com/api/search). :param filters: Field filters to apply. - :param keywords: Query keywords to use. + :param query_string: Query keywords to use. :return: Query string to be provided to the Search API. """ - query_components = [ - keywords, - " ".join(f'{k}:"{v}"' for k, v in filters.items()), - ] - query = " ".join([q for q in query_components if q]) - if not isinstance(query, str): - query = query.decode("utf8") + components = [query_string, *(f'{k}:"{v}"' for k, v in filters.items())] + query = " ".join(filter(None, components)) if self.config["search_query_ascii"].get(): query = unidecode.unidecode(query) diff --git a/beetsplug/deezer.py b/beetsplug/deezer.py index abb7d80c4..c8602b5e8 100644 --- a/beetsplug/deezer.py +++ b/beetsplug/deezer.py @@ -228,17 +228,19 @@ class DeezerPlugin(SearchApiMetadataSourcePlugin[IDResponse]): "user", ], filters: SearchFilter, - keywords="", + query_string: str = "", ) -> Sequence[IDResponse]: - """Query the Deezer Search API for the specified ``keywords``, applying + """Query the Deezer Search API for the specified ``query_string``, applying the provided ``filters``. :param filters: Field filters to apply. - :param keywords: Query keywords to use. + :param query_string: Additional query to include in the search. :return: JSON data for the class:`Response ` object or None if no search results are returned. """ - query = self._construct_search_query(keywords=keywords, filters=filters) + query = self._construct_search_query( + query_string=query_string, filters=filters + ) self._log.debug(f"Searching {self.data_source} for '{query}'") try: response = requests.get( diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index cdbed655e..f45ed158b 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -424,17 +424,19 @@ class SpotifyPlugin( self, query_type: Literal["album", "track"], filters: SearchFilter, - keywords: str = "", + query_string: str = "", ) -> Sequence[SearchResponseAlbums | SearchResponseTracks]: - """Query the Spotify Search API for the specified ``keywords``, + """Query the Spotify Search API for the specified ``query_string``, applying the provided ``filters``. :param query_type: Item type to search across. Valid types are: 'album', 'artist', 'playlist', and 'track'. - :param filters: (Optional) Field filters to apply. - :param keywords: (Optional) Query keywords to use. + :param filters: Field filters to apply. + :param query_string: Additional query to include in the search. """ - query = self._construct_search_query(keywords=keywords, filters=filters) + query = self._construct_search_query( + filters=filters, query_string=query_string + ) self._log.debug(f"Searching {self.data_source} for '{query}'") try: @@ -561,16 +563,18 @@ class SpotifyPlugin( # Custom values can be passed in the config (just in case) artist = item[self.config["artist_field"].get()] album = item[self.config["album_field"].get()] - keywords = item[self.config["track_field"].get()] + query_string = item[self.config["track_field"].get()] # Query the Web API for each track, look for the items' JSON data query_filters: SearchFilter = {"artist": artist, "album": album} response_data_tracks = self._search_api( - query_type="track", keywords=keywords, filters=query_filters + query_type="track", + query_string=query_string, + filters=query_filters, ) if not response_data_tracks: query = self._construct_search_query( - keywords=keywords, filters=query_filters + query_string=query_string, filters=query_filters ) failures.append(query)