first version?
This commit is contained in:
73
api1.py
73
api1.py
@@ -1,12 +1,18 @@
|
|||||||
import flask
|
import flask
|
||||||
|
import sys
|
||||||
from flask import request, jsonify, g
|
from flask import request, jsonify, g
|
||||||
from flask_apscheduler import APScheduler
|
from flask_apscheduler import APScheduler
|
||||||
from datetime import datetime,timedelta,timezone
|
from datetime import datetime,timedelta,timezone
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
import smtplib
|
||||||
|
|
||||||
|
# testing:
|
||||||
|
# start dummy web server for dev with root: python3 -m smtpd -c DebuggingServer -n localhost:25
|
||||||
|
|
||||||
|
# Request:
|
||||||
|
# curl -X POST "http://localhost:5000/api/v1?topic=abc.def&secs=60&email=j.tretter@gmail.com"
|
||||||
DATABASE='deadswitch.sqlite3'
|
DATABASE='deadswitch.sqlite3'
|
||||||
|
|
||||||
|
|
||||||
class Config(object):
|
class Config(object):
|
||||||
JOBS = [
|
JOBS = [
|
||||||
{
|
{
|
||||||
@@ -19,18 +25,33 @@ class Config(object):
|
|||||||
|
|
||||||
SCHEDULER_API_ENABLED = True
|
SCHEDULER_API_ENABLED = True
|
||||||
|
|
||||||
|
def sendMail(topic,toAddr):
|
||||||
|
print(f"sending mail to {toAddr} for topic {topic}")
|
||||||
|
port = 25
|
||||||
|
server=smtplib.SMTP("localhost", port)
|
||||||
|
sender_email = "deadswitch@kawomi.com"
|
||||||
|
receiver_email = "joerg.tretter@gmail.com"
|
||||||
|
message = f"""\
|
||||||
|
Subject: deadSwitch not pushed on topic {topic}
|
||||||
|
|
||||||
|
Notification of deadSwitch not pushed on topic {topic}."""
|
||||||
|
server.sendmail(sender_email, receiver_email, message)
|
||||||
|
|
||||||
|
|
||||||
def expiryCheck():
|
def expiryCheck():
|
||||||
print('LA ')
|
print('.',end='')
|
||||||
db=sqlite3.connect(DATABASE,detect_types=sqlite3.PARSE_DECLTYPES)
|
db=sqlite3.connect(DATABASE,detect_types=sqlite3.PARSE_DECLTYPES)
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
for theRow in cursor.execute("select expiry,topic from entries where expiry < datetime()"):
|
for theRow in cursor.execute("select expiry,topic,email from entries where active=True and isNotified=False and expiry < datetime()"):
|
||||||
print(theRow)
|
print(theRow)
|
||||||
|
sendMail(theRow[1],theRow[2])
|
||||||
|
db.execute("update entries set isNotified=True where topic=?",[theRow[1]])
|
||||||
|
db.commit()
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
app.config["DEBUG"] = True
|
app.config["DEBUG"] = False
|
||||||
|
|
||||||
app.config.from_object(Config())
|
app.config.from_object(Config())
|
||||||
scheduler = APScheduler()
|
scheduler = APScheduler()
|
||||||
@@ -54,7 +75,15 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
@app.route('/', methods=['GET'])
|
@app.route('/', methods=['GET'])
|
||||||
def home():
|
def home():
|
||||||
return "<h1>Hello</h1>"
|
return """<h1>Dead-Switch web service</h1>
|
||||||
|
<hr>
|
||||||
|
<h2>2020-10-29 - j.tretter@gmail.com</h2>
|
||||||
|
<hr>
|
||||||
|
Examples:
|
||||||
|
<ul>
|
||||||
|
<li>curl "https://deadswitch.kawomi.com/api/v1?topic=sys.demo"</li>
|
||||||
|
<li>curl -X POST "https://deadswitch.kawomi.com/api/v1?topic=sys.demo&secs=60&email=demo@gmail.com"</li>
|
||||||
|
</ul>"""
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/v1', methods=['GET'])
|
@app.route('/api/v1', methods=['GET'])
|
||||||
@@ -62,33 +91,43 @@ if __name__ == '__main__':
|
|||||||
if 'topic' in request.args:
|
if 'topic' in request.args:
|
||||||
topic=request.args['topic']
|
topic=request.args['topic']
|
||||||
else:
|
else:
|
||||||
return "Error: need topic"
|
return jsonify({'success':False, 'error':"No topic given"})
|
||||||
|
|
||||||
cursor = get_db().cursor()
|
cursor = get_db().cursor()
|
||||||
cursor.execute("select expiry from entries where topic=?",[topic])
|
cursor.execute("select active,expiry,email,isNotified,expiry<datetime('now') \"isExpired\",lastSeen from entries where topic=?",[topic])
|
||||||
expdatetime=cursor.fetchone()[0]
|
theRow=cursor.fetchone()
|
||||||
|
if theRow is None:
|
||||||
|
return jsonify({'success':False, 'error':f"No entry found for topic _{topic}_"})
|
||||||
|
|
||||||
return "<h1>GET</h1>" + topic + ": " + expdatetime.strftime("%Y-%m-%d %H:%M:%S")
|
return jsonify({'success':True, 'topic':topic, 'active':theRow[0], 'expiry':theRow[1].strftime("%Y-%m-%d %H:%M:%S"), 'lastSeen':theRow[5].strftime("%Y-%m-%d %H:%M:%S"),'isExpired':theRow[4],'email':theRow[2], 'isNotified':theRow[3]})
|
||||||
|
|
||||||
@app.route('/api/v1', methods=['PUT','POST'])
|
@app.route('/api/v1', methods=['PUT','POST'])
|
||||||
def api_setRecord():
|
def api_setRecord():
|
||||||
if 'topic' in request.args:
|
if 'topic' in request.args:
|
||||||
topic = request.args['topic']
|
topic = request.args['topic']
|
||||||
else:
|
else:
|
||||||
return "Error: need topic"
|
return jsonify({'success':False, 'error':"No topic given"})
|
||||||
|
|
||||||
|
if 'email' in request.args:
|
||||||
|
email = request.args['email']
|
||||||
|
else:
|
||||||
|
return jsonify({'success':False, 'error':"No email given"})
|
||||||
|
|
||||||
if 'secs' in request.args:
|
if 'secs' in request.args:
|
||||||
secs = int(request.args['secs'])
|
secs = int(request.args['secs'])
|
||||||
else:
|
else:
|
||||||
return "Errof: need secs"
|
return jsonify({'success':False, 'error':"No secs given"})
|
||||||
|
|
||||||
expiryDateTime=datetime.now(tz=timezone.utc) + timedelta(seconds=secs)
|
expiryDateTime=datetime.now(tz=timezone.utc) + timedelta(seconds=secs)
|
||||||
|
|
||||||
cursor = get_db().cursor()
|
try:
|
||||||
cursor.execute("update entries set expiry=? where topic=?",[expiryDateTime,topic])
|
cursor = get_db().cursor()
|
||||||
if cursor.rowcount == 0:
|
cursor.execute("update entries set active=True,isNotified=False,email=?,expiry=?,lastSeen=datetime('now') where topic=?",[email,expiryDateTime,topic])
|
||||||
cursor.execute("Insert into entries (topic,expiry) values (?,?)",[topic,expiryDateTime])
|
if cursor.rowcount == 0:
|
||||||
|
cursor.execute("Insert into entries (active,topic,expiry,email,isNotified,lastSeen) values (True,?,?,?,False,datetime('now'))",[topic,expiryDateTime,email])
|
||||||
|
except Exception as e:
|
||||||
|
return jsonify({'success':False, 'error':str(e)})
|
||||||
|
|
||||||
return "<h1>SET</h1>" + expiryDateTime.strftime("%Y-%m-%d %H:%M:%S")
|
return jsonify({'success':True, 'expiry':expiryDateTime.strftime("%Y-%m-%d %H:%M:%S"), 'email':email})
|
||||||
|
|
||||||
app.run(use_reloader=False)
|
app.run(use_reloader=False)
|
||||||
1
createDB.sql
Normal file
1
createDB.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
CREATE TABLE entries (active boolean,topic string,email string, expiry timestamp, isNotified boolean, lastSeen timestamp);
|
||||||
Binary file not shown.
1
mail.py
1
mail.py
@@ -1,7 +1,6 @@
|
|||||||
import smtplib
|
import smtplib
|
||||||
|
|
||||||
port = 25
|
port = 25
|
||||||
|
|
||||||
server=smtplib.SMTP("localhost", port)
|
server=smtplib.SMTP("localhost", port)
|
||||||
sender_email = "deadswitch@kawomi.com"
|
sender_email = "deadswitch@kawomi.com"
|
||||||
receiver_email = "joerg.tretter@gmail.com"
|
receiver_email = "joerg.tretter@gmail.com"
|
||||||
|
|||||||
Reference in New Issue
Block a user