From 53fcc6056243a64715bc70922e2f272e6ea95f4e Mon Sep 17 00:00:00 2001 From: Maciej Bromirski <50320623+Nevalicjus@users.noreply.github.com> Date: Wed, 27 Jul 2022 04:04:49 +0200 Subject: [PATCH] renamed send and subscribe to raw_x, added classes and client functions --- setup.cfg | 2 +- src/ntfpy/__init__.py | 6 +- src/ntfpy/classes.py | 91 ++++++++++++++++++++++++++++++ src/ntfpy/{send.py => raw_send.py} | 14 ++--- src/ntfpy/raw_subscribe.py | 32 +++++++++++ src/ntfpy/subscribe.py | 22 -------- 6 files changed, 135 insertions(+), 32 deletions(-) create mode 100644 src/ntfpy/classes.py rename src/ntfpy/{send.py => raw_send.py} (70%) create mode 100644 src/ntfpy/raw_subscribe.py delete mode 100644 src/ntfpy/subscribe.py diff --git a/setup.cfg b/setup.cfg index 846303a..82c636b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = ntfpy -version = 0.0.5 +version = 0.0.7 author = Nevalicjus author_email = nevalicjus@gmail.com description = Ntfy.sh process wrapper diff --git a/src/ntfpy/__init__.py b/src/ntfpy/__init__.py index 78f23a1..ffa9ac1 100644 --- a/src/ntfpy/__init__.py +++ b/src/ntfpy/__init__.py @@ -1,3 +1,5 @@ -from .send import send +from .classes import * -from .subscribe import subscribe \ No newline at end of file +from .raw_send import * + +from .raw_subscribe import * \ No newline at end of file diff --git a/src/ntfpy/classes.py b/src/ntfpy/classes.py new file mode 100644 index 0000000..7305d8e --- /dev/null +++ b/src/ntfpy/classes.py @@ -0,0 +1,91 @@ +import requests +import base64 +import time +import json + +__all__ = [ + "NTFYClient", + "NTFYMessage", + "NTFYServer", + "NTFYUser" +] + +class NTFYClient(): + def __init__(self, server, topic, user = None): + self.server = server + self.topic = topic + self.user = user + + def send(self, message, title = None, priority = None, tags = None, click = None, attach = None, actions = None, email = None, delay = None): + headers = {} + if self.user != None: + auth = f"{self.user.username}:{self.user.password}" + auth_bytes = auth.encode("ascii") + b64_bytes = base64.b64encode(auth_bytes) + b64_s = b64_bytes.decode("ascii") + headers["Authorization"] = f"Basic {b64_s}" + if title != None: + headers["Title"] = title + if priority != None: + headers["Priority"] = priority + if tags != None: + headers["Tags"] = tags + if click != None: + headers["Click"] = click + if attach != None: + headers["Attach"] = attach + if actions != None: + headers["Actions"] = actions + if email != None: + headers["Email"] = email + if delay != None: + headers["Delay"] = delay + r = requests.post(f"{self.server.url}/{self.topic}", headers = headers, data = message) + + async def subscribe(self): + headers = {} + if self.user != None: + auth = f"{self.user.username}:{self.user.password}" + auth_bytes = auth.encode("ascii") + b64_bytes = base64.b64encode(auth_bytes) + b64_s = b64_bytes.decode("ascii") + headers["Authorization"] = f"Basic {b64_s}" + r = requests.get(f"{self.server.url}/{self.topic}/json", stream = True, headers = headers) + for l in r.iter_lines(): + if l: + d = json.loads(l.decode("utf-8")) + if d["event"] == "message": + m = NTFYMessage(d["message"], d["id"], d["time"], d["topic"]) + for x in ["title", "priority", "tags", "click", "attach", "actions", "email", "delay"]: + if x in d: + setattr(m, x, d[x]) + print(m) + else: + print(d) + +class NTFYMessage(): + def __init__(self, message, id, timestamp, topic, title = None, priority = None, tags = None, click = None, attach = None, actions = None, email = None, delay = None): + self.message = message + self.id = id + self.timestamp = timestamp + self.topic = topic + self.title = title + self.priority = priority + self.tags = tags + self.click = click + self.attach = attach + self.actions = actions + self.email = email + self.delay = delay + + def __str__(self): + return f"{self.id} @ {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(self.timestamp))}\n{self.topic}: {self.message}" + +class NTFYServer(): + def __init__(self, url): + self.url = url + +class NTFYUser(): + def __init__(self, username, password): + self.username = username + self.password = password \ No newline at end of file diff --git a/src/ntfpy/send.py b/src/ntfpy/raw_send.py similarity index 70% rename from src/ntfpy/send.py rename to src/ntfpy/raw_send.py index 10f5b50..3908828 100644 --- a/src/ntfpy/send.py +++ b/src/ntfpy/raw_send.py @@ -1,16 +1,16 @@ import requests import base64 -__all__ = ( - 'send' -) +__all__ = [ + "raw_send" +] -def send(server, topic, message, auth = None, title = None, priority = None, tags = None, click = None, attach = None, actions = None, email = None, delay = None): +def raw_send(server, topic, message, auth = None, title = None, priority = None, tags = None, click = None, attach = None, actions = None, email = None, delay = None): headers = {} if auth != None: - auth_bytes = auth.encode('ascii') + auth_bytes = auth.encode("ascii") b64_bytes = base64.b64encode(auth_bytes) - b64_s = b64_bytes.decode('ascii') + b64_s = b64_bytes.decode("ascii") headers["Authorization"] = f"Basic {b64_s}" if title != None: headers["Title"] = title @@ -28,4 +28,4 @@ def send(server, topic, message, auth = None, title = None, priority = None, tag headers["Email"] = email if delay != None: headers["Delay"] = delay - r = requests.post(f"{server}/{topic}", headers = headers, data = message) \ No newline at end of file + r = requests.post(f"{server}/{topic}", headers = headers, data = message) diff --git a/src/ntfpy/raw_subscribe.py b/src/ntfpy/raw_subscribe.py new file mode 100644 index 0000000..b2ac520 --- /dev/null +++ b/src/ntfpy/raw_subscribe.py @@ -0,0 +1,32 @@ +import requests +import base64 +import json + +from .classes import NTFYMessage + +__all__ = [ + "raw_subscribe" +] + +async def raw_subscribe(server, topic, auth = None): + if auth is None: + r = requests.get(f"{server}/{topic}/json", stream = True) + else: + auth_bytes = auth.encode("ascii") + b64_bytes = base64.b64encode(auth_bytes) + b64_s = b64_bytes.decode("ascii") + headers = { + "Authorization": f"Basic {b64_s}" + } + r = requests.get(f"{server}/{topic}/json", stream = True, headers = headers) + for l in r.iter_lines(): + if l: + d = json.loads(l.decode("utf-8")) + if d["event"] == "message": + m = NTFYMessage(d["message"], d["id"], d["time"], d["topic"]) + for x in ["title", "priority", "tags", "click", "attach", "actions", "email", "delay"]: + if x in d: + setattr(m, x, d[x]) + print(m) + else: + print(d) diff --git a/src/ntfpy/subscribe.py b/src/ntfpy/subscribe.py deleted file mode 100644 index 6a31c4c..0000000 --- a/src/ntfpy/subscribe.py +++ /dev/null @@ -1,22 +0,0 @@ -import requests -import base64 -import json - -__all__ = ( - 'subscribe' -) - -async def subscribe(server, topic, auth = None): - if auth is None: - r = requests.get(f"{server}/{topic}/json", stream = True) - else: - auth_bytes = auth.encode('ascii') - b64_bytes = base64.b64encode(auth_bytes) - b64_s = b64_bytes.decode('ascii') - headers = { - "Authorization": f"Basic {b64_s}" - } - r = requests.get(f"{server}/{topic}/json", stream = True, headers = headers) - for l in r.iter_lines(): - if l: - print(json.loads(l.decode('utf-8')))