Last active 1670065441

A script to report what people are watching on your Plex server

Revision ef4665e9df348efe9ff8dc4fb8321d2712cb062e

plexnp.py Raw
1from plexapi.server import PlexServer
2import sys
3plex = PlexServer()
4now_playing = plex.query("/status/sessions")
5if not now_playing:
6 sys.exit(0)
7sessions = []
8for item in now_playing:
9 data = {
10 "title": item.attrib["title"],
11 "user": item.find(".//User").attrib["title"],
12 "product": ("on a " + item.find(".//Player").attrib["device"]) if
13 item.find(".//Session").attrib["location"] == "cellular" else
14 ("using " + item.find(".//Player").attrib["product"])
15 }
16 if item.tag == "Track":
17 data.update({"artist": item.attrib["originalTitle"]})
18 sessions.append("{user} is listening to {title} by {artist} {product}".format(**data))
19 elif item.tag == "Video":
20 data.update({"year": item.attrib["year"]})
21 if "grandparentTitle" in item.attrib:
22 data.update({"show": item.attrib["grandparentTitle"]})
23 sessions.append("{user} is watching {show}: {title} {product}".format(**data))
24 else:
25 sessions.append("{user} is watching {title} ({year}) {product}".format(**data))
26 else:
27 continue
28if len(sessions) > 2:
29 print(", ".join(sessions[:-1]) + ", and " + sessions[-1])
30else:
31 print(", and ".join(sessions))