You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

175 lines
6.2 KiB

import flask, random, datetime, json
from flask import request, jsonify
app = flask.Flask(__name__, static_url_path='', static_folder='static')
app.config["DEBUG"] = True
availableWords = open('static/files/RNN_EditedText.txt', 'r', encoding='utf8')
wordList = availableWords.read()
wordList = wordList.split()
seedText = "not for self but for all"
seedTextList = seedText.split()
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)
def build_error_two():
refusalTwo = 'Your motto request is refused. REFUSAL 401: ' + random.choice(refusal_messages)
return(refusalTwo)
def build_error_three():
refusalThree = 'Your motto request is refused. REFUSAL 402: ' + random.choice(refusal_messages)
return(refusalThree)
# Generating a dict that will be used for the phrases when called upon
toChooseFrom = {}
def build_word_dict():
for i in range(len(seedTextList)):
# Current word in the seed phrase
current = seedTextList[i]
# Going through the letters in the above word
for y in range(len(current)):
charNeeded = current[y]
for item in wordList:
if (len(item)-1) >= y:
addKey = str(y) + item[y]
if addKey not in toChooseFrom.keys():
toChooseFrom[addKey] = []
toChooseFrom[addKey].append(item)
else:
toChooseFrom[addKey].append(item)
build_word_dict()
# Example json file
# slogans = {
# 'description' : 'Description here...',
# 'source' : 'git url?',
# 'data' : [
# {'generated_motto': "not no but fore possible particular southerner being all different be outside not find to our art black culture",
# 'timestamp': 'date here',
# 'organization':'tm',
# 'seedtext':'not for self but for all'},
# {'generated_motto': "not non-European not future body her sharing feel all fulfilled bring muse not face force World art black culture",
# 'timestamp': 'date here',
# 'organization':'tm',
# 'seedtext':'not for self but for all'},
# {'generated_motto': "neither contracts hate face coals words seriously become relationship different bourgeois turning attach feminists work participate about flirty fall",
# 'timestamp': 'date here',
# 'organization': 'tm',
# 'seedtext':'not for self but for all'},
# {'refusal_code': "401",
# 'refusal_message': "new desires, new modes of gender, and new struggles are breaking open",
# 'timestamp': 'date here',
# 'organization': 'tm',
# 'seedtext':'not for self but for all'}
# ]
# }
with open('static/files/slogans.json', 'r', encoding='utf8') as sloganjson:
slogans = json.load(sloganjson)
# slogans['data'].append(
# {'refusal_code': "401",
# 'refusal_message': "new desires, new modes of gender, and new struggles are breaking open",
# 'timestamp': 'date here',
# 'oganization': 'tm',
# 'seedtext':'not for self but for all'
# }
# )
# with open('static/files/slogans.json', 'w', encoding='utf8') as sloganjson:
# json.dump(slogans, sloganjson)
@app.route('/', methods=['GET'])
def home():
return '''<h1>Queer API</h1>
<p>A prototype API.</p>'''
@app.route('/queermottoAPI/r1/refusal', methods=['GET'])
def api_args():
buildDictElem = {}
buildDictElem['seedtext'] = 'not for self but for all'
moment = datetime.datetime.now()
buildDictElem['timestamp'] = moment.__str__()
org = str(request.args['org'])
# buildDictElem['org']= org
count = 0
if count < 40:
# Check if an ID was provided as part of the URL.
# If ID is provided, assign it to a variable.
# If no ID is provided, display an error in the browser.
if 'rq' in request.args:
rqstr = str(request.args['rq'])
if rqstr == "generate":
# Create an empty list for our results
results = []
api_phrase = []
for i in range(len(seedTextList)):
# Current word in the seed phrase
current = seedTextList[i]
# Going through the letters in the above word
for y in range(len(current)):
testKey = str(y) + current[y]
# print(current[y],random.choice(toChooseFrom[testKey]))
api_phrase.append(random.choice(toChooseFrom[testKey]))
api_phrase_str = ' \n '.join(api_phrase)
results.append(api_phrase_str)
for elem in refWords:
if elem in api_phrase:
count +=1
return jsonify(build_error_one())
# Check the date
moment = datetime.datetime.now()
if (moment.month==3 and moment.day==8) or (moment.month==5 and moment.day==1) or (moment.month==7 and moment.day==20):
count +=1
return jsonify(build_error_three())
count +=1
return jsonify(api_phrase_str)
elif rqstr == "all_log":
count +=1
return jsonify(slogans)
elif rqstr == "success_log":
success_slogans = []
for call in slogans:
if "refusal_code" not in call.keys():
success_slogans.append(call)
count +=1
return jsonify(success_slogans)
else:
count +=1
return jsonify(build_error_two())
else:
count = 0
buildDictElem['refusal_code'] = 402
buildDictElem['refusal_message'] = build_error_three()
slogans['data'].append(buildDictElem)
with open('static/files/slogans.json', 'w', encoding='utf8') as sloganjson:
json.dump(slogans, sloganjson)
return jsonify(buildDictElem['refusal_message'])
# app.run()