Skip to content

Commit 0bf1414

Browse files
committed
Fixed issued and optimised code
1 parent e241192 commit 0bf1414

File tree

17 files changed

+41
-196
lines changed

17 files changed

+41
-196
lines changed

__pycache__/app.cpython-35.pyc

-225 Bytes
Binary file not shown.

__pycache__/db.cpython-35.pyc

-1.17 KB
Binary file not shown.

__pycache__/main.cpython-35.pyc

-2.33 KB
Binary file not shown.

__pycache__/push.cpython-35.pyc

-527 Bytes
Binary file not shown.
-759 Bytes
Binary file not shown.

app.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

db.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

db.pyc

-1.5 KB
Binary file not shown.

db.sqlite3

-12 KB
Binary file not shown.

main.py

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
import logging
22
import json, os
3-
from solidwebpush import Pusher
43

5-
from flask import request, Response, render_template, Blueprint, redirect, url_for
6-
from flask import jsonify
4+
from flask import request, Response, render_template, jsonify, Flask
5+
from pywebpush import webpush, WebPushException
76

8-
from flask_api import FlaskAPI
9-
from flask_sqlalchemy import SQLAlchemy
10-
from settings import *
7+
app = Flask(__name__)
8+
app.config['SECRET_KEY'] = '9OLWxND4o83j4K4iuopO'
119

12-
from push import send_web_push
13-
from db import *
10+
DER_BASE64_ENCODED_PRIVATE_KEY_FILE_PATH = os.path.join(os.getcwd(),"private_key.txt")
11+
DER_BASE64_ENCODED_PUBLIC_KEY_FILE_PATH = os.path.join(os.getcwd(),"public_key.txt")
1412

15-
main = Blueprint('main', __name__)
13+
VAPID_PRIVATE_KEY = open(DER_BASE64_ENCODED_PRIVATE_KEY_FILE_PATH, "r+").readline().strip("\n")
14+
VAPID_PUBLIC_KEY = open(DER_BASE64_ENCODED_PUBLIC_KEY_FILE_PATH, "r+").read().strip("\n")
1615

16+
VAPID_CLAIMS = {
17+
"sub": "mailto:develop@raturi.in"
18+
}
1719

18-
@main.route('/')
20+
def send_web_push(subscription_information, message_body):
21+
return webpush(
22+
subscription_info=subscription_information,
23+
data=message_body,
24+
vapid_private_key=VAPID_PRIVATE_KEY,
25+
vapid_claims=VAPID_CLAIMS
26+
)
27+
28+
@app.route('/')
1929
def index():
2030
return render_template('index.html')
2131

22-
@main.route("/subscription/", methods=["GET", "POST"])
32+
@app.route("/subscription/", methods=["GET", "POST"])
2333
def subscription():
2434
"""
2535
POST creates a subscription
@@ -33,23 +43,8 @@ def subscription():
3343
subscription_token = request.get_json("subscription_token")
3444
return Response(status=201, mimetype="application/json")
3545

36-
@main.route("/push/", methods=["POST"])
37-
def push_to_all_users():
38-
token = request.form.get('sub_token')
39-
message = request.form.get('message')
40-
print("token",token)
41-
print("message",message)
42-
try:
43-
send_web_push(json.loads(token), message)
44-
except Exception as e:
45-
print("error",e)
46-
47-
return redirect(url_for('main.index'))
48-
49-
50-
@main.route("/push_v1/",methods=['POST'])
46+
@app.route("/push_v1/",methods=['POST'])
5147
def push_v1():
52-
pusher = Pusher()
5348
message = "Push Test v1"
5449
print("is_json",request.is_json)
5550

@@ -60,9 +55,12 @@ def push_v1():
6055

6156
token = request.json.get('sub_token')
6257
try:
63-
pusher.sendNotification(token, message)
64-
# send_web_push(json.loads(token), message)
58+
token = json.loads(token)
59+
send_web_push(token, message)
6560
return jsonify({'success':1})
6661
except Exception as e:
6762
print("error",e)
6863
return jsonify({'failed':str(e)})
64+
65+
if __name__ == "__main__":
66+
app.run(host="0.0.0.0",port=8080)

0 commit comments

Comments
 (0)