Tyzer34 / plexMusicPlayer

Playback music with Alexa from your Plex Server!
https://medium.com/@Tyzer34/plex-alexa-the-perfect-wedding-38b14b41faf0#.b71cd6lsn
GNU General Public License v3.0
35 stars 19 forks source link

Better handling of requests that fetch large amounts of tracks #13

Open jamesshaw1987 opened 7 years ago

jamesshaw1987 commented 7 years ago

This is something that only really applies when trying to play everything by an artist for whom you have a lot of albums/tracks.

If, for example, you have 20 albums by an artist, each containing 10 tracks, you'd have to make 221 requests (1 for the artist, 20 for the albums and 200 for the tracks) to Plex when using the "play artist" intent. This can obviously take a while to conplete, often longer than Alexa allows for the response.

Not sure of the best approach for handling this and I don't now Python well enough to know the options available. Add the first album to the queue and fetch the rest asynchronously maybe? Or pull back, say, 100 tracks and have some placeholder at the end of the queue telling it to fetch the next batch when it gets to that point? Or maybe just limit each request to ~100 tracks and let the user know that they'll have to be more specific to play others?

Tyzer34 commented 7 years ago

I can understand, as the response time is indeed only 10 seconds. Sometimes, when the local internet is slow, it doesn't even take a 100+ track request to timeout the response. The best way to go about this, may be indeed to globally do the queue adding using an external thread and returning the first available song. This way, a timeout should be avoided while still allowing the queue to be filled up. Problems with multithreading may occur, but normally, the queue should be thread safe.

jamesshaw1987 commented 7 years ago

Haven't looked at putting a proper solution for this, though I have improved things on my setup. I've moved away from using Heroku and am instead just hosting locally (using ngrok to expose the server for now). And what that means is that I can then use the local Plex URL for searching and performing artist/album lookups. Obviously the external Plex URL is still needed to play tracks as the request for that is coming from the Alexa servers. And if the local URL is set on the environment it'll fall back to using the external URL for searching//lookups, which means the code will still work when hosted on Heroku/anywhere external.

So as a result a request that returns 200+ tracks is now completing in ~5s, which covers my needs for now. I can make a pull request for the changes if you like, nothing much to it and it just requires an extra environment variable to be set.

Tyzer34 commented 7 years ago

I have also thought of that solution, but, many people will not be able to do this at home. If you would make it so that, if the extra environmental variable isn't set, that it will still work, you should push it. This is just to introduce a little backwards compatibility :)