-
Notifications
You must be signed in to change notification settings - Fork 305
/
plex_api_parental_control.py
118 lines (90 loc) · 4.42 KB
/
plex_api_parental_control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Set as cron or task for times of allowing and not allowing user access to server.
Unsharing will kill any current stream from user before unsharing.
Share or unshare libraries.
optional arguments:
-h, --help show this help message and exit
-s [], --share [] To share or to unshare.:
(choices: share, share_all, unshare)
-u [], --user [] Space separated list of case sensitive names to process. Allowed names are:
(choices: All users names)
-l [ ...], --libraries [ ...]
Space separated list of case sensitive names to process. Allowed names are:
(choices: All library names)
(default: All Libraries)
Usage:
plex_api_share.py -s share -u USER -l Movies
- Shared libraries: ['Movies'] with USER
plex_api_share.py -s share -u USER -l Movies "TV Shows"
- Shared libraries: ['Movies', 'TV Shows'] with USER
* Double Quote libraries with spaces
plex_api_share.py -s share_all -u USER
- Shared all libraries with USER.
plex_api_share.py -s unshare -u USER
- Kill users current stream.
- Unshared all libraries with USER.
- USER is still exists as a Friend or Home User
"""
from __future__ import print_function
from __future__ import unicode_literals
import argparse
import requests
from time import sleep
from plexapi.server import PlexServer, CONFIG
MESSAGE = "GET TO BED!"
PLEX_URL = ''
PLEX_TOKEN = ''
PLEX_URL = CONFIG.data['auth'].get('server_baseurl', PLEX_URL)
PLEX_TOKEN = CONFIG.data['auth'].get('server_token', PLEX_TOKEN)
sess = requests.Session()
# Ignore verifying the SSL certificate
sess.verify = False # '/path/to/certfile'
# If verify is set to a path to a directory,
# the directory must have been processed using the c_rehash utility supplied
# with OpenSSL.
if sess.verify is False:
# Disable the warning that the request is insecure, we know that...
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
user_lst = [x.title for x in plex.myPlexAccount().users()]
sections_lst = [x.title for x in plex.library.sections()]
def share(user, libraries):
plex.myPlexAccount().updateFriend(user=user, server=plex, sections=libraries)
print('Shared libraries: {libraries} with {user}.'.format(libraries=libraries, user=user))
def unshare(user, libraries):
plex.myPlexAccount().updateFriend(user=user, server=plex, removeSections=True, sections=libraries)
print('Unshared all libraries from {user}.'.format(libraries=libraries, user=user))
def kill_session(user):
for session in plex.sessions():
# Check for users stream
if session.usernames[0] in user:
title = (session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title
print('{user} is watching {title} and it\'s past their bedtime. Killing stream.'.format(
user=user, title=title))
session.stop(reason=MESSAGE)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Share or unshare libraries.",
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-s', '--share', nargs='?', type=str, required=True,
choices=['share', 'share_all', 'unshare'], metavar='',
help='To share or to unshare.: \n (choices: %(choices)s)')
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'
'(choices: %(choices)s)')
parser.add_argument('-l', '--libraries', nargs='+', default='', choices=sections_lst, metavar='',
help='Space separated list of case sensitive names to process. Allowed names are: \n'
'(choices: %(choices)s \n(default: All Libraries)')
opts = parser.parse_args()
if opts.share == 'share':
share(opts.user, opts.libraries)
elif opts.share == 'share_all':
share(opts.user, sections_lst)
elif opts.share == 'unshare':
kill_session(opts.user)
sleep(5)
unshare(opts.user, sections_lst)
else:
print('I don\'t know what else you want.')