Created
February 14, 2017 18:54
-
-
Save blacktwin/fa158ed0ee44ec176c0f052b9974bc68 to your computer and use it in GitHub Desktop.
Send IFTTT notification. Ignore any titles that are listed.
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
# 1. Install the requests module for python. | |
# pip install requests | |
# 2. Add script arguments in PlexPy. | |
# {server_name} {user} {title} {player} {platform} | |
import requests | |
import sys | |
server_name = sys.argv[1] | |
user = sys.argv[2] | |
title = sys.argv[3] | |
player = sys.argv[4] | |
platform = sys.argv[5] | |
SUBJECT_TEXT = "PlexPy Notification" | |
BODY_TEXT = "{}: - {} started playing {} on {} ({})".format(server_name, user, title, player, platform) | |
## EDIT THESE SETTINGS ## | |
PLEXPY_APIKEY = 'XXXXXX' # Your PlexPy API key | |
PLEXPY_URL = 'http://localhost:8181/' # Your PlexPy URL | |
AGENT_ID = 12 # The notification agent ID for IFTTT | |
TITLE_IGNORE = [''] # Title to ignore ['Snow White'] | |
if title in TITLE_IGNORE: | |
sys.stdout.write("Notification of {} being ignored.".format(title)) | |
exit() | |
def send_notification(SUBJECT_TEXT, 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 | |
send_notification(SUBJECT_TEXT, BODY_TEXT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment