Update plex_api_parental_control.py

This commit is contained in:
blacktwin 2017-09-29 15:06:00 -04:00 committed by GitHub
parent b42b7dfe48
commit a96794ad4e

View File

@ -61,29 +61,31 @@ def share(user, libraries):
def unshare(user, libraries): def unshare(user, libraries):
plex.myPlexAccount().updateFriend(user=user, server=plex, remove_sections=True) plex.myPlexAccount().updateFriend(user=user, server=plex, removeSections=True)
print('Unshared libraries: {libraries} from {user}.'.format(libraries=libraries, user=user)) print('Unshared libraries: {libraries} from {user}.'.format(libraries=libraries, user=user))
def kill_session(user, libraries): def kill_session(user, libraries):
for session in plex.sessions(): for session in plex.sessions():
# Check for users stream and if user is watching a to be unshared library. # Check for users stream
if session.usernames[0] in user:
# If to be unshared library is not being watched then stream is not killed. # If to be unshared library is not being watched then stream is not killed.
for library in libraries: title = (session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title
if session.usernames[0] in user and session.librarySectionID != plex.library.section(library).key: for library in libraries:
title = (session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title if session.librarySectionID == plex.library.section(library).key:
print('{user} is watching {title} and it\'s past their bedtime. Killing stream.'.format( break
user=user, title=title)) print('{user} is watching {title} and it\'s past their bedtime. Killing stream.'.format(
session.stop(reason=MESSAGE) user=user, title=title))
session.stop(reason=MESSAGE)
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Share or unshare libraries.", parser = argparse.ArgumentParser(description="Share or unshare libraries.",
formatter_class=argparse.RawTextHelpFormatter) formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-s', '--share', nargs='?', type=str, required=True, choices=['share', 'unshare'], metavar='', parser.add_argument('-s', '--share', nargs='?', type=str, required=True,
help='To share or to unshare.: \n' choices=['share', 'share_all', 'unshare', 'unshare_all'], metavar='',
'(choices: %(choices)s)') help='To share or to unshare.: \n (choices: %(choices)s)')
parser.add_argument('-u', '--user', nargs='?', type=str, required=True, choices=user_lst, metavar='', parser.add_argument('-u', '--user', nargs='?', type=str, required=True, choices=user_lst, metavar='',
help='Space separated list of case sensitive names to process. Allowed names are: \n' help='Space separated list of case sensitive names to process. Allowed names are: \n'
'(choices: %(choices)s)') '(choices: %(choices)s)')
@ -101,7 +103,7 @@ if __name__ == "__main__":
kill_session(opts.user, opts.libraries) kill_session(opts.user, opts.libraries)
unshare(opts.user, opts.libraries) unshare(opts.user, opts.libraries)
elif opts.share == 'unshare_all': elif opts.share == 'unshare_all':
kill_session(opts.user, '') kill_session(opts.user, sections_lst)
unshare(opts.user, sections_lst) unshare(opts.user, sections_lst)
else: else:
print('I don\'t know what else you want.') print('I don\'t know what else you want.')