From 03648f9ae2f75ac6ab9aed14550cad9fd74da836 Mon Sep 17 00:00:00 2001 From: jblu Date: Tue, 16 May 2023 22:05:12 -0500 Subject: [PATCH] added apprise client --- AppriseClient.py | 40 ++++++++++++++++++++++++++++++++++++++++ config.toml.example | 5 +++++ qbit-maid.py | 10 ++++++++++ qlogging.py | 15 +++++++++++++++ 4 files changed, 70 insertions(+) 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/config.toml.example b/config.toml.example index 19a33ba..18c28ac 100644 --- a/config.toml.example +++ b/config.toml.example @@ -23,6 +23,11 @@ use_pushover = false po_key = "" po_token = "" +[apprise] +host = "192.168.x.x" +port = 8088 +aurls = 'mailto://user:pass@gmail.com' + [dragnet] enable_dragnet = true dragnet_outfile = "./orphaned.csv" diff --git a/qbit-maid.py b/qbit-maid.py index 42afb98..5043d82 100644 --- a/qbit-maid.py +++ b/qbit-maid.py @@ -4,11 +4,13 @@ from tomllib import load from qlist import * from qlogging import * from qprocess import * +from AppriseClient import apprise_notify import time import datetime import logging from collections import Counter import csv +import requests as r class Qbt: def __init__(self): @@ -52,6 +54,12 @@ class Qbt: 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"] + #dragnet self.enable_dragnet = self.config["dragnet"]["enable_dragnet"] self.dragnet_outfile = self.config["dragnet"]["dragnet_outfile"] @@ -104,6 +112,8 @@ class Qbt: get_script_runtime(self) if self.use_pushover: tor_notify_summary(self) + if self.use_apprise: + tor_notify_apprise(self, r, apprise_notify) # Run if __name__== "__main__": Qbt() \ No newline at end of file diff --git a/qlogging.py b/qlogging.py index fef5188..b491859 100644 --- a/qlogging.py +++ b/qlogging.py @@ -16,6 +16,19 @@ def tor_notify(self): if self.use_pushover: self.poc = self.po.Pushover(self.po_token) +def tor_notify_apprise(self, req_obj, app_obj): + """Use apprise""" + body = f" Total: {self.total_torrents}\n\ + Premature: {self.preme_tor_counter}\n\ + Ignored: {self.ignored_counter}\n\ + Protected: {self.c[self.tracker_protected_tag]}\n\ + Non-protected: {self.c[self.tracker_non_protected_tag]}\n\ + Orphaned: {self.up_tor_counter}\n\ + Marked for deletion: {len(self.torrent_hash_delete_list)}\n\ + {self.extm}" + title = "--- qbit-maid summary ---" + app_obj(req_obj, self.apprise_host, self.apprise_port, self.apprise_aurls, title, body) + def tornotifytest(self): """Used to make sure tornotify is working and messages are getting to the client""" self.poc.message(self.po_key, "Test Message", title="qbit-maid") @@ -69,4 +82,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 -- 2.45.2