initial checkin

This commit is contained in:
Joe Tretter
2020-10-28 15:20:03 -05:00
commit ef627c24f6
3 changed files with 113 additions and 0 deletions

94
api1.py Normal file
View File

@@ -0,0 +1,94 @@
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)

BIN
deadswitch.sqlite3 Normal file

Binary file not shown.

19
mail.py Normal file
View File

@@ -0,0 +1,19 @@
import smtplib, ssl
port = 465 # For SSL
password = "mailR3L4Y"
# Create a secure SSL context
context = ssl.create_default_context()
with smtplib.SMTP_SSL("kawomi.com", port, context=context) as server:
server.login("pope@kawomi.com", password)
sender_email = "pope@kawomi.com"
receiver_email = "j.tretter@gmail.com"
message = """\
Subject: Hi there
This message is sent from Python."""
server.sendmail(sender_email, receiver_email, message)