Skip to content

Commit

Permalink
renamed send and subscribe to raw_x, added classes and client functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevalicjus committed Jul 27, 2022
1 parent 4b248e7 commit 53fcc60
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 32 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = ntfpy
version = 0.0.5
version = 0.0.7
author = Nevalicjus
author_email = [email protected]
description = Ntfy.sh process wrapper
Expand Down
6 changes: 4 additions & 2 deletions src/ntfpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .send import send
from .classes import *

from .subscribe import subscribe
from .raw_send import *

from .raw_subscribe import *
91 changes: 91 additions & 0 deletions src/ntfpy/classes.py
Original file line number Diff line number Diff line change
@@ -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
14 changes: 7 additions & 7 deletions src/ntfpy/send.py → src/ntfpy/raw_send.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
r = requests.post(f"{server}/{topic}", headers = headers, data = message)
32 changes: 32 additions & 0 deletions src/ntfpy/raw_subscribe.py
Original file line number Diff line number Diff line change
@@ -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)
22 changes: 0 additions & 22 deletions src/ntfpy/subscribe.py

This file was deleted.

0 comments on commit 53fcc60

Please sign in to comment.