python-ntfy 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,69 @@
1
+ Metadata-Version: 2.1
2
+ Name: python-ntfy
3
+ Version: 0.1.0
4
+ Summary: An ntfy library aiming for feature completeness
5
+ Author: Matthew Cane
6
+ Author-email: matthew.cane0@gmail.com
7
+ Requires-Python: >=3.11,<4.0
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.11
10
+ Requires-Dist: requests (>=2.31.0,<3.0.0)
11
+ Description-Content-Type: text/markdown
12
+
13
+ # ntfy.sh Python Library
14
+
15
+ An easy-to-use ntfy python library. Aiming for full feature support.
16
+
17
+ ## Quickstart
18
+
19
+ 1. Install using pip with `pip3 install python-ntfy`
20
+ 2. If you are using a server that requires auth, set the env vars
21
+ 3. Import the library
22
+
23
+ ```python
24
+ from python_ntfy import NtfyClient
25
+ ```
26
+
27
+ 4. Create an `NtfyClient` instance with a topic
28
+
29
+ ```python
30
+ client = NtfyClient(topic="Your topic")
31
+ ```
32
+
33
+ 5. Send a message
34
+
35
+ ```python
36
+ client.send("Your message here")
37
+ ```
38
+
39
+ ## Supported Features
40
+
41
+ - Username + password auth
42
+ - Custom servers
43
+ - Sending plaintext messages
44
+ - Sending Markdown formatted text messages
45
+ - Retrieving cached messages
46
+ - Scheduled delivery
47
+
48
+ ## Future Features
49
+
50
+ - Access token auth
51
+ - Email notifications
52
+ - Tags
53
+ - Action buttons
54
+ - Send to multiple topics at once
55
+
56
+ ## Test and Development
57
+
58
+ This project uses Poetry.
59
+
60
+ ### Tests
61
+
62
+ This project is aiming for 95% code coverage. Any added features must include comprihensive tests.
63
+
64
+ To run tests:
65
+
66
+ 1. `poetry install --include test`
67
+ 2. Add username and password for ntfy.sh to `.env`
68
+ 3. `poetry run pytest --cov`
69
+
@@ -0,0 +1,56 @@
1
+ # ntfy.sh Python Library
2
+
3
+ An easy-to-use ntfy python library. Aiming for full feature support.
4
+
5
+ ## Quickstart
6
+
7
+ 1. Install using pip with `pip3 install python-ntfy`
8
+ 2. If you are using a server that requires auth, set the env vars
9
+ 3. Import the library
10
+
11
+ ```python
12
+ from python_ntfy import NtfyClient
13
+ ```
14
+
15
+ 4. Create an `NtfyClient` instance with a topic
16
+
17
+ ```python
18
+ client = NtfyClient(topic="Your topic")
19
+ ```
20
+
21
+ 5. Send a message
22
+
23
+ ```python
24
+ client.send("Your message here")
25
+ ```
26
+
27
+ ## Supported Features
28
+
29
+ - Username + password auth
30
+ - Custom servers
31
+ - Sending plaintext messages
32
+ - Sending Markdown formatted text messages
33
+ - Retrieving cached messages
34
+ - Scheduled delivery
35
+
36
+ ## Future Features
37
+
38
+ - Access token auth
39
+ - Email notifications
40
+ - Tags
41
+ - Action buttons
42
+ - Send to multiple topics at once
43
+
44
+ ## Test and Development
45
+
46
+ This project uses Poetry.
47
+
48
+ ### Tests
49
+
50
+ This project is aiming for 95% code coverage. Any added features must include comprihensive tests.
51
+
52
+ To run tests:
53
+
54
+ 1. `poetry install --include test`
55
+ 2. Add username and password for ntfy.sh to `.env`
56
+ 3. `poetry run pytest --cov`
@@ -0,0 +1,20 @@
1
+ [tool.poetry]
2
+ name = "python-ntfy"
3
+ version = "0.1.0"
4
+ description = "An ntfy library aiming for feature completeness"
5
+ authors = ["Matthew Cane <matthew.cane0@gmail.com>"]
6
+ readme = "README.md"
7
+
8
+ [tool.poetry.dependencies]
9
+ python = "^3.11"
10
+ requests = "^2.31.0"
11
+
12
+ [tool.poetry.group.test.dependencies]
13
+ pytest = "^7.4.1"
14
+ python-dotenv = "^1.0.0"
15
+ pytest-asyncio = "^0.21.1"
16
+ pytest-codecov = "^0.5.1"
17
+
18
+ [build-system]
19
+ requires = ["poetry-core"]
20
+ build-backend = "poetry.core.masonry.api"
@@ -0,0 +1,40 @@
1
+ import os
2
+
3
+
4
+ class NtfyClient:
5
+ from ._send_functions import send, send_file
6
+ from ._get_functions import get_cached_messages
7
+
8
+ def __init__(
9
+ self,
10
+ topic: str,
11
+ server: str = "https://ntfy.sh",
12
+ ):
13
+ """
14
+ :param topic: The topic to use for this client
15
+ :param server: The server to connect to. Must include the protocol (http/https)
16
+ :return:
17
+ """
18
+ self._server = server
19
+ self._topic = topic
20
+ self.__set_url(server, topic)
21
+
22
+ if (user := os.environ.get("NTFY_USER")) and (
23
+ password := os.environ.get("NTFY_PASSWORD")
24
+ ):
25
+ self._auth = (user, password)
26
+ else:
27
+ self._auth = ("", "")
28
+
29
+ def __set_url(self, server, topic):
30
+ self.url = server.strip("/") + "/" + topic
31
+
32
+ def set_topic(self, topic: str):
33
+ """
34
+ Set a new topic for the client
35
+
36
+ :param topic: The topic to use for this client
37
+ :return: None
38
+ """
39
+ self._topic = topic
40
+ self.__set_url(self._server, self._topic)
@@ -0,0 +1,32 @@
1
+ import json, requests
2
+
3
+
4
+ def get_cached_messages(self, since: str = "all", scheduled: bool = False):
5
+ """
6
+ Get cached messages from the server
7
+
8
+ :param since: The timestamp to start from. If set to "all", will return all messages. Optional
9
+ :param scheduled: If true, will return scheduled messages. Optional
10
+ :return: A list of messages
11
+
12
+ :examples:
13
+ response = client.get(since="all")
14
+ response = client.get(since="all", scheduled=True)
15
+ response = client.get(since="2019-01-01")
16
+ response = client.get(since="2019-01-01", scheduled=True)
17
+ """
18
+
19
+ params = {"poll": "1"}
20
+ if scheduled:
21
+ params.update({"scheduled": scheduled})
22
+ if since:
23
+ params.update({"since": since})
24
+
25
+ response = [
26
+ json.loads(line)
27
+ for line in requests.get(url=self.url + "/json", params=params, auth=self._auth)
28
+ .text.strip()
29
+ .splitlines()
30
+ ]
31
+ # Reverse the list so that the most recent notification is first
32
+ return sorted(response, key=lambda x: x["time"], reverse=True)
@@ -0,0 +1,48 @@
1
+ import json, requests
2
+
3
+
4
+ def send(self, message: str, title: str = None, format_as_markdown: bool = False):
5
+ """
6
+ Send a text based message to the server
7
+
8
+ :param message: The message to send
9
+ :param title: The title of the message. Optional
10
+ :param format_as_markdown: If true, the message will be formatted as markdown. Optional
11
+ :return: The response from the server
12
+
13
+ :examples:
14
+ response = client.send(message="Example message")
15
+ response = client.send(message="Example message", title="Example title")
16
+ response = client.send(message="*Example markdown*", format_as_markdown=True)
17
+ """
18
+ headers = {
19
+ "Title": title,
20
+ "Markdown": "true" if format_as_markdown else "false",
21
+ }
22
+
23
+ response = json.loads(
24
+ requests.post(url=self.url, data=message, headers=headers, auth=self._auth).text
25
+ )
26
+ return response
27
+
28
+
29
+ def send_file(self, file: str, title: str = None):
30
+ """
31
+ Send a file to the server
32
+
33
+ :param file_path: The path to the file to send
34
+ :param title: The title of the file. Optional
35
+ :return: The response from the server
36
+
37
+ :examples:
38
+ response = client.send_file(file_path="example.txt")
39
+ """
40
+ headers = {"Title": title, "Filename": file.split("/")[-1]}
41
+
42
+ with open(file, "rb") as file:
43
+ response = json.loads(
44
+ requests.post(
45
+ url=self.url, data=file, headers=headers, auth=self._auth
46
+ ).text
47
+ )
48
+ return response