commit ef627c24f6bb2beb0e0e5cc5bcc29b78b4ae7310 Author: Joe Tretter Date: Wed Oct 28 15:20:03 2020 -0500 initial checkin 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 0000000..d9838bc Binary files /dev/null and b/deadswitch.sqlite3 differ 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