Inverse-Rezeptsuche/app/application/endpoints.py

54 lines
1.7 KiB
Python
Raw Normal View History

2020-04-10 14:14:34 +00:00
from flask_restful import Resource, reqparse
import flask
2020-04-20 21:31:04 +00:00
from flask import g
2020-04-10 14:14:34 +00:00
import requests
import application.config as config
import json
import base64
2020-04-26 21:44:25 +00:00
from application.db2 import Session, Recipe
import application.search as search
import background.migrate as migrate
2020-04-18 08:18:13 +00:00
import time
2020-04-10 14:14:34 +00:00
class RecipeList(Resource):
2020-04-10 14:14:34 +00:00
def get(self):
""" """
2020-04-20 21:31:04 +00:00
g.session = Session()
2020-05-16 08:25:24 +00:00
# get Input
2020-04-20 21:31:04 +00:00
parser = reqparse.RequestParser()
parser.add_argument('ingred', type=str, action='append')
args = parser.parse_args()
ingreds = args["ingred"]
2020-05-16 08:25:24 +00:00
# stem to find stems in db
stemmed = [migrate.stem(ingred)[0] for ingred in ingreds]
2020-04-20 21:31:04 +00:00
start = time.time()
2020-05-16 08:25:24 +00:00
# returns ids of found recipes
indx = search.search2(stemmed)
print("get recipes",time.time() - start, "\n")
# returns dict with recipes, keys are the % of overlaps with recipes as values
recs = search.getRecDict2(indx, stemmed)
# ignored saved only the indices, has to be matched to the input before it's returned
recs["ignored"] = [ingreds[x] for x in recs["ignored"]]
print("calc overlay", time.time() - start, "\n")
2020-04-20 21:31:04 +00:00
g.session.close()
return flask.make_response(flask.jsonify({'data': recs}), 200)
2020-04-26 21:44:25 +00:00
class Images(Resource):
def get(self, id = None):
if id is None:
flask.make_response(flask.jsonify({'error': "No ID supplied"}), 401)
session = Session()
image = session.query(Recipe.img).filter(Recipe.recipe_id == id).first()[0]
2020-04-27 12:13:13 +00:00
image = base64.b64decode(image)
2020-04-29 21:10:31 +00:00
session.close()
2020-04-26 21:44:25 +00:00
return flask.Response(image, mimetype='image/png')
2020-04-10 14:14:34 +00:00