From 0315274de2255995fd6ea6c1fd551a505adba3c9 Mon Sep 17 00:00:00 2001 From: Blacktwin Date: Mon, 19 Mar 2018 11:35:48 -0400 Subject: [PATCH] updated for tautulli and pep8 --- notify/notify_delay.py | 21 +++++++++-------- notify/notify_newip.py | 53 +++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/notify/notify_delay.py b/notify/notify_delay.py index db3c665..bd0eaf0 100644 --- a/notify/notify_delay.py +++ b/notify/notify_delay.py @@ -1,4 +1,4 @@ -''' +""" Delay Notification Agent message for concurrent streams Arguments passed from PlexPy @@ -12,22 +12,21 @@ PlexPy > Settings > Notification Agents > Scripts > Gear icon: User Concurrent Streams: notify_delay.py PlexPy Settings > Notification Agents > Scripts (Gear) > Script Timeout: 0 to disable or set to > 180 -''' +""" import requests import sys import argparse from time import sleep - ## EDIT THESE SETTINGS ## -PLEXPY_APIKEY = 'xxxxx' # Your PlexPy API key -PLEXPY_URL = 'http://localhost:8182/' # Your PlexPy URL +PLEXPY_APIKEY = '' # Your PlexPy API key +PLEXPY_URL = 'http://localhost:8181/' # Your PlexPy URL CONCURRENT_TOTAL = 2 TIMEOUT = 180 INTERVAL = 20 -AGENT_ID = 10 # Notification agent ID for PlexPy +NOTIFIER_ID = 10 # Notification notifier ID for PlexPy # Find Notification agent ID here: # 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)) pass -def send_notification(SUBJECT_TEXT, BODY_TEXT): + +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=TIMEOUT/60) + subject = subject_text.format(p=p, total=cc_total) + body = body_text.format(p=p, total=cc_total, time=TIMEOUT / 60) except LookupError as 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 payload = {'apikey': PLEXPY_APIKEY, 'cmd': 'notify', - 'agent_id': AGENT_ID, + 'notifier_id': NOTIFIER_ID, 'subject': subject, 'body': body} @@ -87,6 +87,7 @@ def send_notification(SUBJECT_TEXT, BODY_TEXT): sys.stderr.write("PlexPy API 'notify' request failed: {0}.".format(e)) return None + if __name__ == '__main__': parser = argparse.ArgumentParser() diff --git a/notify/notify_newip.py b/notify/notify_newip.py index d3f624d..8307ade 100644 --- a/notify/notify_newip.py +++ b/notify/notify_newip.py @@ -1,4 +1,3 @@ - """ Pulling together User IP information and Email. @@ -9,7 +8,9 @@ PlexPy > Settings > Notification Agents > Scripts > Gear icon: Playback Start: notify_newip.py 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 sys - ## EDIT THESE SETTINGS ## -PLEXPY_APIKEY = 'XXXX' # Your PlexPy API key +PLEXPY_APIKEY = '' # Your PlexPy API key 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 # to retrieve geolocation data. Leave REPLACEMENT_WAN_IP blank for no replacement. @@ -38,16 +38,19 @@ BODY_TEXT = """\

Hi!
-
Poster unavailable {p.user} has watched {p.media_type}:{p.title} from a new IP address: {p.ip_address}
-
On {p.platform}[{p.player}] in {g.city}, {g.country} {g.postal_code} at {p.timestamp} on {p.datestamp}
+
Poster unavailable + {p.user} has watched {p.media_type}:{p.title} from a new IP address: {p.ip_address}
+
On {p.platform}[{p.player}] in + {g.city}, {g.country} {g.postal_code} + at {p.timestamp} on {p.datestamp}



User email is: {u.email}

""" - -##Geo Space## + + class GeoData(object): def __init__(self, data=None): data = data or {} @@ -55,27 +58,22 @@ class GeoData(object): self.city = data.get('city', 'N/A') self.postal_code = data.get('postal_code', 'N/A') -##USER Space## + class UserEmail(object): def __init__(self, data=None): data = data or {} self.email = data.get('email', 'N/A') self.user_id = data.get('user_id', '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=''): # Get the user IP list from PlexPy payload = {'apikey': PLEXPY_APIKEY, 'cmd': 'get_user_ips', 'user_id': user_id, 'search': ip_address} - + try: r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) response = r.json() @@ -88,7 +86,7 @@ def get_user_ip_addresses(user_id='', ip_address=''): sys.stdout.write("Successfully retrieved UserIPs data.") if response['response']['data']['recordsFiltered'] == 0: sys.stdout.write("IP has no history.") - return UserIPs(data=data) + return data else: sys.stdout.write("IP has history, killing script.") exit() @@ -96,7 +94,8 @@ def get_user_ip_addresses(user_id='', ip_address=''): raise Exception(response['response']['message']) except Exception as e: sys.stderr.write("PlexPy API 'get_user_ip_addresses' request failed: {0}.".format(e)) - return UserIPs() + return + def get_geoip_info(ip_address=''): # 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)) return UserEmail() + def send_notification(arguments=None, geodata=None, useremail=None): # Format notification text try: @@ -156,14 +156,14 @@ def send_notification(arguments=None, geodata=None, useremail=None): # Send the notification through PlexPy payload = {'apikey': PLEXPY_APIKEY, 'cmd': 'notify', - 'agent_id': NOTIFICATION_ID, + 'notifier_id': NOTIFIER_ID, 'subject': subject, 'body': body} try: r = requests.post(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) response = r.json() - + if response['response']['result'] == 'success': sys.stdout.write("Successfully sent PlexPy notification.") 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)) return None + if __name__ == '__main__': # Parse arguments from PlexPy parser = argparse.ArgumentParser() @@ -181,7 +182,7 @@ if __name__ == '__main__': parser.add_argument('-us', '--user', action='store', default='', help='Username of the person watching the stream') 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='', help='The media type of the stream') parser.add_argument('-tt', '--title', action='store', default='', @@ -210,7 +211,7 @@ if __name__ == '__main__': help='The summary of the TV show') parser.add_argument('-lbn', '--library_name', action='store', default='', help='The name of the TV show') - + p = parser.parse_args() # Check to make sure there is an IP address before proceeding @@ -219,11 +220,11 @@ if __name__ == '__main__': ip_address = REPLACEMENT_WAN_IP else: ip_address = p.ip_address - + g = get_geoip_info(ip_address=ip_address) u = get_user_email(user_id=p.user_id) get_user_ip_addresses(user_id=p.user_id, ip_address=p.ip_address) send_notification(arguments=p, geodata=g, useremail=u) - + else: sys.stdout.write("No IP address passed from PlexPy.")