forked from blacktwin/JBOPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_play.py
88 lines (71 loc) · 2.53 KB
/
check_play.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
# 1. Install the requests module for python.
# pip install requests
# 2. Add script arguments in Tautulli.
# {user} {title}
# Add to Playback Resume
import requests
import sys
user = sys.argv[1]
title = sys.argv[2]
## EDIT THESE SETTINGS ##
TAUTULLI_APIKEY = 'XXXXXXXXXX' # Your Tautulli API key
TAUTULLI_URL = 'http://localhost:8181/' # Your Tautulli URL
NOTIFIER_ID = 10 # The notification notifier ID for Tautulli
SUBJECT_TEXT = "Tautulli Notification"
BODY_TEXT = """\
<html>
<head></head>
<body>
<p>Hi!<br>
<br>User %s has attempted to watch %s more than 3 times unsuccessfully.<br>
</p>
</body>
</html>
""" %(user, title)
class UserHIS(object):
def __init__(self, data=None):
data = data or {}
self.watched = [d['watched_status'] for d in data]
def get_history():
# Get the user IP list from Tautulli
payload = {'apikey': TAUTULLI_APIKEY,
'cmd': 'get_history',
'user': user,
'search': title}
try:
r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload)
response = r.json()
if response['response']['data']['recordsFiltered'] > 2:
res_data = response['response']['data']['data']
return UserHIS(data=res_data)
except Exception as e:
sys.stderr.write("Tautulli API 'get_history' request failed: {0}.".format(e))
def send_notification():
# Format notification text
try:
subject = SUBJECT_TEXT
body = BODY_TEXT
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__':
hisy = get_history()
if sum(hisy.watched) == 0:
sys.stdout.write(user + ' has attempted to watch ' + title + ' more than 3 times unsuccessfully.')
send_notification()