-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
65 lines (57 loc) · 2.04 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Flask, request, redirect, url_for, render_template, session, jsonify, abort
from user import *
from barcode_reader import *
import requests
from PIL import Image
import io
import base64
from flask_cors import CORS
app = Flask("server")
CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
@app.route('/calculate/initial', methods=['POST'])
def calculate_initial():
user = request.json
nutrients = calculate_initial_nutrients(user)
return jsonify(nutrients)
@app.route('/item/barcode', methods=['POST'])
def get_barcode_data():
# This receives a raw image data url. Look to Pillow documentation for specific parsing instructions.
image_keys = request.json
base64String = base64.b64decode(image_keys['image'])
image = Image.open(io.BytesIO(base64String))
product_info = get_barcode_info(image)
if 'error' in product_info:
abort(product_info['code'], description=product_info["error"])
return jsonify(product_info)
@app.route('/item/search', methods=['GET'])
def search():
item = request.args.get('query')
item_list = search_items(item)
return item_list.json()
@app.route('/item/info', methods=['GET'])
def get_search_data():
fdcid = request.args.get('fdcid')
product_info = get_fdcid_info(fdcid)
return jsonify(product_info)
@app.route('/item/add', methods=['POST'])
def add_item():
food = request.json['food']
nutrients = request.json['nutrients']
if food['type'] == "barcode":
nutrients = add_item_barcode(food['id'], nutrients)
elif food['type'] == "fdcid":
nutrients = add_item_fdcid(food['id'], nutrients)
return jsonify(nutrients)
@app.route('/item/delete', methods=['POST'])
def delete_item():
food = request.json['food']
nutrients = request.json['nutrients']
if food['type'] == "barcode":
nutrients = delete_item_barcode(food['id'], nutrients)
elif food['type'] == "fdcid":
nutrients = delete_item_fdcid(food['id'], nutrients)
return jsonify(nutrients)
@app.route('/')
def home():
return 'Recovery Backend'