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