Skip to content

Commit d2cca4a

Browse files
committed
renamed keywords to query_string, shortened query construct expression,
removed legacy isinstance check
1 parent 0b60fdc commit d2cca4a

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

beets/metadata_plugins.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,13 @@ def _search_api(
353353
self,
354354
query_type: Literal["album", "track"],
355355
filters: SearchFilter,
356-
keywords: str = "",
356+
query_string: str = "",
357357
) -> Sequence[R]:
358358
"""Perform a search on the API.
359359
360360
:param query_type: The type of query to perform.
361361
:param filters: A dictionary of filters to apply to the search.
362-
:param keywords: Additional keywords to include in the search.
362+
:param query_string: Additional query to include in the search.
363363
364364
Should return a list of identifiers for the requested type (album or track).
365365
"""
@@ -387,7 +387,9 @@ def candidates(
387387
def item_candidates(
388388
self, item: Item, artist: str, title: str
389389
) -> Iterable[TrackInfo]:
390-
results = self._search_api("track", {"artist": artist}, keywords=title)
390+
results = self._search_api(
391+
"track", {"artist": artist}, query_string=title
392+
)
391393
if not results:
392394
return []
393395

@@ -397,7 +399,7 @@ def item_candidates(
397399
)
398400

399401
def _construct_search_query(
400-
self, filters: SearchFilter, keywords: str = ""
402+
self, filters: SearchFilter, query_string: str
401403
) -> str:
402404
"""Construct a query string with the specified filters and keywords to
403405
be provided to the Spotify (or similar) Search API.
@@ -407,17 +409,12 @@ def _construct_search_query(
407409
- Deezer (https://developers.deezer.com/api/search).
408410
409411
:param filters: Field filters to apply.
410-
:param keywords: Query keywords to use.
412+
:param query_string: Query keywords to use.
411413
:return: Query string to be provided to the Search API.
412414
"""
413415

414-
query_components = [
415-
keywords,
416-
" ".join(f'{k}:"{v}"' for k, v in filters.items()),
417-
]
418-
query = " ".join([q for q in query_components if q])
419-
if not isinstance(query, str):
420-
query = query.decode("utf8")
416+
components = [query_string, *(f'{k}:"{v}"' for k, v in filters.items())]
417+
query = " ".join(filter(None, components))
421418

422419
if self.config["search_query_ascii"].get():
423420
query = unidecode.unidecode(query)

beetsplug/deezer.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,19 @@ def _search_api(
228228
"user",
229229
],
230230
filters: SearchFilter,
231-
keywords="",
231+
query_string: str = "",
232232
) -> Sequence[IDResponse]:
233-
"""Query the Deezer Search API for the specified ``keywords``, applying
233+
"""Query the Deezer Search API for the specified ``query_string``, applying
234234
the provided ``filters``.
235235
236236
:param filters: Field filters to apply.
237-
:param keywords: Query keywords to use.
237+
:param query_string: Additional query to include in the search.
238238
:return: JSON data for the class:`Response <Response>` object or None
239239
if no search results are returned.
240240
"""
241-
query = self._construct_search_query(keywords=keywords, filters=filters)
241+
query = self._construct_search_query(
242+
query_string=query_string, filters=filters
243+
)
242244
self._log.debug(f"Searching {self.data_source} for '{query}'")
243245
try:
244246
response = requests.get(

beetsplug/spotify.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -424,17 +424,19 @@ def _search_api(
424424
self,
425425
query_type: Literal["album", "track"],
426426
filters: SearchFilter,
427-
keywords: str = "",
427+
query_string: str = "",
428428
) -> Sequence[SearchResponseAlbums | SearchResponseTracks]:
429-
"""Query the Spotify Search API for the specified ``keywords``,
429+
"""Query the Spotify Search API for the specified ``query_string``,
430430
applying the provided ``filters``.
431431
432432
:param query_type: Item type to search across. Valid types are:
433433
'album', 'artist', 'playlist', and 'track'.
434-
:param filters: (Optional) Field filters to apply.
435-
:param keywords: (Optional) Query keywords to use.
434+
:param filters: Field filters to apply.
435+
:param query_string: Additional query to include in the search.
436436
"""
437-
query = self._construct_search_query(keywords=keywords, filters=filters)
437+
query = self._construct_search_query(
438+
filters=filters, query_string=query_string
439+
)
438440

439441
self._log.debug(f"Searching {self.data_source} for '{query}'")
440442
try:
@@ -561,16 +563,18 @@ def _match_library_tracks(self, library: Library, keywords: str):
561563
# Custom values can be passed in the config (just in case)
562564
artist = item[self.config["artist_field"].get()]
563565
album = item[self.config["album_field"].get()]
564-
keywords = item[self.config["track_field"].get()]
566+
query_string = item[self.config["track_field"].get()]
565567

566568
# Query the Web API for each track, look for the items' JSON data
567569
query_filters: SearchFilter = {"artist": artist, "album": album}
568570
response_data_tracks = self._search_api(
569-
query_type="track", keywords=keywords, filters=query_filters
571+
query_type="track",
572+
query_string=query_string,
573+
filters=query_filters,
570574
)
571575
if not response_data_tracks:
572576
query = self._construct_search_query(
573-
keywords=keywords, filters=query_filters
577+
query_string=query_string, filters=query_filters
574578
)
575579

576580
failures.append(query)

0 commit comments

Comments
 (0)