updated for tautulli and pep8
This commit is contained in:
parent
e5fe20401f
commit
0315274de2
@ -1,4 +1,4 @@
|
|||||||
'''
|
"""
|
||||||
Delay Notification Agent message for concurrent streams
|
Delay Notification Agent message for concurrent streams
|
||||||
|
|
||||||
Arguments passed from PlexPy
|
Arguments passed from PlexPy
|
||||||
@ -12,22 +12,21 @@ PlexPy > Settings > Notification Agents > Scripts > Gear icon:
|
|||||||
User Concurrent Streams: notify_delay.py
|
User Concurrent Streams: notify_delay.py
|
||||||
|
|
||||||
PlexPy Settings > Notification Agents > Scripts (Gear) > Script Timeout: 0 to disable or set to > 180
|
PlexPy Settings > Notification Agents > Scripts (Gear) > Script Timeout: 0 to disable or set to > 180
|
||||||
'''
|
"""
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
|
|
||||||
## EDIT THESE SETTINGS ##
|
## EDIT THESE SETTINGS ##
|
||||||
PLEXPY_APIKEY = 'xxxxx' # Your PlexPy API key
|
PLEXPY_APIKEY = '' # Your PlexPy API key
|
||||||
PLEXPY_URL = 'http://localhost:8182/' # Your PlexPy URL
|
PLEXPY_URL = 'http://localhost:8181/' # Your PlexPy URL
|
||||||
CONCURRENT_TOTAL = 2
|
CONCURRENT_TOTAL = 2
|
||||||
TIMEOUT = 180
|
TIMEOUT = 180
|
||||||
INTERVAL = 20
|
INTERVAL = 20
|
||||||
|
|
||||||
AGENT_ID = 10 # Notification agent ID for PlexPy
|
NOTIFIER_ID = 10 # Notification notifier ID for PlexPy
|
||||||
# Find Notification agent ID here:
|
# Find Notification agent ID here:
|
||||||
# https://github.com/JonnyWong16/plexpy/blob/master/API.md#notify
|
# https://github.com/JonnyWong16/plexpy/blob/master/API.md#notify
|
||||||
|
|
||||||
@ -59,11 +58,12 @@ def get_get_activity():
|
|||||||
sys.stderr.write("PlexPy API 'get_activity' request failed: {0}.".format(e))
|
sys.stderr.write("PlexPy API 'get_activity' request failed: {0}.".format(e))
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def send_notification(SUBJECT_TEXT, BODY_TEXT):
|
|
||||||
|
def send_notification(subject_text, body_text):
|
||||||
# Format notification text
|
# Format notification text
|
||||||
try:
|
try:
|
||||||
subject = SUBJECT_TEXT.format(p=p, total=cc_total)
|
subject = subject_text.format(p=p, total=cc_total)
|
||||||
body = BODY_TEXT.format(p=p, total=cc_total, time=TIMEOUT/60)
|
body = body_text.format(p=p, total=cc_total, time=TIMEOUT / 60)
|
||||||
|
|
||||||
except LookupError as e:
|
except LookupError as e:
|
||||||
sys.stderr.write("Unable to substitute '{0}' in the notification subject or body".format(e))
|
sys.stderr.write("Unable to substitute '{0}' in the notification subject or body".format(e))
|
||||||
@ -71,7 +71,7 @@ def send_notification(SUBJECT_TEXT, BODY_TEXT):
|
|||||||
# Send the notification through PlexPy
|
# Send the notification through PlexPy
|
||||||
payload = {'apikey': PLEXPY_APIKEY,
|
payload = {'apikey': PLEXPY_APIKEY,
|
||||||
'cmd': 'notify',
|
'cmd': 'notify',
|
||||||
'agent_id': AGENT_ID,
|
'notifier_id': NOTIFIER_ID,
|
||||||
'subject': subject,
|
'subject': subject,
|
||||||
'body': body}
|
'body': body}
|
||||||
|
|
||||||
@ -87,6 +87,7 @@ def send_notification(SUBJECT_TEXT, BODY_TEXT):
|
|||||||
sys.stderr.write("PlexPy API 'notify' request failed: {0}.".format(e))
|
sys.stderr.write("PlexPy API 'notify' request failed: {0}.".format(e))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
Pulling together User IP information and Email.
|
Pulling together User IP information and Email.
|
||||||
|
|
||||||
@ -9,7 +8,9 @@ PlexPy > Settings > Notification Agents > Scripts > Gear icon:
|
|||||||
Playback Start: notify_newip.py
|
Playback Start: notify_newip.py
|
||||||
|
|
||||||
Arguments passed from PlexPy
|
Arguments passed from PlexPy
|
||||||
-sn {show_name} -ena {episode_name} -ssn {season_num00} -enu {episode_num00} -srv {server_name} -med {media_type} -pos {poster_url} -tt {title} -sum {summary} -lbn {library_name} -ip {ip_address} -us {user} -uid {user_id} -pf {platform} -pl {player} -da {datestamp} -ti {timestamp}
|
-sn {show_name} -ena {episode_name} -ssn {season_num00} -enu {episode_num00} -srv {server_name} -med {media_type}
|
||||||
|
-pos {poster_url} -tt {title} -sum {summary} -lbn {library_name} -ip {ip_address} -us {user} -uid {user_id}
|
||||||
|
-pf {platform} -pl {player} -da {datestamp} -ti {timestamp}
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -17,11 +18,10 @@ import argparse
|
|||||||
import requests
|
import requests
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
## EDIT THESE SETTINGS ##
|
## EDIT THESE SETTINGS ##
|
||||||
PLEXPY_APIKEY = 'XXXX' # Your PlexPy API key
|
PLEXPY_APIKEY = '' # Your PlexPy API key
|
||||||
PLEXPY_URL = 'http://localhost:8181/' # Your PlexPy URL
|
PLEXPY_URL = 'http://localhost:8181/' # Your PlexPy URL
|
||||||
NOTIFICATION_ID = 10 # The notification agent ID for PlexPy 10 = Email
|
NOTIFIER_ID = 12 # The notification notifier ID
|
||||||
|
|
||||||
# Replace LAN IP addresses that start with the LAN_SUBNET with a WAN IP address
|
# Replace LAN IP addresses that start with the LAN_SUBNET with a WAN IP address
|
||||||
# to retrieve geolocation data. Leave REPLACEMENT_WAN_IP blank for no replacement.
|
# to retrieve geolocation data. Leave REPLACEMENT_WAN_IP blank for no replacement.
|
||||||
@ -38,16 +38,19 @@ BODY_TEXT = """\
|
|||||||
<head></head>
|
<head></head>
|
||||||
<body>
|
<body>
|
||||||
<p>Hi!<br>
|
<p>Hi!<br>
|
||||||
<br><a href="mailto:{u.email}"><img src="{u.user_thumb}" alt="Poster unavailable" height="50" width="50"></a> {p.user} has watched {p.media_type}:{p.title} from a new IP address: {p.ip_address}<br>
|
<br><a href="mailto:{u.email}"><img src="{u.user_thumb}" alt="Poster unavailable" height="50" width="50"></a>
|
||||||
<br>On {p.platform}[{p.player}] in <a href="http://maps.google.com/?q={g.city},{g.country},{g.postal_code}">{g.city}, {g.country} {g.postal_code}</a> at {p.timestamp} on {p.datestamp}<br>
|
{p.user} has watched {p.media_type}:{p.title} from a new IP address: {p.ip_address}<br>
|
||||||
|
<br>On {p.platform}[{p.player}] in
|
||||||
|
<a href="http://maps.google.com/?q={g.city},{g.country},{g.postal_code}">{g.city}, {g.country} {g.postal_code}</a>
|
||||||
|
at {p.timestamp} on {p.datestamp}<br>
|
||||||
<br><br>
|
<br><br>
|
||||||
<br>User email is: {u.email}<br>
|
<br>User email is: {u.email}<br>
|
||||||
</p>
|
</p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
##Geo Space##
|
|
||||||
class GeoData(object):
|
class GeoData(object):
|
||||||
def __init__(self, data=None):
|
def __init__(self, data=None):
|
||||||
data = data or {}
|
data = data or {}
|
||||||
@ -55,27 +58,22 @@ class GeoData(object):
|
|||||||
self.city = data.get('city', 'N/A')
|
self.city = data.get('city', 'N/A')
|
||||||
self.postal_code = data.get('postal_code', 'N/A')
|
self.postal_code = data.get('postal_code', 'N/A')
|
||||||
|
|
||||||
##USER Space##
|
|
||||||
class UserEmail(object):
|
class UserEmail(object):
|
||||||
def __init__(self, data=None):
|
def __init__(self, data=None):
|
||||||
data = data or {}
|
data = data or {}
|
||||||
self.email = data.get('email', 'N/A')
|
self.email = data.get('email', 'N/A')
|
||||||
self.user_id = data.get('user_id', 'N/A')
|
self.user_id = data.get('user_id', 'N/A')
|
||||||
self.user_thumb = data.get('user_thumb', 'N/A')
|
self.user_thumb = data.get('user_thumb', 'N/A')
|
||||||
|
|
||||||
##IP Space##
|
|
||||||
class UserIPs(object):
|
|
||||||
def __init__(self, data=None):
|
|
||||||
data = data or {}
|
|
||||||
|
|
||||||
##API Space##
|
|
||||||
def get_user_ip_addresses(user_id='', ip_address=''):
|
def get_user_ip_addresses(user_id='', ip_address=''):
|
||||||
# Get the user IP list from PlexPy
|
# Get the user IP list from PlexPy
|
||||||
payload = {'apikey': PLEXPY_APIKEY,
|
payload = {'apikey': PLEXPY_APIKEY,
|
||||||
'cmd': 'get_user_ips',
|
'cmd': 'get_user_ips',
|
||||||
'user_id': user_id,
|
'user_id': user_id,
|
||||||
'search': ip_address}
|
'search': ip_address}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload)
|
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload)
|
||||||
response = r.json()
|
response = r.json()
|
||||||
@ -88,7 +86,7 @@ def get_user_ip_addresses(user_id='', ip_address=''):
|
|||||||
sys.stdout.write("Successfully retrieved UserIPs data.")
|
sys.stdout.write("Successfully retrieved UserIPs data.")
|
||||||
if response['response']['data']['recordsFiltered'] == 0:
|
if response['response']['data']['recordsFiltered'] == 0:
|
||||||
sys.stdout.write("IP has no history.")
|
sys.stdout.write("IP has no history.")
|
||||||
return UserIPs(data=data)
|
return data
|
||||||
else:
|
else:
|
||||||
sys.stdout.write("IP has history, killing script.")
|
sys.stdout.write("IP has history, killing script.")
|
||||||
exit()
|
exit()
|
||||||
@ -96,7 +94,8 @@ def get_user_ip_addresses(user_id='', ip_address=''):
|
|||||||
raise Exception(response['response']['message'])
|
raise Exception(response['response']['message'])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
sys.stderr.write("PlexPy API 'get_user_ip_addresses' request failed: {0}.".format(e))
|
sys.stderr.write("PlexPy API 'get_user_ip_addresses' request failed: {0}.".format(e))
|
||||||
return UserIPs()
|
return
|
||||||
|
|
||||||
|
|
||||||
def get_geoip_info(ip_address=''):
|
def get_geoip_info(ip_address=''):
|
||||||
# Get the geo IP lookup from PlexPy
|
# Get the geo IP lookup from PlexPy
|
||||||
@ -145,6 +144,7 @@ def get_user_email(user_id=''):
|
|||||||
sys.stderr.write("PlexPy API 'get_user' request failed: {0}.".format(e))
|
sys.stderr.write("PlexPy API 'get_user' request failed: {0}.".format(e))
|
||||||
return UserEmail()
|
return UserEmail()
|
||||||
|
|
||||||
|
|
||||||
def send_notification(arguments=None, geodata=None, useremail=None):
|
def send_notification(arguments=None, geodata=None, useremail=None):
|
||||||
# Format notification text
|
# Format notification text
|
||||||
try:
|
try:
|
||||||
@ -156,14 +156,14 @@ def send_notification(arguments=None, geodata=None, useremail=None):
|
|||||||
# Send the notification through PlexPy
|
# Send the notification through PlexPy
|
||||||
payload = {'apikey': PLEXPY_APIKEY,
|
payload = {'apikey': PLEXPY_APIKEY,
|
||||||
'cmd': 'notify',
|
'cmd': 'notify',
|
||||||
'agent_id': NOTIFICATION_ID,
|
'notifier_id': NOTIFIER_ID,
|
||||||
'subject': subject,
|
'subject': subject,
|
||||||
'body': body}
|
'body': body}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
r = requests.post(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload)
|
r = requests.post(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload)
|
||||||
response = r.json()
|
response = r.json()
|
||||||
|
|
||||||
if response['response']['result'] == 'success':
|
if response['response']['result'] == 'success':
|
||||||
sys.stdout.write("Successfully sent PlexPy notification.")
|
sys.stdout.write("Successfully sent PlexPy notification.")
|
||||||
else:
|
else:
|
||||||
@ -172,6 +172,7 @@ def send_notification(arguments=None, geodata=None, useremail=None):
|
|||||||
sys.stderr.write("PlexPy API 'notify' request failed: {0}.".format(e))
|
sys.stderr.write("PlexPy API 'notify' request failed: {0}.".format(e))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Parse arguments from PlexPy
|
# Parse arguments from PlexPy
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
@ -181,7 +182,7 @@ if __name__ == '__main__':
|
|||||||
parser.add_argument('-us', '--user', action='store', default='',
|
parser.add_argument('-us', '--user', action='store', default='',
|
||||||
help='Username of the person watching the stream')
|
help='Username of the person watching the stream')
|
||||||
parser.add_argument('-uid', '--user_id', action='store', default='',
|
parser.add_argument('-uid', '--user_id', action='store', default='',
|
||||||
help='User_ID of the person watching the stream')
|
help='User_ID of the person watching the stream')
|
||||||
parser.add_argument('-med', '--media_type', action='store', default='',
|
parser.add_argument('-med', '--media_type', action='store', default='',
|
||||||
help='The media type of the stream')
|
help='The media type of the stream')
|
||||||
parser.add_argument('-tt', '--title', action='store', default='',
|
parser.add_argument('-tt', '--title', action='store', default='',
|
||||||
@ -210,7 +211,7 @@ if __name__ == '__main__':
|
|||||||
help='The summary of the TV show')
|
help='The summary of the TV show')
|
||||||
parser.add_argument('-lbn', '--library_name', action='store', default='',
|
parser.add_argument('-lbn', '--library_name', action='store', default='',
|
||||||
help='The name of the TV show')
|
help='The name of the TV show')
|
||||||
|
|
||||||
p = parser.parse_args()
|
p = parser.parse_args()
|
||||||
|
|
||||||
# Check to make sure there is an IP address before proceeding
|
# Check to make sure there is an IP address before proceeding
|
||||||
@ -219,11 +220,11 @@ if __name__ == '__main__':
|
|||||||
ip_address = REPLACEMENT_WAN_IP
|
ip_address = REPLACEMENT_WAN_IP
|
||||||
else:
|
else:
|
||||||
ip_address = p.ip_address
|
ip_address = p.ip_address
|
||||||
|
|
||||||
g = get_geoip_info(ip_address=ip_address)
|
g = get_geoip_info(ip_address=ip_address)
|
||||||
u = get_user_email(user_id=p.user_id)
|
u = get_user_email(user_id=p.user_id)
|
||||||
get_user_ip_addresses(user_id=p.user_id, ip_address=p.ip_address)
|
get_user_ip_addresses(user_id=p.user_id, ip_address=p.ip_address)
|
||||||
send_notification(arguments=p, geodata=g, useremail=u)
|
send_notification(arguments=p, geodata=g, useremail=u)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
sys.stdout.write("No IP address passed from PlexPy.")
|
sys.stdout.write("No IP address passed from PlexPy.")
|
||||||
|
Loading…
Reference in New Issue
Block a user