Created
March 5, 2017 20:02
-
-
Save JonnyWong16/bc50c882985cc495e629f41c12bc7590 to your computer and use it in GitHub Desktop.
Execute a command when no Plex sessions are active.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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