pywebpush 2.1.0__1-py3-none-any.whl

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.
pywebpush/__main__.py ADDED
@@ -0,0 +1,93 @@
1
+ import argparse
2
+ import os
3
+ import json
4
+ import logging
5
+
6
+ from requests import JSONDecodeError
7
+
8
+ from pywebpush import webpush, WebPushException
9
+
10
+
11
+ def get_config():
12
+ parser = argparse.ArgumentParser(description="WebPush tool")
13
+ parser.add_argument("--data", "-d", help="Data file")
14
+ parser.add_argument("--info", "-i", help="Subscription Info JSON file")
15
+ parser.add_argument("--head", help="Header Info JSON file")
16
+ parser.add_argument("--claims", help="Vapid claim file")
17
+ parser.add_argument("--key", help="Vapid private key file path")
18
+ parser.add_argument(
19
+ "--curl",
20
+ help="Don't send, display as curl command",
21
+ default=False,
22
+ action="store_true",
23
+ )
24
+ parser.add_argument("--encoding", default="aes128gcm")
25
+ parser.add_argument(
26
+ "--verbose",
27
+ "-v",
28
+ help="Provide verbose feedback",
29
+ default=False,
30
+ action="store_true",
31
+ )
32
+
33
+ args = parser.parse_args()
34
+
35
+ if not args.info:
36
+ raise WebPushException("Subscription Info argument missing.")
37
+ if not os.path.exists(args.info):
38
+ raise WebPushException("Subscription Info file missing.")
39
+ try:
40
+ with open(args.info) as r:
41
+ try:
42
+ args.sub_info = json.loads(r.read())
43
+ except JSONDecodeError as e:
44
+ raise WebPushException(
45
+ "Could not read the subscription info file: {}", e
46
+ )
47
+ if args.data:
48
+ with open(args.data) as r:
49
+ args.data = r.read()
50
+ if args.head:
51
+ with open(args.head) as r:
52
+ try:
53
+ args.head = json.loads(r.read())
54
+ except JSONDecodeError as e:
55
+ raise WebPushException("Could not read the header arguments: {}", e)
56
+ if args.claims:
57
+ if not args.key:
58
+ raise WebPushException("No private --key specified for claims")
59
+ with open(args.claims) as r:
60
+ try:
61
+ args.claims = json.loads(r.read())
62
+ except JSONDecodeError as e:
63
+ raise WebPushException(
64
+ "Could not read the VAPID claims file {}".format(e)
65
+ )
66
+ except Exception as ex:
67
+ logging.error("Couldn't read input {}.".format(ex))
68
+ raise ex
69
+ return args
70
+
71
+
72
+ def main():
73
+ """Send data"""
74
+
75
+ try:
76
+ args = get_config()
77
+ result = webpush(
78
+ args.sub_info,
79
+ data=args.data,
80
+ vapid_private_key=args.key,
81
+ vapid_claims=args.claims,
82
+ curl=args.curl,
83
+ content_encoding=args.encoding,
84
+ verbose=args.verbose,
85
+ headers=args.head,
86
+ )
87
+ print(result)
88
+ except Exception as ex:
89
+ logging.error("{}".format(ex))
90
+
91
+
92
+ if __name__ == "__main__":
93
+ main()
pywebpush/foo.py ADDED
@@ -0,0 +1,51 @@
1
+ from pywebpush import webpush
2
+ import json
3
+ import logging
4
+ import datetime
5
+
6
+
7
+ def send_push_notification(subscription, payload):
8
+
9
+ try:
10
+
11
+ # subscriptionData = json.loads(subscription)
12
+
13
+ # logger.error(subscriptionData)
14
+
15
+ webpush(
16
+ subscription_info={
17
+ "endpoint": subscription["endpoint"],
18
+ "keys": subscription["keys"],
19
+ },
20
+ data=json.dumps(payload),
21
+ vapid_claims={
22
+ "aud": "https://eshopper.africa",
23
+ "exp": int((datetime.datetime.now().timestamp())) + 86400,
24
+ "sub": "mailto:events@eshopper.africa",
25
+ },
26
+ vapid_private_key="UCUKEHn7Jd33QZx5lJFKBY4plOxGsJ6xJSOzE14jQlo",
27
+ )
28
+
29
+ # subscription_info = { 'endpoint': subscription['endpoint'], 'keys': subscription['keys'] },
30
+
31
+ # data = json.loads(payload),
32
+
33
+ # headers = {}
34
+
35
+ # ttl = 0
36
+
37
+ # gcm_key = ''
38
+
39
+ # content_encoding="aes128gcm"
40
+
41
+ # reg_id=""
42
+
43
+ # WebPusher = webpush(subscription_info)
44
+
45
+ # WebPusher(subscription_info).send(data, headers, ttl, gcm_key, reg_id, content_encoding, timeout=None)
46
+
47
+ except Exception as inst:
48
+ print(f" webpush Notification Error : {inst}")
49
+
50
+
51
+ send_push_notification({"endpoint": "https://example.com", "keys": {}}, "laaaa")
File without changes