Plex - Parsing Now Playing Status

Print
Category: Techtips
Published Date Written by Administrator

I was working on my Home Automation project, adding a status panel for all playing media in the home. I had originally kept this to myself, but came across an IRC user looking to do the same. I figured I would share my findings.

 

First, we need to have the URL to pull the Plex information.

https://support.plex.tv/hc/en-us/articles/204059436-Finding-your-account-token-X-Plex-Token

You need your Plex Account Token for this to work outside of your network (or inside while not authenticated)

Once you have your token you will be able to pull your session status from Plex with the following

https://yourplexserver:yourport/status/sessions?X-Plex-Token=YOURTOKENVALUEHERE

The out put will be in XML format.

I wrote a quick simple VBS to parse the data from there and spit it back into a basic text file for my home automation app to pick up.

Again, this is a quick and dirty VBS script, but it does what I need.


 

Dim oStream

Dim objHttp

 

Dim plex_titletype_reg, plex_titletype_col

Dim plex_username_reg, plex_username_col

Dim plex_gptitle_reg, plex_gptitle_col

 

Set plex_titletype_reg = New RegExp

With plex_titletype_reg

.Pattern = "title=\""(.*?)\"" type=\""(.*?)\"""

.Global = True

.IgnoreCase = False

End With

 

Set plex_username_reg = New RegExp

With plex_username_reg

.Pattern = "User id.*title=\""(.*?)\"""

.Global = True

.IgnoreCase = False

End With

 

Set plex_gptitle_reg = New RegExp

With plex_gptitle_reg

.Pattern = "grandparentTitle=\""(.*?)\"""

.Global = True

.IgnoreCase = False

End With

 

set objHttp = CreateObject("Msxml2.ServerXMLHTTP")

objHttp.open "GET", "http://server:port/status/sessions", False

objHttp.send 

 

 

If objHttp.Status = 200 Then

Set plex_titletype_col = plex_titletype_reg.Execute(objHttp.responseText)

Set plex_username_col = plex_username_reg.Execute(objHttp.responseText)

Set plex_gptitle_col = plex_gptitle_reg.Execute(objHttp.responseText)

 

 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set sr = objFSO.OpenTextFile( "C:\Status\plex_titletype.sts", 2, true, 0 )

 

For each objMatch in plex_titletype_col

sr.WriteLine(objMatch.Value)

Next

sr.Close()

 

Set sr = objFSO.OpenTextFile( "C:\Status\plex_username.sts", 2, true, 0 )

For each objMatch in plex_username_col

sr.WriteLine(objMatch.Value)

Next

sr.Close()

 

Set sr = objFSO.OpenTextFile( "C:\Status\plex_gptitle.sts", 2, true, 0 )

For each objMatch in plex_gptitle_col

sr.WriteLine(objMatch.Value)

Next

sr.Close()

 

End If


 

 

The end result is 3 files, each with different information. 

Depending if it is an MP3 or TV show you will have a GrandParent title, no the case with a movie.

Example of an mp3 output

 

grandparentTitle="Lettuce"

title="Squadlive" type="track"

User id="3511930" title="gle453"

 


 

UPDATE

For those sqlite fans, a friend of mine in IRC provided me with this to pull the same data.

sqlite3 /opt/plexpy/plexpy.db "SELECT friendly_name, state, grandparent_title, title from sessions"

Amount: