Last active
April 29, 2022 18:20
-
-
Save carc1n0gen/d6db985c6b37bf448fb9a386b66d4363 to your computer and use it in GitHub Desktop.
Plain WSGI API for converting text in to sarcasm/mocking format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from urllib.parse import parse_qs | |
def sarcasmify(text): | |
new_text = '' | |
flip = True | |
for i in range(len(text)): | |
char = text[i] | |
if flip: | |
new_text += char.lower() | |
else: | |
new_text += char.upper() | |
# Only flip if the current char was an alpha character | |
if char.isalpha(): | |
flip = not flip | |
return new_text | |
def respond(start_response, status, body): | |
start_response(status, [ | |
('Content-Type', 'text/plain'), | |
('Content-Length', str(len(body))) | |
]) | |
return [body] | |
def handle_get(environ, start_response): | |
params = parse_qs(environ['QUERY_STRING'], True) | |
param = params.get('text', []) | |
if param: | |
text = sarcasmify(param[0]) | |
status = '200 OK' | |
body = f'{text}'.encode('utf-8') | |
else: | |
status = '400 Bad Request' | |
body = b'Bad Request. Missing parameter "text"' | |
respond(start_response, status, body) | |
return [body] | |
def application(environ, start_response): | |
if environ.get('REQUEST_METHOD') == 'GET': | |
return handle_get(environ, start_response) | |
body = b'Method Not Allowed' | |
start_response('405 Method Not Allowed', [ | |
('Content-Type', 'text/plain'), | |
('Content-Length', str(len(body))) | |
]) | |
return [body] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment