Skip to content

Instantly share code, notes, and snippets.

@JonnyWong16
Created March 5, 2017 20:02
Show Gist options
  • Save JonnyWong16/bc50c882985cc495e629f41c12bc7590 to your computer and use it in GitHub Desktop.
Save JonnyWong16/bc50c882985cc495e629f41c12bc7590 to your computer and use it in GitHub Desktop.
Execute a command when no Plex sessions are active.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Description: Execute a command when no Plex sessions are active.
# Author: /u/SwiftPanda16
# Requires: plexapi
import shlex
import subprocess
from plexapi.server import PlexServer
### EDIT SETTINGS ###
PLEX_URL = 'http://localhost:32400'
PLEX_TOKEN = 'xxxxxxxxxx'
COMMAND = ''
### CODE BELOW ###
def main():
try:
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
except:
print("No Plex server found at '{base_url}', or invalid token.".format(base_url=PLEX_URL))
print("Exiting script.")
return
args = shlex.split(COMMAND)
sessions = plex.sessions()
if not sessions:
print("No active sessions.")
print("Executing command: {cmd}".format(cmd=COMMAND))
subprocess.call(args)
else:
print("Sessions active ({count}).".format(count=len(sessions)))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment