示例#1
0
文件: api.py 项目: joram/recipes
def recipe(pub_id):
    session = Session()
    qs = session.query(Recipe).filter(Recipe.pub_id == pub_id)
    if len(qs.all()) == 0:
        return flask.abort(404)
    recipe_json = qs.all()[0].json()

    parser = Parser()
    for key in recipe_json["ingredients"]:
        ingredients = []
        for ingredient in recipe_json["ingredients"][key]:
            ingredients.append(parser.parse(ingredient))
        recipe_json["ingredients"][key] = ingredients

    def get_original_image(d):
        if type(d) == list:
            return [0]
        if "originals" in d["originals"]:
            return get_original_image(d["originals"])
        return d["originals"]

    recipe_json["images"]["originals"] = get_original_image(
        recipe_json["images"]["originals"])

    return flask.jsonify(recipe_json)
示例#2
0
def test_ingredient():
    parser = Parser()
    actual = parser.parse("1 cup caramel sauce")
    expected = {
        "comment": "",
        "material": "caramel sauce",
        "original": "1 cup caramel sauce",
        "quantities": [[1.0, "cup"]]
    }

    assert actual == expected
示例#3
0
def test_ingredient():
    parser = Parser()
    actual = parser.parse("10 medium (blank)s tomatillos, husked")
    expected = {
        "comment": "husked",
        "material": "medium tomatillos",
        "original": "10 medium (blank)s tomatillos, husked",
        "quantities": [[10.0, ""]]
    }

    assert actual == expected
示例#4
0
def test_ingredient():
    parser = Parser()
    actual = parser.parse("salt and pepper to taste")
    expected = {
        "comment": "",
        "material": "salt and pepper to taste",
        "original": "salt and pepper to taste",
        "quantities": [[1, ""]]
    }

    assert actual == expected
示例#5
0
def test_ingredient():
    parser = Parser()
    actual = parser.parse("1 cup white cornmeal")
    expected = {
        "comment": "",
        "material": "white cornmeal",
        "original": "1 cup white cornmeal",
        "quantities": [[1.0, "cup"]]
    }

    assert actual == expected
示例#6
0
def test_ingredient():
    parser = Parser()
    actual = parser.parse("1 teaspoon salt")
    expected = {
        "comment": "",
        "material": "salt",
        "original": "1 teaspoon salt",
        "quantities": [[1.0, "teaspoon"]]
    }

    assert actual == expected
示例#7
0
def test_ingredient():
    parser = Parser()
    actual = parser.parse("1/4 cup chopped fresh cilantro")
    expected = {
        "comment": "",
        "material": "chopped fresh cilantro",
        "original": "1/4 cup chopped fresh cilantro",
        "quantities": [[0.25, "cup"]]
    }

    assert actual == expected
示例#8
0
def test_ingredient():
    parser = Parser()
    actual = parser.parse(".5 cup boiling water")
    expected = {
        "comment": "",
        "material": ".5  boiling water",
        "original": ".5 cup boiling water",
        "quantities": [[0.5, "cup"]]
    }

    assert actual == expected
def test_ingredient():
    parser = Parser()
    actual = parser.parse("3 tablespoons confectioners' sugar")
    expected = {
        "comment": "",
        "material": "confectioners' sugar",
        "original": "3 tablespoons confectioners' sugar",
        "quantities": [[3.0, "tablespoon"]]
    }

    assert actual == expected
def test_ingredient():
    parser = Parser()
    actual = parser.parse("2 pounds white mushrooms")
    expected = {
        "comment": "",
        "material": "pounds white mushrooms",
        "original": "2 pounds white mushrooms",
        "quantities": [[2.0, "pound"]]
    }

    assert actual == expected
示例#11
0
def test_ingredient():
    parser = Parser()
    actual = parser.parse("¾ cup brown sugar")
    expected = {
        "comment": "",
        "material": "brown sugar",
        "original": "\u00be cup brown sugar",
        "quantities": [[0.75, "cup"]]
    }

    assert actual == expected
示例#12
0
def test_ingredient():
    parser = Parser()
    actual = parser.parse("2 eaches apples, sliced")
    expected = {
        "comment": "sliced",
        "material": "eaches apples",
        "original": "2 eaches apples, sliced",
        "quantities": [[2.0, ""]]
    }

    assert actual == expected
def test_ingredient():
    parser = Parser()
    actual = parser.parse(
        "1 (8 ounce) package toffee baking bits (such as SKOR®)")
    expected = {
        "comment": "8 ounce) package toffee baking bits (such as SKOR\u00ae",
        "material": "",
        "original":
        "1 (8 ounce) package toffee baking bits (such as SKOR\u00ae)",
        "quantities": [[1.0, ""]]
    }

    assert actual == expected
示例#14
0
def test_ingredient():
    parser = Parser()
    actual = parser.parse("2 tablespoons dried cranberries")
    expected = {
    "comment": "",
    "material": "dried cranberries",
    "original": "2 tablespoons dried cranberries",
    "quantities": [
        [
            2.0,
            "tablespoon"
        ]
    ]
}

    assert actual == expected
示例#15
0
def test_ingredient():
    parser = Parser()
    actual = parser.parse("1 (8 ounce) package cream cheese")
    expected = {
    "comment": "8 ounce",
    "material": "package cream cheese",
    "original": "1 (8 ounce) package cream cheese",
    "quantities": [
        [
            1.0,
            ""
        ]
    ]
}

    assert actual == expected
def test_ingredient():
    parser = Parser()
    actual = parser.parse("1 small onion, chopped")
    expected = {
    "comment": "chopped",
    "material": "small onion",
    "original": "1 small onion, chopped",
    "quantities": [
        [
            1.0,
            ""
        ]
    ]
}

    assert actual == expected
def test_ingredient():
    parser = Parser()
    actual = parser.parse("3 cloves garlic, chopped")
    expected = {
    "comment": "chopped",
    "material": "cloves garlic",
    "original": "3 cloves garlic, chopped",
    "quantities": [
        [
            3.0,
            ""
        ]
    ]
}

    assert actual == expected
示例#18
0
def test_ingredient():
    parser = Parser()
    actual = parser.parse("2 quarts water")
    expected = {
    "comment": "",
    "material": "water",
    "original": "2 quarts water",
    "quantities": [
        [
            2.0,
            "quart"
        ]
    ]
}

    assert actual == expected
示例#19
0
def gen_tests():
    p = Parser()
    i = 0
    for ingredient in _get_practice_ingredients():
        filename = f"test_{ingredient.replace(' ', '_').replace('/','slash')}.py"
        filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                filename)
        content = test_content.format(
            ingredient=ingredient,
            expected=json.dumps(p.parse(ingredient), indent=4, sort_keys=True),
        )

        with open(filepath, "w") as f:
            f.write(content)
        print(filepath)
        i += 1
        time.sleep(1)
示例#20
0
import os
from urllib.parse import urlparse

import sqlalchemy
from sqlalchemy import Column, String, JSON, ForeignKey, Integer
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

from ingredients.parse import Parser

pwd = os.path.dirname(os.path.abspath(__file__))
engine = create_engine("sqlite:///" + os.path.join(pwd, "db.sqlite"))
Base = declarative_base()
Session = sessionmaker(bind=engine)
ingredient_parser = Parser()


class Recipe(Base):
    __tablename__ = 'recipes'
    pub_id = Column(String, primary_key=True)
    url = Column(String)
    title = Column(String)
    ingredients = Column(JSON)
    instructions = Column(JSON)
    tags = Column(JSON)
    images = Column(JSON)

    def __repr__(self):
        return f"<Recipe id='{self.pub_id}'>"