plexnp.py
· 1.5 KiB · Python
Raw
from plexapi.server import PlexServer
import sys
plex = PlexServer("http://172.16.17.10:32400")
now_playing = plex.query("/status/sessions")
if not now_playing:
print("Nobody's watching anything")
sys.exit(0)
sessions = []
def hms(mil):
s,mil = divmod(int(mil), 1000)
m,s = divmod(s, 60)
h,m = divmod(m, 60)
return h,m,s,mil
for item in now_playing:
data = {
"title": item.attrib["title"],
"user": item.find(".//User").attrib["title"],
"product": ("on a " + item.find(".//Player").attrib["device"]) if
item.find(".//Session").attrib["location"] == "cellular" else
("using " + item.find(".//Player").attrib["product"]),
"offset": "{}:{:02d}:{:02d}.{:03d}".format(*hms(item.attrib["viewOffset"]))
}
if item.tag == "Track":
data.update({"artist": item.attrib["grandparentTitle"]})
sessions.append("{user} is listening to {title} by {artist} {product} ({offset})".format(**data))
elif item.tag == "Video":
data.update({"year": item.attrib["year"]})
if "grandparentTitle" in item.attrib:
data.update({"show": item.attrib["grandparentTitle"]})
sessions.append("{user} is watching {show}: {title} {product} ({offset})".format(**data))
else:
sessions.append("{user} is watching {title} ({year}) {product} ({offset})".format(**data))
else:
continue
if len(sessions) > 2:
print(", ".join(sessions[:-1]) + ", and " + sessions[-1])
else:
print(", and ".join(sessions))
| 1 | from plexapi.server import PlexServer |
| 2 | import sys |
| 3 | plex = PlexServer("http://172.16.17.10:32400") |
| 4 | now_playing = plex.query("/status/sessions") |
| 5 | if not now_playing: |
| 6 | print("Nobody's watching anything") |
| 7 | sys.exit(0) |
| 8 | sessions = [] |
| 9 | def hms(mil): |
| 10 | s,mil = divmod(int(mil), 1000) |
| 11 | m,s = divmod(s, 60) |
| 12 | h,m = divmod(m, 60) |
| 13 | return h,m,s,mil |
| 14 | for item in now_playing: |
| 15 | data = { |
| 16 | "title": item.attrib["title"], |
| 17 | "user": item.find(".//User").attrib["title"], |
| 18 | "product": ("on a " + item.find(".//Player").attrib["device"]) if |
| 19 | item.find(".//Session").attrib["location"] == "cellular" else |
| 20 | ("using " + item.find(".//Player").attrib["product"]), |
| 21 | "offset": "{}:{:02d}:{:02d}.{:03d}".format(*hms(item.attrib["viewOffset"])) |
| 22 | } |
| 23 | if item.tag == "Track": |
| 24 | data.update({"artist": item.attrib["grandparentTitle"]}) |
| 25 | sessions.append("{user} is listening to {title} by {artist} {product} ({offset})".format(**data)) |
| 26 | elif item.tag == "Video": |
| 27 | data.update({"year": item.attrib["year"]}) |
| 28 | if "grandparentTitle" in item.attrib: |
| 29 | data.update({"show": item.attrib["grandparentTitle"]}) |
| 30 | sessions.append("{user} is watching {show}: {title} {product} ({offset})".format(**data)) |
| 31 | else: |
| 32 | sessions.append("{user} is watching {title} ({year}) {product} ({offset})".format(**data)) |
| 33 | else: |
| 34 | continue |
| 35 | if len(sessions) > 2: |
| 36 | print(", ".join(sessions[:-1]) + ", and " + sessions[-1]) |
| 37 | else: |
| 38 | print(", and ".join(sessions)) |