94 lines
2.6 KiB
Python
94 lines
2.6 KiB
Python
|
|
import flask
|
||
|
|
from flask import request, jsonify, g
|
||
|
|
from flask_apscheduler import APScheduler
|
||
|
|
from datetime import datetime,timedelta,timezone
|
||
|
|
import sqlite3
|
||
|
|
|
||
|
|
DATABASE='deadswitch.sqlite3'
|
||
|
|
|
||
|
|
|
||
|
|
class Config(object):
|
||
|
|
JOBS = [
|
||
|
|
{
|
||
|
|
'id': 'job1',
|
||
|
|
'func': 'api1:expiryCheck',
|
||
|
|
'trigger': 'interval',
|
||
|
|
'seconds': 10
|
||
|
|
}
|
||
|
|
]
|
||
|
|
|
||
|
|
SCHEDULER_API_ENABLED = True
|
||
|
|
|
||
|
|
|
||
|
|
def expiryCheck():
|
||
|
|
print('LA ')
|
||
|
|
db=sqlite3.connect(DATABASE,detect_types=sqlite3.PARSE_DECLTYPES)
|
||
|
|
cursor = db.cursor()
|
||
|
|
for theRow in cursor.execute("select expiry,topic from entries where expiry < datetime()"):
|
||
|
|
print(theRow)
|
||
|
|
db.close()
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
app = flask.Flask(__name__)
|
||
|
|
app.config["DEBUG"] = True
|
||
|
|
|
||
|
|
app.config.from_object(Config())
|
||
|
|
scheduler = APScheduler()
|
||
|
|
# it is also possible to enable the API directly
|
||
|
|
# scheduler.api_enabled = True
|
||
|
|
scheduler.init_app(app)
|
||
|
|
scheduler.start()
|
||
|
|
|
||
|
|
def get_db():
|
||
|
|
db = getattr(g, '_database', None)
|
||
|
|
if db is None:
|
||
|
|
db = g._database = sqlite3.connect(DATABASE,detect_types=sqlite3.PARSE_DECLTYPES)
|
||
|
|
return db
|
||
|
|
|
||
|
|
@app.teardown_appcontext
|
||
|
|
def close_connection(exception):
|
||
|
|
db = getattr(g, '_database', None)
|
||
|
|
if db is not None:
|
||
|
|
db.commit()
|
||
|
|
db.close()
|
||
|
|
|
||
|
|
@app.route('/', methods=['GET'])
|
||
|
|
def home():
|
||
|
|
return "<h1>Hello</h1>"
|
||
|
|
|
||
|
|
|
||
|
|
@app.route('/api/v1', methods=['GET'])
|
||
|
|
def api_getByTopic():
|
||
|
|
if 'topic' in request.args:
|
||
|
|
topic=request.args['topic']
|
||
|
|
else:
|
||
|
|
return "Error: need topic"
|
||
|
|
|
||
|
|
cursor = get_db().cursor()
|
||
|
|
cursor.execute("select expiry from entries where topic=?",[topic])
|
||
|
|
expdatetime=cursor.fetchone()[0]
|
||
|
|
|
||
|
|
return "<h1>GET</h1>" + topic + ": " + expdatetime.strftime("%Y-%m-%d %H:%M:%S")
|
||
|
|
|
||
|
|
@app.route('/api/v1', methods=['PUT','POST'])
|
||
|
|
def api_setRecord():
|
||
|
|
if 'topic' in request.args:
|
||
|
|
topic = request.args['topic']
|
||
|
|
else:
|
||
|
|
return "Error: need topic"
|
||
|
|
|
||
|
|
if 'secs' in request.args:
|
||
|
|
secs = int(request.args['secs'])
|
||
|
|
else:
|
||
|
|
return "Errof: need secs"
|
||
|
|
|
||
|
|
expiryDateTime=datetime.now(tz=timezone.utc) + timedelta(seconds=secs)
|
||
|
|
|
||
|
|
cursor = get_db().cursor()
|
||
|
|
cursor.execute("update entries set expiry=? where topic=?",[expiryDateTime,topic])
|
||
|
|
if cursor.rowcount == 0:
|
||
|
|
cursor.execute("Insert into entries (topic,expiry) values (?,?)",[topic,expiryDateTime])
|
||
|
|
|
||
|
|
return "<h1>SET</h1>" + expiryDateTime.strftime("%Y-%m-%d %H:%M:%S")
|
||
|
|
|
||
|
|
app.run(use_reloader=False)
|