generated from mwc/lab_server
60 lines
2.2 KiB
Python
60 lines
2.2 KiB
Python
from banjo.urls import route_get, route_post
|
|
from banjo.http import BadRequest, NotFound
|
|
from app.models import Line, Poem, Rhyme
|
|
from random import choice, sample
|
|
|
|
@route_get('lines/random', args={})
|
|
def get_random_line(params):
|
|
return {'line': Line.objects.random().text}
|
|
|
|
@route_get('lines/about', args={'topic' : str})
|
|
def get_about_line(params):
|
|
try:
|
|
line = Line.objects.filter(clean_text_contains=params['topic']).random()
|
|
return line.to_dict()
|
|
except Line.DoesNotExist:
|
|
raise BadRequest("Line not found")
|
|
|
|
@route_get('lines/rhyme', args={'word' : str})
|
|
def get_rhyme_line(params):
|
|
try:
|
|
rhyme = Rhyme.get_rhyme_for_word(params['word']).lines.exclude(clean_text__endswidth=" "+params['word']).first()
|
|
return rhyme.to_dict()
|
|
except Rhyme.DoesNotExist:
|
|
raise BadRequest("Rhyme not found")
|
|
|
|
@route_get('couplets/random', args={})
|
|
def get_random_couplet(params):
|
|
try:
|
|
line = Line.objects.random()
|
|
for rl in line.rhyming_lines().sample(1):
|
|
rhyme = rl
|
|
return {'lines' : [line.text, rhyme.text]}
|
|
except Line.DoesNotExist:
|
|
raise BadRequest("Rhyme Not Found")
|
|
|
|
@route_get('couplets/about', args={'topic' : str})
|
|
def get_about_couplet(params):
|
|
try:
|
|
line = Line.objects.filter(clean_text__contains=params['topics']).random()
|
|
for rl in line.rhyming_lines().sample(1):
|
|
rhyme = rl
|
|
return {'lines': [line.text, rhyme.text], 'topic': params['topic']}
|
|
except Line.DoesNotExist:
|
|
raise BadRequest("Couplet not found with given topic")
|
|
|
|
@route_get('couplets/rhyme', args={'word' : str})
|
|
def get_rhyme_couplet(params):
|
|
try:
|
|
toRhyme = Rhyme.get_rhyme_for_word(params['word']).lines.exclude(clean_text__endswith= " " + params['word']).first()
|
|
#line = Line.
|
|
for rl in line.rhyming_lines().sample(1):
|
|
rhyme = rl
|
|
return {'lines': [rhyme.text, toRhyme.text], 'word': params['word']}
|
|
except Rhyme.DoesNotExist:
|
|
raise BadRequest("Did not fnid a couplet with a Rhyme")
|
|
except Exception as e:
|
|
if isinstance(e, BadRequest):
|
|
raise BadRequest(f"Couldnt figure out how to pronounce {params['word']}")
|
|
|