From ef627c24f6bb2beb0e0e5cc5bcc29b78b4ae7310 Mon Sep 17 00:00:00 2001 From: Joe Tretter Date: Wed, 28 Oct 2020 15:20:03 -0500 Subject: [PATCH] initial checkin --- api1.py | 94 +++++++++++++++++++++++++++++++++++++++++++++ deadswitch.sqlite3 | Bin 0 -> 8192 bytes mail.py | 19 +++++++++ 3 files changed, 113 insertions(+) create mode 100644 api1.py create mode 100644 deadswitch.sqlite3 create mode 100644 mail.py diff --git a/api1.py b/api1.py new file mode 100644 index 0000000..5bd784c --- /dev/null +++ b/api1.py @@ -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 "

Hello

" + + + @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 "

GET

" + 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 "

SET

" + expiryDateTime.strftime("%Y-%m-%d %H:%M:%S") + + app.run(use_reloader=False) \ No newline at end of file diff --git a/deadswitch.sqlite3 b/deadswitch.sqlite3 new file mode 100644 index 0000000000000000000000000000000000000000..d9838bc41c905dd84591a76f4a2e5812b0d6432f GIT binary patch literal 8192 zcmeI#!D<3A5C-6h7H^_A4||zowYp@pB6crU%ifey^#xXqCD7GnH=sUMU&c${rV(A} zrFiY(9}?zI7zli~EZ(QaYToYiG_f2~lZ2obW+E4fDsGomb>X2rxGsL>zhaa8(Rcka z-1s85P!NCs1Rwwb2tWV=5P$##AOHaf+!uHgwU+0JgH2YSTJLObba88y*Lgf%#=IO) zrZL|x`O)q(v*yB0?moJ#kD19&Y)z^Qo1|I$`ZqsQ<9z>dOh5nv5P$##AOHafKmY;| tfB*y_@P7n4qUz0()!N_at&&Ri0@+hT4xXd1AIZ>HLC_zlCn+N-e*o2qFLM9@ literal 0 HcmV?d00001 diff --git a/mail.py b/mail.py new file mode 100644 index 0000000..551d0bf --- /dev/null +++ b/mail.py @@ -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) \ No newline at end of file