-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplexmon.py
More file actions
47 lines (39 loc) · 1.27 KB
/
plexmon.py
File metadata and controls
47 lines (39 loc) · 1.27 KB
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
#!/usr/bin/env python3
import collections
import dotenv
import requests
# Suppress only the single warning from urllib3 needed.
from urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
def web_request(method, url, **kwargs):
s = requests.Session()
s.verify = False
return getattr(s, method)(url, **kwargs)
defcfg = collections.OrderedDict([
('plex_addr', 'https://localhost:32400'),
('plex_token', ''),
('plex_jail', 'plexmediaserver'),
('truenas_addr', 'https://localhost'),
('truenas_apikey', ''),
])
config = dotenv.dotenv_values('plexmon.conf')
for k in defcfg:
if not k in config:
config[k] = defcfg[k]
try:
r = web_request(
'get', config['plex_addr'] + '/',
headers={
'X-Plex-Token': config['plex_token']
})
if r.status_code not in (200, 201, 204):
raise ConnectionError
except Exception as e:
print('Restarting Plex Media Server...')
web_request(
'post', config['truenas_addr'] + '/api/v2.0/jail/restart',
headers={
'Authorization': 'Bearer ' + config['truenas_apikey'],
'Content-Type': 'application/json',
},
data='"' + config['plex_jail'] + '"')