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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
"""Cookidoo API helpers."""
import json
import logging
import os
from typing import cast
import aiofiles
import isodate
from cookidoo_api.raw_types import (
AdditionalItemJSON,
AuthResponseJSON,
CalendarDayJSON,
CustomCollectionJSON,
CustomRecipeJSON,
IngredientJSON,
ItemJSON,
ManagedCollectionJSON,
QuantityJSON,
RecipeDetailsJSON,
RecipeJSON,
SubscriptionJSON,
UserInfoJSON,
)
from cookidoo_api.types import (
CookidooAdditionalItem,
CookidooAuthResponse,
CookidooCalendarDay,
CookidooCalendarDayRecipe,
CookidooCategory,
CookidooChapter,
CookidooChapterRecipe,
CookidooCollection,
CookidooCustomRecipe,
CookidooIngredient,
CookidooIngredientItem,
CookidooLocalizationConfig,
CookidooRecipeCollection,
CookidooShoppingRecipe,
CookidooShoppingRecipeDetails,
CookidooSubscription,
CookidooUserInfo,
)
_LOGGER = logging.getLogger(__name__)
localization_file_path = os.path.join(os.path.dirname(__file__), "localization.json")
def cookidoo_auth_data_from_json(
auth_data: AuthResponseJSON,
) -> CookidooAuthResponse:
"""Convert a auth data received from the API to a cookidoo auth data."""
return CookidooAuthResponse(
sub=auth_data["sub"],
access_token=auth_data["access_token"],
refresh_token=auth_data["refresh_token"],
token_type=auth_data["token_type"],
expires_in=auth_data["expires_in"],
)
def cookidoo_user_info_from_json(
user_info: UserInfoJSON,
) -> CookidooUserInfo:
"""Convert a user info received from the API to a cookidoo user info."""
return CookidooUserInfo(
username=user_info["username"],
description=user_info.get("description"),
picture=user_info["picture"],
)
def cookidoo_subscription_from_json(
subscription: SubscriptionJSON,
) -> CookidooSubscription:
"""Convert a subscription received from the API to a cookidoo subscription."""
return CookidooSubscription(
active=subscription["active"],
expires=subscription["expires"],
start_date=subscription["startDate"],
status=subscription["status"],
subscription_level=subscription["subscriptionLevel"],
subscription_source=subscription["subscriptionSource"],
type=subscription["type"],
extended_type=subscription["extendedType"],
)
def cookidoo_collection_from_json(
collection: CustomCollectionJSON | ManagedCollectionJSON,
) -> CookidooCollection:
"""Convert a collection received from the API to a cookidoo collection."""
return CookidooCollection(
id=collection["id"],
name=collection["title"],
description=cast(str, collection.get("description", None)),
chapters=[
CookidooChapter(
name=chapter["title"],
recipes=[
CookidooChapterRecipe(
id=recipe["id"],
name=recipe["title"],
total_time=int(float(recipe["totalTime"])),
)
for recipe in chapter["recipes"]
],
)
for chapter in collection["chapters"]
],
)
def cookidoo_recipe_from_json(
recipe: RecipeJSON,
) -> CookidooShoppingRecipe:
"""Convert a shopping recipe received from the API to a cookidoo shopping recipe."""
return CookidooShoppingRecipe(
id=recipe["id"],
name=recipe["title"],
ingredients=[
cookidoo_ingredient_from_json(ingredient)
for ingredient in recipe["recipeIngredientGroups"]
],
)
def cookidoo_quantity_from_json(
quantity: QuantityJSON,
) -> str:
"""Convert an quantity received from the API to a str."""
if "value" in quantity and quantity["value"]:
return str(quantity["value"])
elif (
"from" in quantity and "to" in quantity and quantity["from"] and quantity["to"]
):
return f"{quantity['from']} - {quantity['to']}"
else:
return ""
def cookidoo_recipe_details_from_json(
recipe: RecipeDetailsJSON,
) -> CookidooShoppingRecipeDetails:
"""Convert an recipe details received from the API to a cookidoo recipe details."""
return CookidooShoppingRecipeDetails(
id=recipe["id"],
name=recipe["title"],
ingredients=[
cookidoo_ingredient_from_json(ingredient)
for ingredientGroup in recipe["recipeIngredientGroups"]
for ingredient in ingredientGroup["recipeIngredients"]
],
difficulty=recipe["difficulty"],
notes=[
additional_notes["content"]
for additional_notes in recipe["additionalInformation"]
],
categories=[
CookidooCategory(
id=category["id"], name=category["title"], notes=category["subtitle"]
)
for category in recipe["categories"]
],
collections=[
CookidooRecipeCollection(
id=collection["id"],
name=collection["title"],
total_recipes=collection["recipesCount"]["value"],
)
for collection in recipe["inCollections"]
],
utensils=[utensil["utensilNotation"] for utensil in recipe["recipeUtensils"]],
serving_size=recipe["servingSize"]["quantity"]["value"] or 0,
active_time=next(
time_["quantity"]["value"]
for time_ in recipe["times"]
if time_["type"] == "activeTime" and time_["quantity"]["value"]
),
total_time=next(
time_["quantity"]["value"]
for time_ in recipe["times"]
if time_["type"] == "totalTime" and time_["quantity"]["value"]
),
)
def cookidoo_custom_recipe_from_json(
recipe: CustomRecipeJSON,
) -> CookidooCustomRecipe:
"""Convert a custom recipe received from the API to a cookidoo custom recipe."""
total_time = isodate.parse_duration(
recipe["recipeContent"]["totalTime"]
).total_seconds()
active_time = isodate.parse_duration(
recipe["recipeContent"]["prepTime"]
).total_seconds()
return CookidooCustomRecipe(
id=recipe["recipeId"],
name=recipe["recipeContent"]["name"],
ingredients=recipe["recipeContent"]["recipeIngredient"],
instructions=recipe["recipeContent"]["recipeInstructions"],
serving_size=recipe["recipeContent"]["recipeYield"]["value"],
total_time=int(total_time) if isinstance(total_time, float) else 0,
active_time=int(active_time) if isinstance(active_time, float) else 0,
tools=recipe["recipeContent"]["tool"],
)
def cookidoo_ingredient_from_json(
ingredient: IngredientJSON | ItemJSON,
) -> CookidooIngredient:
"""Convert an ingredient received from the API to a cookidoo ingredient."""
return CookidooIngredient(
id=ingredient["localId"] if "localId" in ingredient else ingredient["id"], # type: ignore[typeddict-item]
name=ingredient["ingredientNotation"],
description=f"{cookidoo_quantity_from_json(ingredient['quantity'])} {ingredient['unitNotation']}"
if "unitNotation" in ingredient
and ingredient["unitNotation"]
and "quantity" in ingredient
and ingredient["quantity"]
else cookidoo_quantity_from_json(ingredient["quantity"])
if "quantity" in ingredient and ingredient["quantity"]
else "",
)
def cookidoo_ingredient_item_from_json(
item: ItemJSON,
) -> CookidooIngredientItem:
"""Convert an ingredient item received from the API to a cookidoo item."""
return CookidooIngredientItem(
id=item["id"],
name=item["ingredientNotation"],
is_owned=item["isOwned"],
description=f"{cookidoo_quantity_from_json(item['quantity'])} {item['unitNotation']}"
if "unitNotation" in item
and item["unitNotation"]
and "quantity" in item
and item["quantity"]
else str(cookidoo_quantity_from_json(item["quantity"]))
if "quantity" in item and item["quantity"]
else "",
)
def cookidoo_additional_item_from_json(
item: AdditionalItemJSON,
) -> CookidooAdditionalItem:
"""Convert an additional item received from the API to a cookidoo item."""
return CookidooAdditionalItem(
id=item["id"],
name=item["name"],
is_owned=item["isOwned"],
)
def cookidoo_calendar_day_from_json(
calendar_day: CalendarDayJSON,
) -> CookidooCalendarDay:
"""Convert a calendar day received from the API to a cookidoo item."""
return CookidooCalendarDay(
id=calendar_day["id"],
title=calendar_day["title"],
recipes=[
CookidooCalendarDayRecipe(
id=recipe["id"], name=recipe["title"], total_time=recipe["totalTime"]
)
for recipe in calendar_day["recipes"]
],
)
async def __get_localization_options(
country: str | None = None,
language: str | None = None,
) -> list[CookidooLocalizationConfig]:
async with aiofiles.open(localization_file_path, encoding="utf-8") as file:
options_ = cast(list[dict[str, str]], json.loads(await file.read()))
options = (CookidooLocalizationConfig(**x) for x in options_)
filtered_options = filter(
lambda option: (not country or option.country_code == country)
and (not language or option.language == language),
options,
)
return list(cast(list[CookidooLocalizationConfig], filtered_options))
async def get_localization_options(
country: str | None = None,
language: str | None = None,
) -> list[CookidooLocalizationConfig]:
"""Get a list of possible localization options."""
return await __get_localization_options(country, language)
async def get_country_options() -> list[str]:
"""Get a list of possible country options."""
return list({option.country_code for option in await get_localization_options()})
async def get_language_options() -> list[str]:
"""Get a list of possible language options."""
return list({option.language for option in await get_localization_options()})
|