updated for tautulli and pep8

This commit is contained in:
Blacktwin 2018-03-19 11:35:48 -04:00
parent e5fe20401f
commit 0315274de2
2 changed files with 38 additions and 36 deletions

View File

@ -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()

View File

@ -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,8 +38,11 @@ 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>
@ -47,7 +50,7 @@ BODY_TEXT = """\
</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,7 +58,7 @@ 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 {}
@ -63,12 +66,7 @@ class UserEmail(object):
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,
@ -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,7 +156,7 @@ 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}
@ -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()