From 89083156653c91788b84f5eaad793c5797e9b58f Mon Sep 17 00:00:00 2001 From: jblu Date: Mon, 5 Jun 2023 22:30:53 -0500 Subject: [PATCH] added apprise support #15 --- AppriseClient.py | 40 ++++++++++++++++++++++++++++++++++++++++ clogging.py | 12 +++++++++--- crane.py | 18 +++++++++++++----- 3 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 AppriseClient.py diff --git a/AppriseClient.py b/AppriseClient.py new file mode 100644 index 0000000..f4d821f --- /dev/null +++ b/AppriseClient.py @@ -0,0 +1,40 @@ +import requests as r +from tomllib import load +import os + +def apprise_notify(req_obj, host, port, aurls, title, body): + payload = {'urls': aurls,'title': title,'body': body,} + url = f'http://{host}:{port}/notify/' + apprise_response = req_obj.post(url, json = payload ,verify=False) + return apprise_response + +class AppriseClient: + def __init__(self): + self.config = '' + try: + if os.environ["DOCKER"]: + self.host = os.environ["host"] + self.port = os.environ["port"] + self.aurls = os.environ["aurls"] + self.title = os.environ["title"] + self.body = os.environ["body"] + if os.environ["toml_path"]: + config_file_path=os.environ["toml_path"] + with open(config_file_path, 'rb') as c: + self.config = load(c) + except: + KeyError + if os.path.exists('./config.toml'): + config_file_path = './config.toml' + with open(config_file_path, 'rb') as c: + self.config = load(c) + if self.config: + self.host = self.config["apprise"]["host"] + self.port = self.config["apprise"]["port"] + self.aurls = self.config["apprise"]["aurls"] + self.title = self.config["apprise"]["title"] + self.body = self.config["apprise"]["body"] + self.apprise_response = apprise_notify(r,self.host,self.port,self.aurls,self.title,self.body) + +if __name__ == "__main__": + AppriseClient() \ No newline at end of file diff --git a/clogging.py b/clogging.py index ea5c55d..a1167ce 100644 --- a/clogging.py +++ b/clogging.py @@ -16,11 +16,15 @@ def cont_notify(self): if self.use_pushover: self.poc = self.po.Pushover(self.po_token) -def cont_notify_summary(self): +def cont_notify_summary(self, app_obj, req_obj): """Main notification method when the app is used in an automated fashion""" + title = "--- crane summary ---" + body = f'{self.extm}' if self.use_pushover: - self.poc.message(self.po_key,f" \ - {self.extm}", title="--- crane summary ---") + #TODO figure out why the flip it thinks its getting 4 pos args + self.poc.message(self.po_key, message=body, title=title) + if self.use_apprise: + app_obj(req_obj, self.apprise_host, self.apprise_port, self.apprise_aurls, title, body) def list_first_cont(self, index=0): """Only lists the first torrent""" @@ -35,4 +39,6 @@ def get_script_runtime(self): if self.use_log: self.tl.info(f'Execution time: [{elapsed_time}]') if self.use_pushover: + self.extm = f"Execution time: [{elapsed_time}]" + if self.use_apprise: self.extm = f"Execution time: [{elapsed_time}]" \ No newline at end of file diff --git a/crane.py b/crane.py index a87d9d1..dbedac2 100644 --- a/crane.py +++ b/crane.py @@ -9,16 +9,17 @@ import logging import requests from tomllib import load import os +from AppriseClient import apprise_notify class Crn: def __init__(self): """Main object, should be calling functions from qlist.py, qlogging.py and qprocess.py""" # Open the config. Needs a json file with the data in config.json.example self.st = datetime.datetime.now() - - config_file_path=os.environ["toml_path"] - with open(config_file_path, 'rb') as c: - self.config = load(c) + if os.getenv("toml_path"): + config_file_path=os.getenv("toml_path") + with open(config_file_path, 'rb') as c: + self.config = load(c) if os.path.exists('./config.toml'): config_file_path = './config.toml' with open(config_file_path, 'rb') as c: @@ -45,6 +46,11 @@ class Crn: self.use_pushover = self.config["pushover"]["use_pushover"] self.po_key = self.config["pushover"]["po_key"] self.po_token = self.config["pushover"]["po_token"] + #apprise + self.use_apprise = self.config["apprise"]["use_apprise"] + self.apprise_host = self.config["apprise"]["host"] + self.apprise_port = self.config["apprise"]["port"] + self.apprise_aurls = self.config["apprise"]["aurls"] #containers self.observed_containers = list(self.config["containers"].values()) self.container_statuses = ["paused","dead","created","exited","removing","restarting","created"] @@ -71,7 +77,9 @@ class Crn: self.et = datetime.datetime.now() get_script_runtime(self) if self.use_pushover: - cont_notify_summary(self) + cont_notify_summary(self, apprise_notify, requests) + if self.use_apprise: + cont_notify_summary(self, apprise_notify, requests) # Run if __name__== "__main__": Crn() \ No newline at end of file