Forked from JonnyWong16/exec_command_no_sessions.py
Created
December 19, 2017 22:48
-
-
Save zmike808/a879a90c75c6ce05ef756b077dd18100 to your computer and use it in GitHub Desktop.
Execute a command when no Plex sessions are active.
This file contains hidden or 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