Created
February 14, 2017 14:18
-
-
Save blacktwin/f2f5e000aeb0ccc16cc9d4795908a5f7 to your computer and use it in GitHub Desktop.
Notify FaceBook on what was added to Plex last week.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import sys | |
import time | |
TODAY = int(time.time()) | |
LASTWEEK = int(TODAY - 7 * 24 * 60 * 60) | |
## EDIT THESE SETTINGS ## | |
PLEXPY_APIKEY = 'XXXXXX' # Your PlexPy API key | |
PLEXPY_URL = 'http://localhost:8181/' # Your PlexPy URL | |
LIBRARY_NAMES = ['My TV Shows', 'My Movies'] # Name of libraries you want to check. | |
SUBJECT_TEXT = "PlexPy Added Last Week Notification" | |
AGENT_ID = 16 # The Facebook notification agent ID for PlexPy | |
class METAINFO(object): | |
def __init__(self, data=None): | |
d = data or {} | |
self.added_at = d['added_at'] | |
self.parent_rating_key = d['parent_rating_key'] | |
self.title = d['title'] | |
self.rating_key = d['rating_key'] | |
self.media_type = d['media_type'] | |
self.grandparent_title = d['grandparent_title'] | |
self.file_size = d['file_size'] | |
self.thumb = d['art'] | |
self.summary = d['summary'] | |
def get_get_recent(section_id): | |
# Get the metadata for a media item. | |
payload = {'apikey': PLEXPY_APIKEY, | |
'count': '10', | |
'section_id': section_id, | |
'cmd': 'get_recently_added'} | |
try: | |
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) | |
response = r.json() | |
if response['response']['result'] == 'success': | |
res_data = response['response']['data']['recently_added'] | |
return [d['rating_key'] for d in res_data] | |
except Exception as e: | |
sys.stderr.write("PlexPy API 'get_recently_added' request failed: {0}.".format(e)) | |
def get_get_metadata(rating_key): | |
# Get the metadata for a media item. | |
payload = {'apikey': PLEXPY_APIKEY, | |
'rating_key': rating_key, | |
'cmd': 'get_metadata', | |
'media_info': True} | |
try: | |
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) | |
response = r.json() | |
if response['response']['result'] == 'success': | |
res_data = response['response']['data']['metadata'] | |
return METAINFO(data=res_data) | |
except Exception as e: | |
sys.stderr.write("PlexPy API 'get_get_metadata' request failed: {0}.".format(e)) | |
def get_get_libraries_table(): | |
# Get the data on the PlexPy libraries table. | |
payload = {'apikey': PLEXPY_APIKEY, | |
'cmd': 'get_libraries_table'} | |
try: | |
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) | |
response = r.json() | |
res_data = response['response']['data']['data'] | |
return [d['section_id'] for d in res_data if d['section_name'] in LIBRARY_NAMES] | |
except Exception as e: | |
sys.stderr.write("PlexPy API 'get_libraries_table' request failed: {0}.".format(e)) | |
def update_library_media_info(section_id): | |
# Get the data on the PlexPy media info tables. | |
payload = {'apikey': PLEXPY_APIKEY, | |
'cmd': 'get_library_media_info', | |
'section_id': section_id, | |
'refresh': True} | |
try: | |
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) | |
response = r.status_code | |
if response != 200: | |
print(r.content) | |
except Exception as e: | |
sys.stderr.write("PlexPy API 'get_get_library_media_info' request failed: {0}.".format(e)) | |
def get_pms_image_proxy(thumb): | |
# Get the data on the PlexPy libraries table. | |
payload = {'apikey': PLEXPY_APIKEY, | |
'cmd': 'pms_image_proxy', | |
'img': thumb, | |
'fallback': 'art'} | |
try: | |
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload, stream=True) | |
return r.url | |
except Exception as e: | |
sys.stderr.write("PlexPy API 'pms_image_proxy' request failed: {0}.".format(e)) | |
def send_notification(BODY_TEXT): | |
# 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 PlexPy | |
payload = {'apikey': PLEXPY_APIKEY, | |
'cmd': 'notify', | |
'agent_id': AGENT_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: | |
raise Exception(response['response']['message']) | |
except Exception as e: | |
sys.stderr.write("PlexPy API 'notify' request failed: {0}.".format(e)) | |
return None | |
recent_lst = [] | |
notify_lst = [] | |
# Find the libraries from LIBRARY_NAMES | |
glt = [lib for lib in get_get_libraries_table()] | |
# Updating media info for libraries. | |
[update_library_media_info(i) for i in glt] | |
for i in glt: | |
recent_lst += get_get_recent(i) | |
for i in recent_lst: | |
meta = get_get_metadata(str(i)) | |
if LASTWEEK <= int(meta.added_at) <= TODAY: | |
added = time.ctime(float(meta.added_at)) | |
if meta.grandparent_title == '' or meta.media_type == 'movie': | |
# Movies | |
notify_lst += [u"<dt>{x.title} ({x.rating_key}) was added {when}.<br>" | |
.format(x=meta, when=added)] | |
else: | |
# Shows | |
notify_lst += [u"<dt>{x.grandparent_title}: {x.title} ({x.rating_key}) was added {when}.<br>" | |
.format(x=meta, when=added)] | |
BODY_TEXT = """\ | |
<html> | |
<head></head> | |
<body> | |
<p>Hi!<br> | |
<br>Below is the list of items added to {LIBRARY_NAMES} last week.<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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment