2020/08/25/The Plex API

From Woozle Writes Code
Revision as of 22:00, 25 August 2020 by Woozle (talk | contribs) (Created page with "==Background== We've been watching a lot lately, here at Hypertwin Manor, using {{l/htyp|Plex}} -- a web service which lets individuals offer libraries of video material for r...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Background

We've been watching a lot lately, here at Hypertwin Manor, using Plex -- a web service which lets individuals offer libraries of video material for remote streaming.

One recurring problem has been that the bandwidth between our network and the remote server is, somehow, not adequate, and videos will freeze and "buffer" (wait for a certain amount of material to be received before attempting to resume playing) for anywhere between seconds and minutes at a time. More than once, we've ended up taking a whole hour to watch a half-hour video, or abandoning the session altogether because watching video in 2-second increments with 5-minute gaps just isn't very enjoyable.

The obvious solution is to download the next video beforehand -- but Plex's web interface, at least, does not offer this option.

So I've been trying to figure out how to talk to the Plex server directly from code, so that I can download things ahead of time. (Also, their UI is kind of awful in a lot of ways -- even now that we're using a laptop instead of a Roku, which was omgterrible -- and it would be nice not to have to deal with that.)

The API is, however, not officially documented, so I'm having to rely on other people's reverse-engineering -- which is often incomplete, not well explained, and may be out of date.

Progress So Far

Using Arcanemagus's API documentation, I've been able to log in with our account credentials and obtain an authorization token. Yay! Basically you send a bunch of specific X-Plex- data to https://plex.tv/users/sign_in.xml via POST, and you get back a wodge of XML which includes the auth code.

The problem now is, how do I use that token to get information about what's available?

According to Arcanemagus, there are then a dozen or so URLs at which further information can be obtained if one is logged on ("Must be logged in (web/cookie) or pass basic auth") -- but the docs don't say what information you need to pass in order to establish that you're logged in, much less how to do so. Presumably the auth code should be used -- but what's the key name? Should it be passed via POST, GET, cookie? Is anything else needed?

So I tried a few obvious things on the first item in the list, https://plex.tv/devices.xml -- but I always get an HTTP 404 error.

As well as Arcanemagus's links, I have several examples of code to look at:

  • Plex-API, a PHP wrapper
  • plexdownloader, a Python interactive script
    • This works until I get to the point of telling it to start downloading, and then it crashes.
    • ...and loses everything I've entered, so I have to start over in order to try again.

writing in progress