-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
renamed send and subscribe to raw_x, added classes and client functions
- Loading branch information
1 parent
4b248e7
commit 53fcc60
Showing
6 changed files
with
135 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.