munch-ease-backend/product.py
2024-01-08 11:38:17 +11:00

26 lines
No EOL
829 B
Python

import httpx
import re
from model import Product
import json
from typing import List
async def _get_woolies_data(url: str) -> dict:
woolies_regex = r'https://www.woolworths.com.au/shop/productdetails/(\d+)/'
match = re.match(woolies_regex, url)
if match:
product_id = match.group(1)
async with httpx.AsyncClient() as client:
response = await client.get(f'https://www.woolworths.com.au/apis/ui/product/detail/{product_id}')
response.raise_for_status()
return response.json()
async def create_product(url: str) -> Product:
data = await _get_woolies_data(url)
return Product(
id=0,
name=data['Name'],
link=url,
img_small=data['SmallImageFile'],
img_large=data['LargeImageFile'],
raw_data=json.dumps(data)
)