Recipe Images

This commit is contained in:
jableader 2024-01-13 19:44:07 +11:00
parent 9e03f707dc
commit 3be9bf4786
5 changed files with 82 additions and 15 deletions

24
main.py
View file

@ -1,5 +1,5 @@
import sqlite3 import sqlite3
import products, recipes, db, meals import products, recipes, db, meals, persons
import datetime import datetime
from pydantic import BaseModel from pydantic import BaseModel
@ -64,6 +64,14 @@ async def load_full_recipe(conn: sqlite3.Connection, id: int) -> recipes.Recipe:
return r return r
@app.get("/recipes/")
async def get_recipes(conn: sqlite3.Connection = Depends(get_db)) -> List[recipes.Recipe]:
result = []
async for recipe in recipes.get_all(conn):
result.append(recipe)
return result
@app.get("/recipes/{recipe_id}") @app.get("/recipes/{recipe_id}")
async def get_recipe(recipe_id: int, conn: sqlite3.Connection = Depends(get_db)) -> recipes.Recipe: async def get_recipe(recipe_id: int, conn: sqlite3.Connection = Depends(get_db)) -> recipes.Recipe:
r = await load_full_recipe(conn, recipe_id) r = await load_full_recipe(conn, recipe_id)
@ -113,3 +121,17 @@ async def create_meal(meal: meals.Meal, conn: sqlite3.Connection = Depends(get_d
await meals.insert_meal(conn, meal) await meals.insert_meal(conn, meal)
await conn.commit() await conn.commit()
return meal return meal
@app.get("/persons/")
async def get_persons(conn: sqlite3.Connection = Depends(get_db)) -> List[meals.Person]:
result = []
async for person in persons.get_all(conn):
result.append(person)
return result
@app.post("/persons/")
async def create_person(person: persons.Person, conn: sqlite3.Connection = Depends(get_db)) -> persons.Person:
await persons.insert(conn, person)
await conn.commit()
return person

View file

@ -1 +1 @@
from persons.db import Person from persons.db import *

View file

@ -6,7 +6,35 @@ class Person(BaseModel):
name: str name: str
async def create(conn): async def create(conn):
return None await conn.execute('''
CREATE TABLE IF NOT EXISTS Person (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE
);''')
def get(conn, id: int) -> Person: async def get_by_id(conn, id: int) -> Person:
return Person(id=id, name='test') cursor = await conn.execute('''
SELECT id, name
FROM Person
WHERE id = ?
''', (id,))
row = await cursor.fetchone()
if not row:
return None
return Person(id=row[0], name=row[1])
async def get_all(conn) -> List[Person]:
async with conn.execute('''
SELECT id, name
FROM Person
''') as cursor:
async for row in cursor:
yield Person(id=row[0], name=row[1])
async def insert(conn, person: Person) -> Person:
cursor = await conn.execute('''
INSERT INTO Person (name)
VALUES (?)
''', (person.name,))
person.id = cursor.lastrowid
return person

View file

@ -1,10 +1,9 @@
from products import Product from products import Product, find_product_by_tag
from recipes.db import Recipe, Ingredient, insert_recipe, insert_ingredient, find_recipe_by_id, find_ingredients_by_recipe_id from recipes.db import Recipe, Ingredient, insert_recipe, insert_ingredient, find_recipe_by_id, find_ingredients_by_recipe_id, get_all
from recipes.scraping import scrape_recipe from recipes.scraping import scrape_recipe
from ingredient_parser import parse_multiple_ingredients from ingredient_parser import parse_multiple_ingredients
from typing import List from typing import List
from products import find_product_by_tag
import json, units import json, units
@ -51,10 +50,13 @@ async def match_existing_products(conn, ingredients: List[Ingredient]) -> List[I
async def _get_recipe_from_ldata(conn, url: str, ldata: dict) -> dict: async def _get_recipe_from_ldata(conn, url: str, ldata: dict) -> dict:
ingredients = parse_ingredient_from_nlp(ldata['recipeIngredient']) ingredients = parse_ingredient_from_nlp(ldata['recipeIngredient'])
ingredients = await match_existing_products(conn, ingredients) ingredients = await match_existing_products(conn, ingredients)
name = ldata['name'] if 'name' in ldata else url
images = ldata['image'] if 'image' in ldata else []
return Recipe( return Recipe(
id=0, id=0,
name=ldata['name'], name=name,
link=url, link=url,
image_urls=images,
raw_data=json.dumps(ldata), raw_data=json.dumps(ldata),
ingredients=ingredients ingredients=ingredients
) )

View file

@ -1,3 +1,4 @@
import json
import aiosqlite import aiosqlite
from products import Product from products import Product
@ -18,11 +19,12 @@ class Ingredient(BaseModel):
product: Product = None product: Product = None
class Recipe(BaseModel): class Recipe(BaseModel):
KEYS: ClassVar[List[str]] = ['id', 'name', 'link', 'raw_data'] KEYS: ClassVar[List[str]] = ['id', 'name', 'link', 'image_urls', 'raw_data']
id: int id: int
name: str name: str
link: str link: str
raw_data: str raw_data: str
image_urls: List[str] = []
ingredients: List[Ingredient] = [] ingredients: List[Ingredient] = []
async def create(conn): async def create(conn):
@ -31,6 +33,7 @@ async def create(conn):
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name TEXT, name TEXT,
link TEXT, link TEXT,
image_urls TEXT,
raw_data TEXT raw_data TEXT
);''') );''')
@ -57,11 +60,16 @@ async def insert_ingredient(conn, ingredient: Ingredient):
async def insert_recipe(conn, recipe: Recipe): async def insert_recipe(conn, recipe: Recipe):
async with conn.execute(''' async with conn.execute('''
INSERT INTO Recipe (name, link, raw_data) INSERT INTO Recipe (name, link, raw_data, image_urls)
VALUES (?, ?, ?) VALUES (?, ?, ?)
''', (recipe.name, recipe.link, recipe.raw_data)) as cursor: ''', (recipe.name, recipe.link, recipe.raw_data, json.dumps(recipe.image_urls))) as cursor:
recipe.id = cursor.lastrowid recipe.id = cursor.lastrowid
def row_to_recipe(row) -> Recipe:
d = {k:v for k,v in zip(Recipe.KEYS, row)}
d['img_urls'] = json.loads(d['img_urls'])
return Recipe(**d)
async def find_recipe_by_id(conn, recipe_id: int) -> Recipe: async def find_recipe_by_id(conn, recipe_id: int) -> Recipe:
async with conn.execute(f''' async with conn.execute(f'''
SELECT {','.join(Recipe.KEYS)} FROM Recipe SELECT {','.join(Recipe.KEYS)} FROM Recipe
@ -69,7 +77,7 @@ async def find_recipe_by_id(conn, recipe_id: int) -> Recipe:
LIMIT 1 LIMIT 1
''', (recipe_id,)) as cursor: ''', (recipe_id,)) as cursor:
async for row in cursor: async for row in cursor:
return Recipe(**{k:v for k,v in zip(Recipe.KEYS, row)}) return row_to_recipe(row)
async def find_ingredients_by_recipe_id(conn, recipe_id: int) -> List[Ingredient]: async def find_ingredients_by_recipe_id(conn, recipe_id: int) -> List[Ingredient]:
async with conn.execute(f''' async with conn.execute(f'''
@ -78,3 +86,10 @@ async def find_ingredients_by_recipe_id(conn, recipe_id: int) -> List[Ingredient
''', (recipe_id,)) as cursor: ''', (recipe_id,)) as cursor:
async for row in cursor: async for row in cursor:
yield Ingredient(**{k:v for k,v in zip(Ingredient.KEYS, row)}) yield Ingredient(**{k:v for k,v in zip(Ingredient.KEYS, row)})
async def get_all(conn) -> List[Recipe]:
async with conn.execute(f'''
SELECT {','.join(Recipe.KEYS)} FROM Recipe
''') as cursor:
async for row in cursor:
yield row_to_recipe(row)