mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-10-12 17:10:45 +00:00
Update search_markets method to apply the total parameter to all types, add tests (#901)
* Update search_markets method to apply the total parameter to all types, fixes #534 * Add integration tests for searching multiple types in multiple markets * Update search_markets method to apply the total parameter to all types, add tests --------- Co-authored-by: Stéphane Bruckert <stephane.bruckert@gmail.com>
This commit is contained in:
1 parent
f2d23e2219
commit
fe438c0432
3 files changed
+104
-13
No files matched your search
+20
-11
@@ -15,6 +15,8 @@ import urllib3
|
||||
|
||||
from spotipy.exceptions import SpotifyException
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -594,12 +596,12 @@ class Spotify(object):
|
||||
official documentation https://developer.spotify.com/documentation/web-api/reference/search/) # noqa
|
||||
- limit - the number of items to return (min = 1, default = 10, max = 50). If a search is to be done on multiple
|
||||
markets, then this limit is applied to each market. (e.g. search US, CA, MX each with a limit of 10).
|
||||
If multiple types are specified, this applies to each type.
|
||||
- offset - the index of the first item to return
|
||||
- type - the types of items to return. One or more of 'artist', 'album',
|
||||
'track', 'playlist', 'show', or 'episode'. If multiple types are desired, pass in a comma separated string.
|
||||
- markets - A list of ISO 3166-1 alpha-2 country codes. Search all country markets by default.
|
||||
- total - the total number of results to return if multiple markets are supplied in the search.
|
||||
If multiple types are specified, this only applies to the first type.
|
||||
- total - the total number of results to return across multiple markets and types.
|
||||
"""
|
||||
warnings.warn(
|
||||
"Searching multiple markets is an experimental feature. "
|
||||
@@ -2005,22 +2007,29 @@ class Spotify(object):
|
||||
UserWarning,
|
||||
)
|
||||
|
||||
results = {}
|
||||
first_type = type.split(",")[0] + 's'
|
||||
results = defaultdict(dict)
|
||||
item_types = [item_type + "s" for item_type in type.split(",")]
|
||||
count = 0
|
||||
|
||||
for country in markets:
|
||||
result = self._get(
|
||||
"search", q=q, limit=limit, offset=offset, type=type, market=country
|
||||
)
|
||||
results[country] = result
|
||||
for item_type in item_types:
|
||||
results[country][item_type] = result[item_type]
|
||||
|
||||
# Truncate the items list to the current limit
|
||||
if len(results[country][item_type]['items']) > limit:
|
||||
results[country][item_type]['items'] = \
|
||||
results[country][item_type]['items'][:limit]
|
||||
|
||||
count += len(results[country][item_type]['items'])
|
||||
if total and limit > total - count:
|
||||
# when approaching `total` results, adjust `limit` to not request more
|
||||
# items than needed
|
||||
limit = total - count
|
||||
|
||||
count += len(result[first_type]['items'])
|
||||
if total and count >= total:
|
||||
break
|
||||
if total and limit > total - count:
|
||||
# when approaching `total` results, adjust `limit` to not request more
|
||||
# items than needed
|
||||
limit = total - count
|
||||
return results
|
||||
|
||||
return results
|
||||
Reference in new issue
Block a user