-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify-ntfy
executable file
·92 lines (75 loc) · 2.22 KB
/
notify-ntfy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
import argparse
import pathlib
import socket
import sys
import textwrap
from typing import Mapping
try:
import requests
except ImportError as exc:
print(f'{pathlib.Path(__file__).name}:', exc.msg)
sys.exit(1)
import logging
logging.basicConfig(level=logging.ERROR)
log = logging.getLogger(__name__)
# TODO)):
# tags: https://docs.ntfy.sh/emojis/
PROG = pathlib.Path(__file__).name
HELP = textwrap.dedent(
f"""{PROG}: [Options]
Send a message to ntfy.sh
arguments:
-c, --topic Topic
-t, --title Title of message
-m, --mesg Message to send
-T, --tags Tags to send (default: clipboard)
-p, --priority Priority of message (default: default)
-h, --help Show this help message and exit"""
)
def send_notify(
topic: str, title: str, msg: str, priority: str, tags: str
) -> None:
headers: Mapping[str, str | bytes | None] = {
'Title': title.encode(encoding='utf-8'),
'Priority': priority,
'Markdown': 'yes',
'Tags': tags,
}
url = f'https://ntfy.sh/{topic}'
requests.post(
url,
data=msg.encode(encoding='utf-8'),
headers=headers,
timeout=10,
)
def setup_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(epilog=HELP, add_help=False)
parser.add_argument('-c', '--topic')
parser.add_argument('-m', '--mesg')
parser.add_argument('-t', '--title', default=socket.gethostname())
parser.add_argument('-T', '--tags', type=str, default='clipboard')
parser.add_argument(
'-p',
'--priority',
default='default',
choices=['default', 'high', 'urgent'],
)
parser.add_argument('-h', '--help', action='store_true')
return parser.parse_args()
def main() -> int:
args = setup_args()
try:
if args.help or args.mesg is None:
print(HELP)
return 0
if args.topic is None:
err = f'{PROG}: missing ntfy.sh <topic>'
log.error(err)
return 1
send_notify(args.topic, args.title, args.mesg, args.priority, args.tags)
except KeyboardInterrupt:
log.info('terminated by user')
return 0
if __name__ == '__main__':
sys.exit(main())