How to start a Plex stream using python-PlexAPI?

Question:

I’m making a python script using the module PlexAPI. My goal is to start a stream on the client Chrome. The movie to view has the key /library/metadata/1.

Documentation and sources of code:

from plexapi.server import PlexServer
baseurl = 'http://xxx.xxx.xxx.xxx:xxxxx'
token = 'xxxxx'
plex = PlexServer(baseurl, token)

client = plex.client("Chrome")
client.playMedia(key="/library/metadata/1")

This gives the following error:

Traceback (most recent call last):
  File "start_stream.py", line 7, in <module>
    client.playMedia(key="/library/metadata/1")
TypeError: playMedia() missing 1 required positional argument: 'media'

So I edit the file:

client.playMedia(key="/library/metadata/1")
#changed to
client.playMedia(key="/library/metadata/1", media="movie")

But then I get a different error:

Traceback (most recent call last):
  File "start_stream.py", line 7, in <module>
    client.playMedia(key="/library/metadata/1", media="movie")
  File "/usr/local/lib/python3.8/dist-packages/plexapi/client.py", line 497, in playMedia
    server_url = media._server._baseurl.split(':')
AttributeError: 'str' object has no attribute '_server'

I don’t really understand what’s going on. Can someone help?

Asked By: Cas

||

Answers:

I was able to make it work the following way:

from plexapi.server import PlexServer
baseurl = 'http://xxx.xxx.xxx.xxx:xxxxx'
token = 'xxxxx'
plex = PlexServer(baseurl, token)

key = '/library/metadata/1'

client = plex.client("Chrome")
media = plex.fetchItem(key)
client.playMedia(media)
Answered By: Cas