forked from blacktwin/JBOPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify_delay.py
122 lines (99 loc) · 3.95 KB
/
notify_delay.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
119
120
121
122
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Delay Notification Agent message for concurrent streams
Arguments passed from Tautulli
-u {user} -srv {server_name}
You can add more arguments if you want more details in the email body
Adding to Tautulli
Tautulli > Settings > Notification Agents > Scripts > Bell icon:
[X] Notify on concurrent streams
Tautulli > Settings > Notification Agents > Scripts > Gear icon:
User Concurrent Streams: notify_delay.py
Tautulli Settings > Notification Agents > Scripts (Gear) > Script Timeout: 0 to disable or set to > 180
"""
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals
from past.utils import old_div
import requests
import sys
import argparse
from time import sleep
# ## EDIT THESE SETTINGS ##
TAUTULLI_APIKEY = '' # Your Tautulli API key
TAUTULLI_URL = 'http://localhost:8181/' # Your Tautulli URL
CONCURRENT_TOTAL = 2
TIMEOUT = 180
INTERVAL = 20
NOTIFIER_ID = 10 # Notification notifier ID for Tautulli
# Find Notification agent ID here:
# https://github.com/JonnyWong16/plexpy/blob/master/API.md#notify
SUBJECT_TEXT = 'Concurrent Streams from {p.user} on {p.plex_server}'
BODY_TEXT = """\
<html>
<head></head>
<body>
<p>Hi!<br>
{p.user} has had {total} concurrent streams for longer than {time} minutes.
</p>
</body>
</html>
"""
def get_activity():
"""Get the current activity on the PMS."""
payload = {'apikey': TAUTULLI_APIKEY,
'cmd': 'get_activity'}
try:
r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload)
response = r.json()
res_data = response['response']['data']['sessions']
return [d['user'] for d in res_data]
except Exception as e:
sys.stderr.write("Tautulli API 'get_activity' request failed: {0}.".format(e))
pass
def send_notification(subject_text, body_text):
"""Format notification text."""
try:
subject = subject_text.format(p=p, total=cc_total)
body = body_text.format(p=p, total=cc_total, time=old_div(TIMEOUT, 60))
except LookupError as e:
sys.stderr.write("Unable to substitute '{0}' in the notification subject or body".format(e))
return None
# Send the notification through Tautulli
payload = {'apikey': TAUTULLI_APIKEY,
'cmd': 'notify',
'notifier_id': NOTIFIER_ID,
'subject': subject,
'body': body}
try:
r = requests.post(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload)
response = r.json()
if response['response']['result'] == 'success':
sys.stdout.write("Successfully sent Tautulli notification.")
else:
raise Exception(response['response']['message'])
except Exception as e:
sys.stderr.write("Tautulli API 'notify' request failed: {0}.".format(e))
return None
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--user', action='store', default='',
help='Username of the person watching the stream')
parser.add_argument('-srv', '--plex_server', action='store', default='',
help='The name of the Plex server')
p = parser.parse_args()
x = 0
while x < TIMEOUT and x is not None:
# check if user still has concurrent streams
print('Checking concurrent stream count.')
cc_total = get_activity().count(p.user)
if cc_total >= CONCURRENT_TOTAL:
print('{p.user} still has {total} concurrent streams.'.format(p=p, total=cc_total))
sleep(INTERVAL)
x += INTERVAL
else:
print('Exiting, user no longer has concurrent streams.')
exit()
print('Concurrent stream monitoring timeout limit has been reached. Sending notification.')
send_notification(SUBJECT_TEXT, BODY_TEXT)