plexnp.py
                        
                             · 1.2 KiB · Python
                        
                    
                    
                      
                        Raw
                      
                      
                        
                          
                        
                    
                    
                
                
            from plexapi.server import PlexServer
import sys
plex = PlexServer()
now_playing = plex.query("/status/sessions")
if not now_playing:
    sys.exit(0)
sessions = []
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"])
    }
    if item.tag == "Track":
        data.update({"artist": item.attrib["originalTitle"]})
        sessions.append("{user} is listening to {title} by {artist} {product}".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}".format(**data))
        else:
            sessions.append("{user} is watching {title} ({year}) {product}".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() | 
| 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)) |