Plex-Meta-Manager/modules/webhooks.py

100 lines
5.2 KiB
Python
Raw Normal View History

2021-11-19 19:28:11 +00:00
from json import JSONDecodeError
2021-12-30 21:29:16 +00:00
from modules import util
2021-11-03 14:36:11 +00:00
from modules.util import Failed
logger = util.logger
2021-11-03 14:36:11 +00:00
class Webhooks:
def __init__(self, config, system_webhooks, library=None, notifiarr=None):
self.config = config
self.error_webhooks = system_webhooks["error"] if "error" in system_webhooks else []
self.run_start_webhooks = system_webhooks["run_start"] if "run_start" in system_webhooks else []
self.run_end_webhooks = system_webhooks["run_end"] if "run_end" in system_webhooks else []
2021-11-03 14:36:11 +00:00
self.library = library
self.notifiarr = notifiarr
def _request(self, webhooks, json):
if self.config.trace_mode:
logger.separator("Webhooks", space=False, border=False)
2021-11-03 14:36:11 +00:00
logger.debug("")
logger.debug(f"JSON: {json}")
2021-11-16 16:44:03 +00:00
for webhook in list(set(webhooks)):
2021-12-20 14:50:17 +00:00
response = None
2021-11-03 14:36:11 +00:00
if self.config.trace_mode:
logger.debug(f"Webhook: {webhook}")
if webhook == "notifiarr":
2021-12-20 14:47:50 +00:00
if self.notifiarr:
url, params = self.notifiarr.get_url("notification/pmm/")
for x in range(6):
response = self.config.get(url, json=json, params=params)
if response.status_code < 500:
break
2021-11-03 14:36:11 +00:00
else:
response = self.config.post(webhook, json=json)
2021-12-20 14:50:17 +00:00
if response:
try:
response_json = response.json()
if self.config.trace_mode:
logger.debug(f"Response: {response_json}")
if "result" in response_json and response_json["result"] == "error" and "details" in response_json and "response" in response_json["details"]:
raise Failed(f"Notifiarr Error: {response_json['details']['response']}")
if response.status_code >= 400 or ("result" in response_json and response_json["result"] == "error"):
raise Failed(f"({response.status_code} [{response.reason}]) {response_json}")
except JSONDecodeError:
if response.status_code >= 400:
raise Failed(f"({response.status_code} [{response.reason}])")
2021-11-03 14:36:11 +00:00
def start_time_hooks(self, start_time):
if self.run_start_webhooks:
2021-12-01 05:38:20 +00:00
self._request(self.run_start_webhooks, {"start_time": start_time.strftime("%Y-%m-%d %H:%M:%S")})
2021-11-03 14:36:11 +00:00
2021-12-01 05:38:20 +00:00
def end_time_hooks(self, start_time, end_time, run_time, stats):
2021-11-03 14:36:11 +00:00
if self.run_end_webhooks:
self._request(self.run_end_webhooks, {
2021-12-01 05:38:20 +00:00
"start_time": start_time.strftime("%Y-%m-%d %H:%M:%S"),
"end_time": end_time.strftime("%Y-%m-%d %H:%M:%S"),
2021-11-03 14:36:11 +00:00
"run_time": run_time,
"collections_created": stats["created"],
"collections_modified": stats["modified"],
"collections_deleted": stats["deleted"],
"items_added": stats["added"],
"items_removed": stats["removed"],
"added_to_radarr": stats["radarr"],
"added_to_sonarr": stats["sonarr"],
"names": stats["names"]
2021-11-03 14:36:11 +00:00
})
2021-12-17 14:24:46 +00:00
def error_hooks(self, text, server=None, library=None, collection=None, playlist=None, critical=True):
2021-11-03 14:36:11 +00:00
if self.error_webhooks:
json = {"error": str(text), "critical": critical}
2021-12-23 17:02:07 +00:00
if server: json["server_name"] = str(server)
if library: json["library_name"] = str(library)
if collection: json["collection"] = str(collection)
if playlist: json["playlist"] = str(playlist)
2021-11-03 14:36:11 +00:00
self._request(self.error_webhooks, json)
def collection_hooks(self, webhooks, collection, poster_url=None, background_url=None, created=False, deleted=False,
additions=None, removals=None, radarr=None, sonarr=None, playlist=False):
2021-11-03 14:36:11 +00:00
if self.library:
thumb = None
if not poster_url and collection.thumb and next((f for f in collection.fields if f.name == "thumb"), None):
2021-11-03 14:36:11 +00:00
thumb = self.config.get_image_encoded(f"{self.library.url}{collection.thumb}?X-Plex-Token={self.library.token}")
art = None
if not playlist and not background_url and collection.art and next((f for f in collection.fields if f.name == "art"), None):
2021-11-03 14:36:11 +00:00
art = self.config.get_image_encoded(f"{self.library.url}{collection.art}?X-Plex-Token={self.library.token}")
self._request(webhooks, {
2021-11-03 14:36:11 +00:00
"server_name": self.library.PlexServer.friendlyName,
"library_name": self.library.name,
2021-12-17 14:24:46 +00:00
"playlist" if playlist else "collection": collection.title,
2021-11-03 14:36:11 +00:00
"created": created,
"deleted": deleted,
2021-11-03 14:36:11 +00:00
"poster": thumb,
"background": art,
"poster_url": poster_url,
"background_url": background_url,
"additions": additions if additions else [],
"removals": removals if removals else [],
"radarr_adds": radarr if radarr else [],
"sonarr_adds": sonarr if sonarr else [],
})