Skip to content

Commit 62dd99c

Browse files
authored
Merge pull request #1 from Explosion-Scratch/patch-2
Truncate search query by iterating over components
2 parents a197d4c + 35f1f4c commit 62dd99c

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

beetsplug/spotify.py

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,36 @@ def track_for_id(self, track_id=None, track_data=None):
381381
track.index = i
382382
track.medium_total = medium_total
383383
return track
384-
384+
385+
@staticmethod
386+
def _list_limit(str_input, max_length=50, delim=","):
387+
"""Return a list of strings from the input string that can be joined into
388+
a single string with a length less than or equal to max_length.
389+
390+
:param str_input: String to split and join.
391+
:type str_input: str
392+
:param max_length: Maximum length of the resulting string.
393+
:type max_length: int
394+
:param delim: Delimiter to use when splitting and joining the strings.
395+
:type delim: str
396+
:return: List of strings that can be joined into a single string with a
397+
length less than or equal to max_length.
398+
:rtype: List[str]
399+
"""
400+
str_list = str_input.split(delim)
401+
if len(str_list) == 1:
402+
# If the input string doesn't contain the delimiter, trim it to max_length
403+
return [str_list[0][:max_length]]
404+
result = []
405+
current_length = 0
406+
for item in str_list:
407+
item_length = len(item) + len(delim)
408+
if current_length + item_length > max_length:
409+
break
410+
result.append(item)
411+
current_length += item_length
412+
return result
413+
385414
@staticmethod
386415
def _construct_search_query(filters=None, keywords=""):
387416
"""Construct a query string with the specified filters and keywords to
@@ -399,10 +428,19 @@ def _construct_search_query(filters=None, keywords=""):
399428
keywords,
400429
" ".join(":".join((k, v)) for k, v in filters.items()),
401430
]
402-
query = " ".join([q for q in query_components if q])
403-
if not isinstance(query, str):
404-
query = query.decode("utf8")
405-
return unidecode.unidecode(query)
431+
query = []
432+
433+
# Limit each component to 50 characters max, so, for example the artist field doesn't take up all 100 characters
434+
for component in query_components:
435+
if component
436+
if not isinstance(component, str):
437+
component = component.decode("utf8")
438+
limited_list = self._list_limit(component, max_length=50, delim=',')
439+
# Each component is split by a comma but queries are joined by a space
440+
query.append(",".join(limited_list))
441+
442+
# Make sure it's less than 100 characters
443+
return unidecode.unidecode(self._list_limit(" ".join(query), max_length=100, delim=' '))
406444

407445
def _search_api(self, query_type, filters=None, keywords=""):
408446
"""Query the Spotify Search API for the specified ``keywords``,
@@ -553,8 +591,6 @@ def _match_library_tracks(self, library, keywords):
553591
keywords = item[self.config["track_field"].get()]
554592

555593
# Query the Web API for each track, look for the items' JSON data
556-
if len(artist) > 50:
557-
artist = ",".join(artist.split(",")[:2])
558594
query_filters = {"artist": artist, "album": album}
559595
response_data_tracks = self._search_api(
560596
query_type="track", keywords=keywords, filters=query_filters

0 commit comments

Comments
 (0)