Steven Smith revised this gist . Go to revision
1 file changed, 13 insertions, 6 deletions
plexnp.py
| @@ -1,28 +1,35 @@ | |||
| 1 | 1 | from plexapi.server import PlexServer | |
| 2 | 2 | import sys | |
| 3 | - | plex = PlexServer() | |
| 3 | + | plex = PlexServer("http://172.16.17.10:32400") | |
| 4 | 4 | now_playing = plex.query("/status/sessions") | |
| 5 | 5 | if not now_playing: | |
| 6 | + | print("Nobody's watching anything") | |
| 6 | 7 | sys.exit(0) | |
| 7 | 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 | |
| 8 | 14 | for item in now_playing: | |
| 9 | 15 | data = { | |
| 10 | 16 | "title": item.attrib["title"], | |
| 11 | 17 | "user": item.find(".//User").attrib["title"], | |
| 12 | 18 | "product": ("on a " + item.find(".//Player").attrib["device"]) if | |
| 13 | 19 | item.find(".//Session").attrib["location"] == "cellular" else | |
| 14 | - | ("using " + item.find(".//Player").attrib["product"]) | |
| 20 | + | ("using " + item.find(".//Player").attrib["product"]), | |
| 21 | + | "offset": "{}:{:02d}:{:02d}.{:03d}".format(*hms(item.attrib["viewOffset"])) | |
| 15 | 22 | } | |
| 16 | 23 | if item.tag == "Track": | |
| 17 | - | data.update({"artist": item.attrib["originalTitle"]}) | |
| 18 | - | sessions.append("{user} is listening to {title} by {artist} {product}".format(**data)) | |
| 24 | + | data.update({"artist": item.attrib["grandparentTitle"]}) | |
| 25 | + | sessions.append("{user} is listening to {title} by {artist} {product} ({offset})".format(**data)) | |
| 19 | 26 | elif item.tag == "Video": | |
| 20 | 27 | data.update({"year": item.attrib["year"]}) | |
| 21 | 28 | if "grandparentTitle" in item.attrib: | |
| 22 | 29 | data.update({"show": item.attrib["grandparentTitle"]}) | |
| 23 | - | sessions.append("{user} is watching {show}: {title} {product}".format(**data)) | |
| 30 | + | sessions.append("{user} is watching {show}: {title} {product} ({offset})".format(**data)) | |
| 24 | 31 | else: | |
| 25 | - | sessions.append("{user} is watching {title} ({year}) {product}".format(**data)) | |
| 32 | + | sessions.append("{user} is watching {title} ({year}) {product} ({offset})".format(**data)) | |
| 26 | 33 | else: | |
| 27 | 34 | continue | |
| 28 | 35 | if len(sessions) > 2: | |
Steven Smith revised this gist . Go to revision
1 file changed, 31 insertions
plexnp.py(file created)
| @@ -0,0 +1,31 @@ | |||
| 1 | + | from plexapi.server import PlexServer | |
| 2 | + | import sys | |
| 3 | + | plex = PlexServer() | |
| 4 | + | now_playing = plex.query("/status/sessions") | |
| 5 | + | if not now_playing: | |
| 6 | + | sys.exit(0) | |
| 7 | + | sessions = [] | |
| 8 | + | for 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 | |
| 28 | + | if len(sessions) > 2: | |
| 29 | + | print(", ".join(sessions[:-1]) + ", and " + sessions[-1]) | |
| 30 | + | else: | |
| 31 | + | print(", and ".join(sessions)) | |