Skip to content

Instantly share code, notes, and snippets.

@raunakdoesdev
Created April 6, 2021 15:24
Show Gist options
  • Save raunakdoesdev/83465b4507bf30adfbe2ddb6db0accd1 to your computer and use it in GitHub Desktop.
Save raunakdoesdev/83465b4507bf30adfbe2ddb6db0accd1 to your computer and use it in GitHub Desktop.
import sqlite3
import numpy as np
import datetime
secret_key = 0.10721210370764633
db_path = '/var/jail/home/raunakc/lab08a/server.db'
def request_handler(request):
if request['method'] == 'GET':
with sqlite3.connect(db_path) as conn:
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS subs (x float,y float,timing timestamp);''')
thirty_seconds_ago = datetime.datetime.now()- datetime.timedelta(seconds = 30) # create time for fifteen minutes ago!
things = c.execute('''SELECT * FROM subs WHERE timing > ? ORDER BY timing DESC;''',(thirty_seconds_ago,)).fetchall()
x = [thing[0] for thing in things]
y = [thing[1] for thing in things]
if len(things) >= 3:
fitted_poly = np.poly1d(np.polyfit(x, y, 2))
if abs(fitted_poly(0) - 0.10721210370764633) < 1e-3:
return """ <iframe src="https://giphy.com/embed/E6jscXfv3AkWQ" width="480" height="480" frameBorder="0" class="giphy-embed" allowFullScreen></iframe><p><a href="https://giphy.com/gifs/cat-typing-E6jscXfv3AkWQ">via GIPHY</a></p>"""
return "Failed to authenticate <br> (" + str(x) + "," + str(y) + ")"
else:
with sqlite3.connect(db_path) as conn:
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS subs (x float,y float,timing timestamp);''')
thirty_seconds_ago = datetime.datetime.now()- datetime.timedelta(seconds = 30) # create time for fifteen minutes ago!
things = c.execute('''SELECT * FROM subs WHERE timing > ? ORDER BY timing DESC;''',(thirty_seconds_ago,)).fetchall()
c.execute('''INSERT into subs VALUES (?,?,?);''', (request['form']['x'], request['form']['y'], datetime.datetime.now()))
return request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment