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

Web API playlists v2 #228

Closed

Conversation

kingosticks
Copy link
Member

@kingosticks kingosticks commented Sep 12, 2019

This branch is the successor to #188 and aimed at fixing #140 and #182.

  • Cache playlist API response data
  • Support Spotify's new playlist URI scheme
  • Save cached data to file on exit and restore on login - not in this PR
  • Translator should use @memoized decorator?
  • Track translators need to consider market availability
  • Tests
  • Handle playlists refresh

I'm not happy with returning the raw (mutable) cache data dict to SpotifyOAuthClient and this led to the hacky workaround at https://github.com/kingosticks/mopidy-spotify/blob/fix/web-api-playlists-v2/mopidy_spotify/web.py#L440-L443. Returning something immutable like a namedtuple might be better.

Cache playlist web API responses in a simple dict.

playlists: Support Spotify's new playlist URI scheme (Fixes mopidy#215).

search: uses 'from_token' market.
Spotify Track relinking guide says must use the original uri for any playlist manipulation etc
so we should return that when translating to Mopidy models. libspotfy will handle any relinking
when track is loaded for playback.

Re-use track ref logic to extract correct URI.

Added valid_web_data helper for checking common web object fields.
Block attemps to get user's playlists until first refresh completes.
Temporarily increased timing logging verbosity.

Using this as a baseline for optimisations where we:
1. Start Mopidy
2. Get the playlists (mpc lsplaylists)
3. Load a playlist of 454 songs (mpc load X).

1. ~7 seconds
INFO     2019-09-12 23:00:21,985 Logged into Spotify Web API as kingosticks
INFO     2019-09-12 23:00:22,346 playlists.as_list() took 360ms
INFO     2019-09-12 23:00:28,830 Refreshed 66 playlists
INFO     2019-09-12 23:00:28,830 Refresh Playlists took 6845ms
INFO     2019-09-12 23:00:28,831 Starting Mopidy core

2. real	0m0.539s
INFO     2019-09-12 23:01:03,461 playlists.as_list() took 270ms
INFO     2019-09-12 23:01:03,641 playlists.as_list() took 176ms

2. real	0m0.202s
INFO     2019-09-12 23:01:16,267 playlists.as_list() took 174ms

3. real	0m8.926s

3. real	0m0.410s

1. Cold cache.
2. Cache expired.
3. because libspotify has to load the all the track data.
Even once we have all playlist data from the Web API, any *track* lookups are done
through libspotify which do a full load of the data. Clients may try
to lookup all tracks in a playlist and this is therefore now very slow for large
playlists. Previously, libspotify would start loading playlist track data in the
background on first access to the playlist_container; this meant that any subsequent track
lookups we did were fast. libspotify will do this background loading for any
object once it has a Link to it so we can recreate this optimisation by simply grabbing
libspotfy Link objects for all playlist track URIs we got from the Web API.
We do need to maintain a reference to all these libspotify Links else they'll be freed
and the track data will not be loaded.

1. ~7 seconds
INFO     2019-09-12 23:07:28,755 Logged into Spotify Web API as kingosticks
INFO     2019-09-12 23:07:35,521 Refreshed 66 playlists
INFO     2019-09-12 23:07:35,521 Refresh Playlists took 6765ms
INFO     2019-09-12 23:07:35,521 Starting Mopidy core

2. real	0m0.356s
INFO     2019-09-12 23:07:55,680 playlists.as_list() took 143ms
INFO     2019-09-12 23:07:55,830 playlists.as_list() took 147ms

2. real	0m0.167s
INFO     2019-09-12 23:08:06,595 playlists.as_list() took 145ms

3. real	0m0.467s

3. real	0m0.399s

1. Unchanged
2. Unchanged
3. Big speedup for first call
Workaround for Spotify's use of max-age=0 for the me/playlists endpoint.

1. ~7 seconds
INFO     2019-09-12 23:09:16,752 Logged into Spotify Web API as kingosticks
INFO     2019-09-12 23:09:23,377 Refreshed 66 playlists
INFO     2019-09-12 23:09:23,377 Refresh Playlists took 6625ms
INFO     2019-09-12 23:09:23,377 Starting Mopidy core

2. real	0m0.194s
INFO     2019-09-12 23:09:52,968 playlists.as_list() took 145ms
INFO     2019-09-12 23:09:52,974 playlists.as_list() took 4ms

2. real	0m0.033s
INFO     2019-09-12 23:09:54,108 playlists.as_list() took 13ms

3. real	0m0.353s

3. real	0m0.119s

1. Unchanged
2. Speedup for subsequent call
3. As above
@kingosticks kingosticks added the C-bug Category: This is a bug label Sep 12, 2019
@kingosticks kingosticks mentioned this pull request Sep 12, 2019
6 tasks
@jodal
Copy link
Member

jodal commented Sep 13, 2019

❤️

@kirek007
Copy link

kirek007 commented Oct 30, 2019

Hi, I've tested your changes yesterday. You've done good job there @kingosticks! Do you need help with last fixes? I'd love to help merge it asap just tell me where are bugs :)

@jodal jodal closed this Nov 18, 2019
@jodal
Copy link
Member

jodal commented Nov 18, 2019

To simplify a bit, I removed the develop branch in preference for just using a single master branch. That had the unintentional consequence of closing all PRs which targeted the develop branch.

For this PR, that shouldn't be a big problem, as I know @kingosticks intended to rebase this PR on the now Python 3 compatible master branch anyway. Now he'll just have to make yet another successor PR 😇

@kingosticks
Copy link
Member Author

If I re-write this code enough times, chances are I'll get it right at some point. Maybe.

@kirek007
Copy link

@kingosticks, tell me if you need more hands on board ;)

@kingosticks
Copy link
Member Author

I'll port the branch tonight, after which you'll need the latest (and very greatest) pre-release version of Mopidy 3 in order to test it.

@kingosticks kingosticks mentioned this pull request Dec 3, 2019
8 tasks
@kingosticks kingosticks deleted the fix/web-api-playlists-v2 branch January 6, 2021 01:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants