changed database structure, count and motto_log

This commit is contained in:
Your Name 2021-01-24 15:18:43 +01:00
parent 5e9954fb07
commit 3272d367b9

157
api.py
View File

@ -5,6 +5,10 @@ from flask import g
from multiprocessing import Value
import pytz
from tokens import secrettoken1, secrettoken2, f1, f2
from removed_words import delWords
from refused_words import refWords
from refusal_messages import refusal_messages
from intact_words import intact
app = flask.Flask(__name__, static_url_path='', static_folder='static')
app.config["DEBUG"] = True
@ -18,17 +22,11 @@ seedText = "not for self but for all"
seedTextList = seedText.split()
tz = pytz.timezone('Europe/Berlin')
from removed_words import delWords
for word in wordList:
for delword in delWords:
if word == delword:
wordList.remove(word)
from refused_words import refWords
from refusal_messages import refusal_messages
def build_error_one():
refusalOne = 'Your motto request is refused. REFUSAL 400: ' + random.choice(refusal_messages)
return(refusalOne)
@ -71,7 +69,6 @@ def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
# db.row_factory = make_dicts
return db
@ -87,6 +84,57 @@ def query_db(query, args=(), one=False):
cur.close()
return (rv[0] if rv else None) if one else rv
def add_db(api_phrase_str,timestamp,org,seedText,request_type):
query = "INSERT INTO mottos (generated_motto,timestamp,organisation,seedtext,request_type) VALUES (?,?,?,?,?)"
args = (api_phrase_str,timestamp,org,seedText,request_type)
try:
con = get_db()
cur = con.cursor()
cur.execute(query, args)
con.commit()
except Error as error:
print(error)
finally:
cur.close()
con.close()
def add_error_db(timestamp,org,seedText,request_type,refusal_code,refusal_message):
query = "INSERT INTO mottos (timestamp,organisation,seedtext,request_type,refusal_code,refusal_message) VALUES (?,?,?,?,?,?)"
args = (timestamp,org,seedText,request_type,refusal_code,refusal_message)
try:
con = get_db()
cur = con.cursor()
cur.execute(query, args)
con.commit()
except Error as error:
print(error)
finally:
cur.close()
con.close()
def add_other_db(timestamp,seedText,request_type):
query = "INSERT INTO mottos (timestamp,seedtext,request_type) VALUES (?,?,?)"
args = (timestamp,seedText,request_type)
try:
con = get_db()
cur = con.cursor()
cur.execute(query, args)
con.commit()
except Error as error:
print(error)
finally:
cur.close()
con.close()
@app.route('/', methods=['GET'])
def home():
return '''<h1>Queer API</h1>
@ -100,19 +148,32 @@ def api_args():
rqstr = str(request.args['rq'])
if rqstr == "all_log":
all_slogans = query_db('SELECT * FROM mottos;')
moment = datetime.datetime.now(tz)
timestamp = moment.__str__()
request_type = 'all_log request'
add_other_db(timestamp,seedText,request_type)
with counter.get_lock():
counter.value += 1
return jsonify(all_slogans)
if rqstr == "success_log":
success_slogans = query_db('SELECT * FROM mottos WHERE GENERATED_MOTTO IS NOT NULL AND GENERATED_MOTTO!="";')
if rqstr == "motto_log":
success_slogans = query_db('SELECT generated_motto,timestamp,organisation,seedtext FROM mottos WHERE generated_motto IS NOT NULL AND generated_motto!="";')
moment = datetime.datetime.now(tz)
request_type = 'motto_log request'
add_other_db(timestamp,seedText,request_type)
with counter.get_lock():
counter.value += 1
timestamp = moment.__str__()
return jsonify(success_slogans)
if 'org' in request.args:
orgVal = str(request.args['org'])
orgVal = bytes(orgVal, encoding='utf-8')
if (rqstr == "generate" and orgVal == f1.decrypt(secrettoken1)) or (rqstr == "generate" and orgVal == f2.decrypt(secrettoken2)):
if orgVal == f1.decrypt(secrettoken1):
org = "Transmediale"
org = "transmediale"
elif orgVal == f2.decrypt(secrettoken2):
org = "Test"
org = "test"
# Generate the motto
api_phrase_str = ''
for i in range(len(seedTextList)):
@ -129,16 +190,16 @@ def api_args():
api_phrase_str_word = ''
api_phrase_str = api_phrase_str[2:]
request_type = 'generate request'
# Check if one of the words has been refused
for elem in refWords:
if elem in api_phrase_str:
refusal_code = 400
refusal_message = build_error_one()
# Write to the database
with sqlite3.connect("queermottoapi.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO mottos (TIMESTAMP,ORGANISATION,SEEDTEXT,REFUSAL_CODE,REFUSAL_MESSAGE) VALUES (?,?,?,?,?)",(timestamp,org,seedText,refusal_code,refusal_message) )
con.commit()
add_error_db(timestamp,org,seedText,request_type,refusal_code,refusal_message)
with counter.get_lock():
counter.value += 1
return jsonify(refusal_message)
# Check the date
@ -146,11 +207,9 @@ def api_args():
if (moment.month==3 and moment.day==8) or (moment.month==5 and moment.day==1) or (moment.month==7 and moment.day==20):
refusal_code = 402
refusal_message = build_error_three()
# Write to the database
with sqlite3.connect("queermottoapi.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO mottos (TIMESTAMP,ORGANISATION,SEEDTEXT,REFUSAL_CODE,REFUSAL_MESSAGE) VALUES (?,?,?,?,?)",(timestamp,org,seedText,refusal_code,refusal_message) )
con.commit()
add_error_db(timestamp,org,seedText,request_type,refusal_code,refusal_message)
with counter.get_lock():
counter.value += 1
return jsonify(refusal_message)
# Check if the counter is smaller than 10
@ -159,34 +218,25 @@ def api_args():
with counter.get_lock():
counter.value += 1
out = counter.value
# Write to the database
with sqlite3.connect("queermottoapi.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO mottos (GENERATED_MOTTO,TIMESTAMP,ORGANISATION,SEEDTEXT) VALUES (?,?,?,?)",(api_phrase_str,timestamp,org,seedText) )
con.commit()
add_db(api_phrase_str,timestamp,org,seedText,request_type)
return jsonify(api_phrase_str)
else:
with counter.get_lock():
counter.value = 0
refusal_code = 402
refusal_message = build_error_three()
# Write to the database
with sqlite3.connect("queermottoapi.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO mottos (TIMESTAMP,ORGANISATION,SEEDTEXT,REFUSAL_CODE,REFUSAL_MESSAGE) VALUES (?,?,?,?,?)",(timestamp,org,seedText,refusal_code,refusal_message) )
con.commit()
add_error_db(timestamp,org,seedText,request_type,refusal_code,refusal_message)
return jsonify(refusal_message)
else:
moment = datetime.datetime.now(tz)
timestamp = moment.__str__()
refusal_code = 401
refusal_message = build_error_two()
org = "Someone"
# Write to the database
with sqlite3.connect("queermottoapi.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO mottos (TIMESTAMP,ORGANISATION,SEEDTEXT,REFUSAL_CODE,REFUSAL_MESSAGE) VALUES (?,?,?,?,?)",(timestamp,org,seedText,refusal_code,refusal_message) )
con.commit()
org = 'someone'
request_type = 'unknown request'
add_error_db(timestamp,org,seedText,request_type,refusal_code,refusal_message)
with counter.get_lock():
counter.value += 1
return jsonify(refusal_message)
@ -196,12 +246,11 @@ def error_fourzerozero(e):
timestamp = moment.__str__()
refusal_code = 401
refusal_message = build_error_two()
org = "Someone"
# Write to the database
with sqlite3.connect("queermottoapi.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO mottos (TIMESTAMP,ORGANISATION,SEEDTEXT,REFUSAL_CODE,REFUSAL_MESSAGE) VALUES (?,?,?,?,?)",(timestamp,org,seedText,refusal_code,refusal_message) )
con.commit()
org = 'someone'
request_type = 'unknown request'
add_error_db(timestamp,org,seedText,request_type,refusal_code,refusal_message)
with counter.get_lock():
counter.value += 1
return jsonify(refusal_message)
@app.errorhandler(404)
@ -210,12 +259,11 @@ def error_fourzerofour(e):
timestamp = moment.__str__()
refusal_code = 401
refusal_message = build_error_two()
org = "Someone"
# Write to the database
with sqlite3.connect("queermottoapi.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO mottos (TIMESTAMP,ORGANISATION,SEEDTEXT,REFUSAL_CODE,REFUSAL_MESSAGE) VALUES (?,?,?,?,?)",(timestamp,org,seedText,refusal_code,refusal_message) )
con.commit()
org = 'someone'
request_type = 'unknown request'
add_error_db(timestamp,org,seedText,request_type,refusal_code,refusal_message)
with counter.get_lock():
counter.value += 1
return jsonify(refusal_message)
@app.errorhandler(500)
@ -224,12 +272,11 @@ def error_fivezerozero(e):
timestamp = moment.__str__()
refusal_code = 401
refusal_message = build_error_two()
org = "Someone"
# Write to the database
with sqlite3.connect("queermottoapi.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO mottos (TIMESTAMP,ORGANISATION,SEEDTEXT,REFUSAL_CODE,REFUSAL_MESSAGE) VALUES (?,?,?,?,?)",(timestamp,org,seedText,refusal_code,refusal_message) )
con.commit()
org = 'someone'
request_type = 'unknown request'
add_error_db(timestamp,org,seedText,request_type,refusal_code,refusal_message )
with counter.get_lock():
counter.value += 1
return jsonify(refusal_message)
# app.run()