Plex Status

Python and python framework

Moderator: leecollings

User avatar
galadril
Posts: 824
Joined: Monday 07 September 2015 10:32
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Contact:

Plex Status

Post by galadril »

But with these script, you get the status of your Plex Media Server

Prepare Domoticz
If you don't know how to create a virtual sensor chances are you have never done this before.
- Go to "Hardware"
- Fill in the name field with a desired name (like "Virtual")
- Choose for type the "Dummy, does nothing, use for virtual switches only"
- Click on "Add"
- The added hardware is now added to the hardware list. In the same row there is a "Create virtual sensor" button shown. Click on it.


Hardware
Now you are ready to create virtual devices.
- Go to "Hardware"
- Press "Create virtual sensor".
- Fill in the name field with a name like 'Plex'


Devices
Now you are ready to create a virtual device.
- Go to "Hardware"
- Press "Create virtual sensor".
- Fill in the name field with a name like 'Plex'


Plex script
Config the parameters in the Plex script:

Code: Select all

#config plex url
plexURL 			= '<plex ip>:<plex port>' #base url (default port = 32400)
	
#domoticz settings
domoticz_host      	= ''
domoticz_port      	= ''
domoticz_url       	= 'json.htm'

#domoticz idx
domoticz_PlexStatus = '' #idx of device

Put the script under /domoticz/scripts and make the script executable:
"sudo chmod +x /home/pi/domoticz/scripts/plex.py"


Test
Run the script by calling:
"sudo python /home/pi/domoticz/scripts/plex.py"

Image

If Plex Server is not playing anything, this is the result:

Image


Create the Cronjob
Now all ingredients are available to read the data from the portal and insert them into Domoticz.
- execute "sudo crontab -e"
- add the following line "*/10 * * * * /home/pi/domoticz/scripts/plex.py"
- exit crontab. Now everything should be working for you.

Scripts
plex.py

Code: Select all


#!/usr/bin/python

import urllib, urllib2, hashlib
from xml.etree import ElementTree as ET

#config plex url
plexURL 			= '<plex ip>:<plex port>' #base url (default port = 32400)
	
#domoticz settings
domoticz_host      	= ''
domoticz_port      	= ''
domoticz_url       	= 'json.htm'

#domoticz idx
domoticz_PlexStatus = '' #idx of device

#building url
requestURL = 'http://'+plexURL+'/status/sessions'
print requestURL


try:
  #call session url
  test = urllib.urlopen(requestURL).read()
  print test

  root = ET.XML(test)
  print root.tag

  videotitle = root.find('Video').attrib['title']
	
  print 'Video Playing: ' + videotitle
  urllib.urlopen("http://" + domoticz_host + ":" + domoticz_port + "/" + domoticz_url+ "?type=command&param=addlogmessage&message=Video Playing: " + videotitle)
	
  #uploading values to domoticz
  url = ("http://" + domoticz_host + ":" + domoticz_port + "/" + domoticz_url+ "?type=command&param=udevice&idx=" + domoticz_PlexStatus + "&nvalue=0&svalue=Playing: " + videotitle)
  urllib.urlopen(url)
	
except Exception: 
  print 'IDLE'
  #uploading values to domoticz
  url = ("http://" + domoticz_host + ":" + domoticz_port + "/" + domoticz_url+ "?type=command&param=udevice&idx=" + domoticz_PlexStatus + "&nvalue=0&svalue=IDLE")
  urllib.urlopen(url)
  pass

Solar panels of Ginlong, Omnik-Solar, Transenergy or Solarman?? Try my Android app:
https://play.google.com/store/apps/deta ... ongmonitor
MAidEN
Posts: 17
Joined: Sunday 07 December 2014 15:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Plex Status

Post by MAidEN »

Hi,

I have a problem with this script. My plex server is on NAS Synology.

When I request 'http://'+plexURL+'/status/sessions' I have this return :

Code: Select all

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<MediaContainer size="0"></MediaContainer>
I tried to change url like (this URL return live activity on my plex server) :
requestURL = 'http://'+plexURL+'/web/index.html#!/activity/playing'

But the script doesn't work :

Code: Select all

sudo python plex.py
http://192.168.0.249:32400/web/index.html#!/activity/playing
<html><head><title>Not Found</title></head><body><h1>404 Not Found</h1></body></html>
html
IDLE
Can you help me? :roll:
JoshDinsdale
Posts: 29
Joined: Monday 24 February 2014 14:31
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Plex Status

Post by JoshDinsdale »

Any Chance you could update this to support authorization for those of us that use PlexHome?

Many Thanks.
atomicpapa
Posts: 1
Joined: Tuesday 03 November 2015 15:05
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Plex Status

Post by atomicpapa »

I'm thinking authorization may be my issue as well. When I run the test on the script, I receive the following error:

http://192.168.0.6:32400/status/sessions
IDLE
Traceback (most recent call last):
File "plex.py", line 43, in <module>
urllib.urlopen(url)
File "/usr/lib/python2.7/urllib.py", line 86, in urlopen
return opener.open(url)
File "/usr/lib/python2.7/urllib.py", line 207, in open
return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py", line 358, in open_http
return self.http_error(url, fp, errcode, errmsg, headers)
File "/usr/lib/python2.7/urllib.py", line 371, in http_error
result = method(url, fp, errcode, errmsg, headers)
File "/usr/lib/python2.7/urllib.py", line 682, in http_error_401
errcode, errmsg, headers)
File "/usr/lib/python2.7/urllib.py", line 380, in http_error_default
raise IOError, ('http error', errcode, errmsg, headers)
IOError: ('http error', 401, 'Unauthorized', <httplib.HTTPMessage instance at 0x768597d8>)


That last line pretty much says it all. Is there a way around this?
racquemis
Posts: 73
Joined: Monday 02 November 2015 18:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: Plex Status

Post by racquemis »

Try this
Go to your plex media server settings. On the Network page find the settings that says "List of networks that are allowed without auth " and enter the ip address and subnet of the device running the script. Gateway IP could possiby work aswell.
Enter something Like: 192.168.0.1/255.255.255.0

As soon as you saved the settings and restarted the server only the specified devices/networks can access the plex server without authentication
j0ystick
Posts: 2
Joined: Monday 19 October 2015 8:12
Target OS: Linux
Domoticz version:
Contact:

Re: Plex Status

Post by j0ystick »

Works nicely also on a linux install from current git (not on rpi), same host for plex server and domoticz (IPs both set to 127.0.0.1).
Thanks! :)
trollmar
Posts: 16
Joined: Friday 24 April 2015 9:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Plex Status

Post by trollmar »

Nice THX!!!
Works on RPi2!!

Maybe in future we could change the Icon for utilitys (Text Objects)!!
zotya
Posts: 1
Joined: Sunday 06 December 2015 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Plex Status

Post by zotya »

A very similar script written in PHP

https://github.com/zoltanlaczko/domoticz-plex
gflvern
Posts: 19
Joined: Monday 29 December 2014 11:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Plex Status

Post by gflvern »

MAidEN wrote:Hi,

I have a problem with this script. My plex server is on NAS Synology.

When I request 'http://'+plexURL+'/status/sessions' I have this return :

Code: Select all

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<MediaContainer size="0"></MediaContainer>
I tried to change url like (this URL return live activity on my plex server) :
requestURL = 'http://'+plexURL+'/web/index.html#!/activity/playing'

But the script doesn't work :

Code: Select all

sudo python plex.py
http://192.168.0.249:32400/web/index.html#!/activity/playing
<html><head><title>Not Found</title></head><body><h1>404 Not Found</h1></body></html>
html
IDLE
Can you help me? :roll:
Did you get a solution to this?

When I try to access /status/sessions I get a 401 unauthroized error. I've tried changing the network setting in plex but no luck.

gfl
User avatar
nayr
Posts: 354
Joined: Tuesday 11 November 2014 18:42
Target OS: Linux
Domoticz version: github
Location: Denver, CO - USA
Contact:

Re: Plex Status

Post by nayr »

log out of myPlex on the plex server, and then restart plex server for good measure... then go change the subnet and save. should work, even after logging back into myplex.
Debian Jessie: CuBox-i4 (Primary) w/Static Routed IP and x509 / BeagleBone with OpenSprinkler / BeagleBone Planted Aquarium / 3x Raspbery Pi2b GPIO Slaves
Elemental Theme - node-domoticz-mqtt - Home Theatre Controller - AndroidTV Simple OSD Remote - x509 TLS Auth
gflvern
Posts: 19
Joined: Monday 29 December 2014 11:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Plex Status

Post by gflvern »

nayr wrote:log out of myPlex on the plex server, and then restart plex server for good measure... then go change the subnet and save. should work, even after logging back into myplex.
Thanks, eventually got it working after a couple of stop/starts!
gflvern
Posts: 19
Joined: Monday 29 December 2014 11:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Plex Status

Post by gflvern »

I was just playing with the script to show the Season name as well as the title.

I used

Code: Select all

season = root.find('Video').attrib(''grandparentTitle')
That works find for TV programmes but not movies that don't have that value. Can some point help me with how to handle that exception?

Thanks

gfl
alphabetagamma
Posts: 1
Joined: Friday 12 February 2016 11:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Plex Status

Post by alphabetagamma »

Hi all,
after some work on this scripts i can tell it works fine.
BUT
if you get
"IOError: ('http error', 401, 'Unauthorized', <httplib.HTTPMessage instance at 0x768597d8>)"
while running it ,
it appears to be a unauthorized access not from your plex server but from domoticz when updating the plex status


Quickest solution to fix it is to allow self IP connection without authentification from configuration panel.
e.g.: 127.0.0.*;0.0.0.*
In order to try your connexion launch the below test.py python script adapting your config in the setting section

Code: Select all

$ python test.py
if it is asking you for login and password you should enable your ip for no authentification.
$ python test.py
http://127.0.0.1:8080/json.htm?type=com ... value=IDLE
Enter username for Domoticz.com at 127.0.0.1:8080:
if it is returning the following you are just fine
{
"status" : "OK",
"title" : "Update Device"
}

Code: Select all

import urllib, urllib2, hashlib

#domoticz settings
domoticz_host         = '127.0.0.1'
domoticz_port         = '8080'
domoticz_url          = 'json.htm'

#domoticz idx
domoticz_PlexStatus = '4' #idx of device

url = ("http://" + domoticz_host + ":" + domoticz_port + "/" + domoticz_url+ "?type=command&param=udevice&idx=" + domoticz_PlexStatus + "&nvalue=0&svalue=IDLE")
print (url)
# uploading values to domoticz
x = urllib.urlopen(url)
#x = urllib.urlopen('https://www.google.com/')
print(x.read())

kllngtme
Posts: 47
Joined: Tuesday 14 June 2016 16:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: East Coast, USA
Contact:

Re: Plex Status

Post by kllngtme »

I'm using Windows for this. I can only seem to get the status/sessions page to load if I add my Plex token to it but even then, It's producing an error I can't seem to figure out. If I just try and browse to 192.168.1.2:32400/status/sessions it will give me 401 Unauthorized. It's only when I add the extra bit, to inlcude my plex token that it actually seems to pull the information

The output is:
C:\Program Files (x86)\Domoticz\scripts\python>plex.py
http://192.168.1.2:32400/status/session ... n=blahblah
<?xml version="1.0" encoding="UTF-8"?>
<MediaContainer size="0">
</MediaContainer>

MediaContainer
IDLE
Traceback (most recent call last):
File "C:\Program Files (x86)\Domoticz\scripts\python\plex.py", line 85, in <module>
urllib.urlopen(url)
File "C:\Python27\lib\urllib.py", line 87, in urlopen
return opener.open(url)
File "C:\Python27\lib\urllib.py", line 213, in open
return getattr(self, name)(url)
File "C:\Python27\lib\urllib.py", line 364, in open_http
return self.http_error(url, fp, errcode, errmsg, headers)
File "C:\Python27\lib\urllib.py", line 377, in http_error
result = method(url, fp, errcode, errmsg, headers)
File "C:\Python27\lib\urllib.py", line 689, in http_error_401
errcode, errmsg, headers)
File "C:\Python27\lib\urllib.py", line 386, in http_error_default
raise IOError, ('http error', errcode, errmsg, headers)
IOError: ('http error', 401, 'Unauthorized', <httplib.HTTPMessage instance at 0x0000000002C2E7C8>)

What's interesting as well is that when I had a show playing, it seemed to pull the information but still included the errors after traceback..

I've tried removing my pin to my login as well as restarting the server several times.
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Plex Status

Post by ben53252642 »

Wrote a bash script that updates a variable in Domoticz with the playing state of a specific user eg: playing, paused or idle.

It only updates the status if it doesn't already match. :D

Code: Select all

#!/bin/bash
while [ 1 ]
do
plexuser="ENTERPLEXUSERNAME"
domoticzvariablename="ENTERTHENAMEOFTHEVARIABLE"
domoticzvariableIDX="3"
plexstatus=$(curl -k -s 'https://192.168.0.4:32400/status/sessions' | sed -n "/"$plexuser"/,/title/p")
if [[ $plexstatus == *"playing"* ]]
then
status="playing"
fi
if [[ $plexstatus == *"paused"* ]]
then
status="paused"
fi
if [[ $plexstatus != *"$plexuser"* ]]
then
status="idle"
fi
# Check existing status of variable in Domoticz
variablestate=$(curl -s "http://192.168.0.5/json.htm?type=command&param=getuservariable&idx="$domoticzvariableIDX"" | grep "Value")
# Check if Plex status matches existing variable set in Domoticz
if [[ $variablestate != *"$status"* ]]
then
echo "Status changed: $status"
curl -s "http://192.168.0.5/json.htm?type=command&param=updateuservariable&vname="$domoticzvariablename"&vtype=2&vvalue="$status"" 2>/dev/null 1>/dev/null
else
echo "Status in sync"
fi
sleep 1
done
Below is a basic Lua script to act on changes to the variable

Code: Select all

commandArray = {}
if (uservariables["ENTERTHENAMEOFTHEVARIABLE"] == 'paused' and otherdevices['Bens Bed Lamp'] == 'Off') then
commandArray['Bens Bed Lamp']='On'
end
return commandArray
Attachments
s4254.png
s4254.png (11.02 KiB) Viewed 12195 times
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
q8djent
Posts: 1
Joined: Saturday 26 November 2016 22:34
Target OS: Windows
Domoticz version:
Contact:

Re: Plex Status

Post by q8djent »

racquemis wrote:Try this
Go to your plex media server settings. On the Network page find the settings that says "List of networks that are allowed without auth " and enter the ip address and subnet of the device running the script. Gateway IP could possiby work aswell.
Enter something Like: 192.168.0.1/255.255.255.0

As soon as you saved the settings and restarted the server only the specified devices/networks can access the plex server without authentication

Dude seriously been looking for a solution for days!! and yours has fixed my problem THANK YOU!!!! :)
multinet
Posts: 97
Joined: Friday 05 December 2014 22:52
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Plex Status

Post by multinet »

Hello

Since a plex update, the url of plex http:// ...... /status/sessions return a 401 unauthorized

Multinet
PI 2 - Domoticz 2021.1
RFXCOM - RFXtrx433 USB 433.92MHz Transceiver (5 DIO 54755 + 2 DIO 54756 + 3 DIO 54798)
Z-Wave.Me ZME_UZB1 USB Stick (6 FGSD002 + 2 FGRM222 + 1 FGS223 + 1 FGMS001-ZW5 + 1 FGRGBWM441 + 1 FGBS001 + 2 FGFS101)
6 sondes DS18B20
matteos1
Posts: 36
Joined: Monday 08 May 2017 17:29
Target OS: -
Domoticz version:
Contact:

Re: Plex Status

Post by matteos1 »

if domoticz have username and password?
Goldwing1973
Posts: 69
Joined: Monday 08 August 2016 22:55
Target OS: Linux
Domoticz version: Stable
Location: Netherlands, Alphen aan den Rijn
Contact:

Re: Plex Status

Post by Goldwing1973 »

Is there a easy way not to log the "Idle" State?
I would like to be put in the log when something started to play or stopped to play.
Over 50 Z-Wave Devices (Fibaro and Neo), 4xLogitech Hub, 2xPhilips Hue Bridge+55 Lights, 1x Nest Thermostat, 2xNetatmo Weather Station, 1xSkybell v1 Doorbell, 3 Kwikset KEVO Doorlocks and Domoticz running on a Intel NUC
dizzyweb
Posts: 20
Joined: Tuesday 20 June 2017 13:46
Target OS: Linux
Domoticz version: 4.10007
Contact:

Re: Plex Status

Post by dizzyweb »

Well, you want to log at least that you're not playing anything anymore. And if you don't want to repeat the values all the time, you'll have to check what the current value in domoticz is, which makes it a bit more complicated. The current script doesn't get any data from domoticz and to be honest, I don't know how to do that from a regular python script.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest