updated naming and pep8

This commit is contained in:
Blacktwin 2018-03-16 16:12:35 -04:00
parent 76cef02e43
commit 3266f88ed4

View File

@ -1,6 +1,6 @@
""" """
Find what was added TFRAME ago and not watched and notify admin using PlexPy. Find what was added TFRAME ago and not watched and notify admin using Tautulli.
""" """
@ -8,16 +8,15 @@ import requests
import sys import sys
import time import time
TFRAME = 1.577e+7 # ~ 6 months in seconds TFRAME = 1.577e+7 # ~ 6 months in seconds
TODAY = time.time() TODAY = time.time()
## EDIT THESE SETTINGS ## ## EDIT THESE SETTINGS ##
PLEXPY_APIKEY = 'XXXXXXX' # Your PlexPy API key TAUTULLI_APIKEY = '' # Your Tautulli API key
PLEXPY_URL = 'http://localhost:8181/' # Your PlexPy URL TAUTULLI_URL = 'http://localhost:8183/' # Your Tautulli URL
LIBRARY_NAMES = ['My Movies', 'My TV Shows'] # Name of libraries you want to check. LIBRARY_NAMES = ['Movies', 'TV Shows'] # Name of libraries you want to check.
SUBJECT_TEXT = "PlexPy Notification" SUBJECT_TEXT = "Tautulli Notification"
AGENT_ID = 10 # The email notification agent ID for PlexPy NOTIFIER_ID = 12 # The email notification agent ID for Tautulli
class LIBINFO(object): class LIBINFO(object):
@ -44,15 +43,15 @@ class METAINFO(object):
self.file = d['file'] self.file = d['file']
def get_get_new_rating_keys(rating_key, media_type): def get_new_rating_keys(rating_key, media_type):
# Get a list of new rating keys for the PMS of all of the item's parent/children. # Get a list of new rating keys for the PMS of all of the item's parent/children.
payload = {'apikey': PLEXPY_APIKEY, payload = {'apikey': TAUTULLI_APIKEY,
'cmd': 'get_new_rating_keys', 'cmd': 'get_new_rating_keys',
'rating_key': rating_key, 'rating_key': rating_key,
'media_type': media_type} 'media_type': media_type}
try: try:
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload)
response = r.json() response = r.json()
res_data = response['response']['data'] res_data = response['response']['data']
@ -63,139 +62,143 @@ def get_get_new_rating_keys(rating_key, media_type):
return episode_lst return episode_lst
except Exception as e: except Exception as e:
sys.stderr.write("PlexPy API 'get_new_rating_keys' request failed: {0}.".format(e)) sys.stderr.write("Tautulli API 'get_new_rating_keys' request failed: {0}.".format(e))
def get_get_metadata(rating_key): def get_metadata(rating_key):
# Get the metadata for a media item. # Get the metadata for a media item.
payload = {'apikey': PLEXPY_APIKEY, payload = {'apikey': TAUTULLI_APIKEY,
'rating_key': rating_key, 'rating_key': rating_key,
'cmd': 'get_metadata', 'cmd': 'get_metadata',
'media_info': True} 'media_info': True}
try: try:
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload)
response = r.json() response = r.json()
res_data = response['response']['data']
res_data = response['response']['data']['metadata']
return METAINFO(data=res_data) return METAINFO(data=res_data)
except Exception as e: except Exception as e:
# sys.stderr.write("PlexPy API 'get_get_metadata' request failed: {0}.".format(e)) sys.stderr.write("Tautulli API 'get_metadata' request failed: {0}.".format(e))
pass pass
def get_get_library_media_info(section_id): def get_library_media_info(section_id):
# Get the data on the PlexPy media info tables. # Get the data on the Tautulli media info tables.
payload = {'apikey': PLEXPY_APIKEY, payload = {'apikey': TAUTULLI_APIKEY,
'section_id': section_id, 'section_id': section_id,
'cmd': 'get_library_media_info', 'cmd': 'get_library_media_info'}
'length': 10000}
try: try:
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload)
response = r.json() response = r.json()
print(response)
res_data = response['response']['data']['data'] res_data = response['response']['data']
return [LIBINFO(data=d) for d in res_data if d['play_count'] is None and (TODAY - int(d['added_at'])) > TFRAME] return [LIBINFO(data=d) for d in res_data if d['play_count'] is None and (TODAY - int(d['added_at'])) > TFRAME]
except Exception as e: except Exception as e:
sys.stderr.write("PlexPy API 'get_library_media_info' request failed: {0}.".format(e)) sys.stderr.write("Tautulli API 'get_library_media_info' request failed: {0}.".format(e))
def get_get_libraries_table():
# Get the data on the PlexPy libraries table. def get_libraries_table():
payload = {'apikey': PLEXPY_APIKEY, # Get the data on the Tautulli libraries table.
payload = {'apikey': TAUTULLI_APIKEY,
'cmd': 'get_libraries_table'} 'cmd': 'get_libraries_table'}
try: try:
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload)
response = r.json() response = r.json()
res_data = response['response']['data']['data'] res_data = response['response']['data']['data']
return [d['section_id'] for d in res_data if d['section_name'] in LIBRARY_NAMES] return [d['section_id'] for d in res_data if d['section_name'] in LIBRARY_NAMES]
except Exception as e: except Exception as e:
sys.stderr.write("PlexPy API 'get_libraries_table' request failed: {0}.".format(e)) sys.stderr.write("Tautulli API 'get_libraries_table' request failed: {0}.".format(e))
def send_notification(BODY_TEXT):
def send_notification(body_text):
# Format notification text # Format notification text
try: try:
subject = SUBJECT_TEXT subject = SUBJECT_TEXT
body = BODY_TEXT body = body_text
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))
return None return None
# Send the notification through PlexPy # Send the notification through Tautulli
payload = {'apikey': PLEXPY_APIKEY, payload = {'apikey': TAUTULLI_APIKEY,
'cmd': 'notify', 'cmd': 'notify',
'agent_id': AGENT_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(TAUTULLI_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 Tautulli notification.")
else: else:
raise Exception(response['response']['message']) raise Exception(response['response']['message'])
except Exception as e: except Exception as e:
sys.stderr.write("PlexPy API 'notify' request failed: {0}.".format(e)) sys.stderr.write("Tautulli API 'notify' request failed: {0}.".format(e))
return None return None
show_lst = [] show_lst = []
notify_lst = [] notify_lst = []
glt = [lib for lib in get_get_libraries_table()] libraries = [lib for lib in get_libraries_table()]
for i in glt: for library in libraries:
print(library, type(library))
try: try:
gglm = get_get_library_media_info(i) library_media_info = get_library_media_info(library)
for x in gglm: for lib in library_media_info:
try: try:
if x.media_type in ['show', 'episode']: if lib.media_type in ['show', 'episode']:
# Need to find TV shows rating_key for episode. # Need to find TV shows rating_key for episode.
show_lst += get_get_new_rating_keys(x.rating_key, x.media_type) show_lst += get_new_rating_keys(lib.rating_key, lib.media_type)
else: else:
# Find movie rating_key. # Find movie rating_key.
show_lst += [int(x.rating_key)] show_lst += [int(lib.rating_key)]
except Exception as e: except Exception as e:
print("Rating_key failed: {e}").format(e=e) print "Rating_key failed: {e}".format(e=e)
except Exception as e: except Exception as e:
print("Library media info failed: {e}").format(e=e) print "Library media info failed: {e}".format(e=e)
for i in show_lst: for show in show_lst:
try: try:
x = get_get_metadata(str(i)) meta = get_metadata(str(show))
added = time.ctime(float(x.added_at)) added = time.ctime(float(meta.added_at))
if x.grandparent_title == '' or x.media_type == 'movie': if meta.grandparent_title == '' or meta.media_type == 'movie':
# Movies # Movies
notify_lst += [u"<dt>{x.title} ({x.rating_key}) was added {when} and has not been" notify_lst += [u"<dt>{x.title} ({x.rating_key}) was added {when} and has not been"
u" watched.</dt> <dd>File location: {x.file}</dd> <br>".format(x=x, when=added)] u" watched.</dt> <dd>File location: {x.file}</dd> <br>".format(x=meta, when=added)]
else: else:
# Shows # Shows
notify_lst += [u"<dt>{x.grandparent_title}: {x.title} ({x.rating_key}) was added {when} and has" notify_lst += [u"<dt>{x.grandparent_title}: {x.title} ({x.rating_key}) was added {when} and has"
u" not been watched.<d/t> <dd>File location: {x.file}</dd> <br>".format(x=x, when=added)] u" not been watched.<d/t> <dd>File location: {x.file}</dd> <br>".format(x=meta, when=added)]
except Exception as e: except Exception as e:
print("Metadata failed. Likely end of range: {e}").format(e=e) print "Metadata failed. Likely end of range: {e}".format(e=e)
if notify_lst:
BODY_TEXT = """\
<html>
<head></head>
<body>
<p>Hi!<br>
<br>Below is the list of {LIBRARY_NAMES} that have not been watched.<br>
<dl>
{notify_lst}
</dl>
</p>
</body>
</html>
""".format(notify_lst="\n".join(notify_lst).encode("utf-8"), LIBRARY_NAMES=" & ".join(LIBRARY_NAMES))
BODY_TEXT = """\ print(BODY_TEXT)
<html> send_notification(BODY_TEXT)
<head></head> else:
<body> exit()
<p>Hi!<br>
<br>Below is the list of {LIBRARY_NAMES} that have not been watched.<br>
<dl>
{notify_lst}
</dl>
</p>
</body>
</html>
""".format(notify_lst="\n".join(notify_lst).encode("utf-8"),LIBRARY_NAMES=" & ".join(LIBRARY_NAMES))
send_notification(BODY_TEXT)