Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to search multiple markets at once. #526

Merged
merged 7 commits into from Jul 3, 2020
Merged

Add support to search multiple markets at once. #526

merged 7 commits into from Jul 3, 2020

Conversation

nleroy917
Copy link
Contributor

Resolve issue #525

Overview

  • Support to search multiple markets at once by passing in a list.
  • Support to search all available Spotify markets.
  • Added two unit tests to test the new feature.

Detailed

I reformatted the API interface search method. I did notice that all but one method directly returned the result that comes from the self._get call, and I wanted to keep this to retain any abstraction, but I couldn't think of a way around it. The new search method on the Spotify client keeps all original functionality, but I added in some logic to handle if a user passes in a list of markets to search through. In addition, as referenced in #525, I added in the ability to search ALL markets as well as another available parameter n that will stop the searching once a given number of results are found.

I really tried to keep the object structure returned from Spotify... So, the final object returned from search when multiple markets are passed in will be almost identical to the object returned if one market or no markets are passed in.

To the best of my knowledge, it is the same, except href now results in a list of hrefs used for searching.

Performance Issues

As mentioned by @stephanebruckert, this code performs poorly due to looping through http requests, but I can not think of a way around it.

Copy link
Member

@stephanebruckert stephanebruckert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added minor comments

spotipy/client.py Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
spotipy/client.py Outdated Show resolved Hide resolved
tests/integration/test_non_user_endpoints.py Outdated Show resolved Hide resolved
tests/integration/test_non_user_endpoints.py Outdated Show resolved Hide resolved
tests/integration/test_non_user_endpoints.py Show resolved Hide resolved
spotipy/client.py Show resolved Hide resolved
spotipy/client.py Outdated Show resolved Hide resolved
spotipy/client.py Outdated Show resolved Hide resolved
@stephanebruckert stephanebruckert linked an issue Jul 3, 2020 that may be closed by this pull request
@nleroy917
Copy link
Contributor Author

Will adjust formatting to try and resolve those checks in just a bit - local flake8 didn’t find anything

results[type + 's']['items'] += result[type + 's']['items']
results[type + 's']['total'] += result[type + 's']['total']
if total and len(results[type + 's']['items']) >= total:
# sice 'items' to only include number of results requested
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: slice*?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Will fix that one!

Copy link
Member

@stephanebruckert stephanebruckert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks a lot for the PR and fixes

@stephanebruckert
Copy link
Member

Do you have any improvement idea on this maybe @ritiek? Otherwise really happy to merge

spotipy/client.py Outdated Show resolved Hide resolved
spotipy/client.py Outdated Show resolved Hide resolved
spotipy/client.py Outdated Show resolved Hide resolved
spotipy/client.py Outdated Show resolved Hide resolved
@stephanebruckert stephanebruckert merged commit 2bfa7e0 into spotipy-dev:master Jul 3, 2020
@nleroy917 nleroy917 deleted the market-search branch July 3, 2020 18:35
Comment on lines +1582 to +1588
results[type + 's']['href'].append(result[type + 's']['href'])
results[type + 's']['items'] += result[type + 's']['items']
results[type + 's']['total'] += result[type + 's']['total']
if total and len(results[type + 's']['items']) >= total:
# splice 'items' to only include number of results requested
results[type + 's']['items'] = results[type + 's']['items'][:total]
return results
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems weird to me here that we're messing with the API response returned by Spotify. This could be confusing at times.

I think a better choice would be to have a key for each market in the result. This way we do not mess with the internal response returned by Spotify. The results would then look something like below on using sp.search:

>>> result = sp.search("lany good guys", market=["US", "GB"])
>>> us_tracks = result["US"]["tracks"]
>>> us_tracks.keys()
dict_keys(['href', 'items', 'limit', 'next', 'offset', 'previous', 'total'])

>>> us_tracks["href"]
'https://api.spotify.com/v1/search?query=lany+good+guys&type=track&market=US&offset=0&limit=10'

>>> gb_tracks = result["GB"]["tracks"]
>>> gb_tracks["href"]
'https://api.spotify.com/v1/search?query=lany+good+guys&type=track&market=GB&offset=0&limit=10'

@stephanebruckert what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I completely agree - It feels hacky. I was actually thinking of implementing the market key idea you mention as I was working on it, but I wanted to try and preserve the format of the response from Spotify.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I merged this too quickly. I agree, it does feel weird. Now that you point it out it's true that mixing up results does not make sense, mainly due to the fact that results are supposed to be ordered by "relevancy".

The reason I thought it was OK in the first place is that in many cases, we are only interested in the first result. In this specific case, getting a dictionary of all countries can make it difficult to find in which country the release was found.

But it's probably nicer to let developers handle country results the way they want, since we don't really know what their use case is. So I would be all for this change

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephanebruckert It's all good. Thank you for maintaining spotipy all this while! I agree that devs should be able to do whatever they want with the country results later on.

@nleroy917 Are you willing to make a new PR for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, definitely - I can work on that

stephanebruckert added a commit that referenced this pull request Jul 7, 2020
* resolve return object formatting issues referened in #526

* resolve comments from @ritiek

* create separate method to search multiple markets

* Use old description again

* market -> markets + fix test

* pep8

* Use break

Co-authored-by: Stephane Bruckert <contact@stephanebruckert.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Search through all markets at once
3 participants