2023-05-31 09:42:57 +08:00
|
|
|
import time
|
2022-10-29 23:19:09 +08:00
|
|
|
from json import JSONDecodeError
|
2021-12-17 22:19:40 +08:00
|
|
|
|
2022-10-29 23:19:09 +08:00
|
|
|
from modules import util
|
2021-12-17 22:19:40 +08:00
|
|
|
from modules.util import Failed
|
|
|
|
|
2022-08-15 09:41:33 +08:00
|
|
|
logger = util.logger
|
2021-12-17 22:19:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Notifiarr:
|
2023-04-01 02:37:33 +08:00
|
|
|
"""Notifiarr API"""
|
|
|
|
|
|
|
|
BASE_URL = "https://notifiarr.com/api"
|
|
|
|
API_VERSION = "v1"
|
|
|
|
|
2021-12-17 22:19:40 +08:00
|
|
|
def __init__(self, config, params):
|
2023-04-01 02:37:33 +08:00
|
|
|
"""Initialize Notifiarr API"""
|
2021-12-17 22:19:40 +08:00
|
|
|
self.config = config
|
|
|
|
self.apikey = params["apikey"]
|
2022-10-27 04:11:38 +08:00
|
|
|
self.header = {"X-API-Key": self.apikey}
|
2021-12-18 09:21:27 +08:00
|
|
|
self.instance = params["instance"]
|
2023-04-01 02:37:33 +08:00
|
|
|
self.url = f"{self.BASE_URL}/{self.API_VERSION}/"
|
2022-08-21 10:17:51 +08:00
|
|
|
logger.secret(self.apikey)
|
2023-04-01 02:37:33 +08:00
|
|
|
response = self.config.get(f"{self.url}user/qbitManage/", headers=self.header, params={"fetch": "settings"})
|
2022-03-18 05:51:01 +08:00
|
|
|
response_json = None
|
2021-12-20 23:11:52 +08:00
|
|
|
try:
|
|
|
|
response_json = response.json()
|
2021-12-20 23:28:29 +08:00
|
|
|
except JSONDecodeError as e:
|
2022-10-27 04:11:38 +08:00
|
|
|
logger.debug(e)
|
|
|
|
raise Failed("Notifiarr Error: Invalid response")
|
2021-12-17 22:19:40 +08:00
|
|
|
if response.status_code >= 400 or ("result" in response_json and response_json["result"] == "error"):
|
|
|
|
logger.debug(f"Response: {response_json}")
|
|
|
|
raise Failed(f"({response.status_code} [{response.reason}]) {response_json}")
|
2022-10-27 04:11:38 +08:00
|
|
|
if not response_json["details"]["response"]:
|
2021-12-17 22:19:40 +08:00
|
|
|
raise Failed("Notifiarr Error: Invalid apikey")
|
|
|
|
|
2022-10-27 04:11:38 +08:00
|
|
|
def notification(self, json):
|
2023-04-01 02:37:33 +08:00
|
|
|
"""Send notification to Notifiarr"""
|
2022-10-27 04:11:38 +08:00
|
|
|
params = {"qbit_client": self.config.data["qbt"]["host"], "instance": self.instance}
|
2023-05-31 09:42:57 +08:00
|
|
|
response = self.config.get(f"{self.url}notification/qbitManage/", json=json, headers=self.header, params=params)
|
|
|
|
time.sleep(1) # Pause for 1 second before sending the next request
|
|
|
|
return response
|