From 45185d9ad845541df1e3100f192c07e6e1b512fc Mon Sep 17 00:00:00 2001 From: Blacktwin Date: Tue, 16 Apr 2019 10:13:54 -0400 Subject: [PATCH] Create new script #152 --- notify/notify_recently_aired.py | 86 +++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 notify/notify_recently_aired.py diff --git a/notify/notify_recently_aired.py b/notify/notify_recently_aired.py new file mode 100644 index 0000000..1efcd38 --- /dev/null +++ b/notify/notify_recently_aired.py @@ -0,0 +1,86 @@ +""" +Description: Notify only if recently aired/released +Author: Blacktwin +Requires: requests + +Enabling Scripts in Tautulli: +Tautulli > Settings > Notification Agents > Add a Notification Agent > Script + +Configuration: +Tautulli > Settings > Notification Agents > New Script > Configuration: + + Script Name: notify_recently_aired.py + Set Script Timeout: Default + Description: Notify only if recently aired/released + Save + +Triggers: +Tautulli > Settings > Notification Agents > New Script > Triggers: + + Check: Recently Added + Save + +Conditions: +Tautulli > Settings > Notification Agents > New Script > Conditions: + + Set Conditions: [{condition} | {operator} | {value} ] + Save + +Script Arguments: +Tautulli > Settings > Notification Agents > New Script > Script Arguments: + + Select: Recently Added + Arguments: {air_date} or {release_date} {rating_key} + + Save + Close + +Note: + You'll need another notification agent to use for actually sending the notification. + The notifier_id in the edit section will need to be this other notification agent you intend to use. + It does not have to be an active notification agent, just setup. +""" +import os +import sys +import requests +from datetime import date +from datetime import datetime + +TAUTULLI_URL = '' +TAUTULLI_APIKEY = '' +TAUTULLI_URL = os.getenv('TAUTULLI_URL', TAUTULLI_URL) +TAUTULLI_APIKEY = os.getenv('TAUTULLI_APIKEY', TAUTULLI_APIKEY) + +# Edit +date_format = "%Y-%m-%d" +RECENT_DAYS = 3 +NOTIFIER_ID = 34 +# /Edit + +air_date = sys.argv[1] +rating_key = int(sys.argv[2]) + +aired_date = datetime.strptime(air_date, date_format) +today = date.today() +delta = today - aired_date.date() + +def notify_recently_added(rating_key, notifier_id): + # Get the metadata for a media item. + payload = {'apikey': TAUTULLI_APIKEY, + 'rating_key': rating_key, + 'notifier_id': notifier_id, + 'cmd': 'notify_recently_added'} + + try: + r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload) + response = r.json() + sys.stdout.write(response["response"]["message"]) + + except Exception as e: + sys.stderr.write("Tautulli API 'notify_recently_added' request failed: {0}.".format(e)) + pass + +if delta.days < RECENT_DAYS: + notify_recently_added(rating_key, NOTIFIER_ID) +else: + print("Not recent enough, no notification to be sent.") \ No newline at end of file