Debian Codegraph @

example.py

raw
1 #!/usr/bin/env python3
2 """Example script for cookidoo-api."""
3
4 import asyncio
5 from datetime import datetime
6 import logging
7 import os
8 import sys
9
10 import aiohttp
11 from dotenv import load_dotenv
12
13 from cookidoo_api import Cookidoo
14 from cookidoo_api.helpers import (
15 get_country_options,
16 get_language_options,
17 get_localization_options,
18 )
19 from cookidoo_api.types import (
20 CookidooAdditionalItem,
21 CookidooConfig,
22 CookidooIngredientItem,
23 )
24
25 load_dotenv()
26
27 # Configure the root logger
28 logging.basicConfig(
29 level=logging.DEBUG, # Set the logging level (DEBUG, INFO, WARNING, etc.)
30 format="%(asctime)s [%(levelname)8s] %(name)s:%(lineno)s %(message)s", # Format of the log messages
31 handlers=[ # Specify the handlers for the logger
32 logging.StreamHandler(sys.stdout) # Output to stdout
33 ],
34 )
35
36
37 async def main():
38 """Run main example function."""
39 # Use CookieJar(unsafe=True) to support cross-domain cookies during login
40 jar = aiohttp.CookieJar(unsafe=True)
41 async with aiohttp.ClientSession(cookie_jar=jar) as session:
42 # Show all country_codes, languages and some localizations
43 _country_codes = await get_country_options()
44 _languages = await get_language_options()
45 _localizations_ch = await get_localization_options(country="ch")
46 _localizations_en = await get_localization_options(language="en")
47
48 # Create Cookidoo instance with email and password. The OAuth2 client
49 # identifiers default to the mobile app's public ones, nothing else to
50 # configure (see docs/oauth-client.md)
51 cookidoo = Cookidoo(
52 session,
53 cfg=CookidooConfig(
54 email=os.environ["EMAIL"],
55 password=os.environ["PASSWORD"],
56 localization=(
57 await get_localization_options(country="ie", language="en-GB")
58 )[0],
59 ),
60 )
61
62 # Try to reuse a saved token, otherwise login fresh
63 token_file = ".token"
64 try:
65 cookidoo.load_token(token_file)
66 except Exception:
67 await cookidoo.login()
68 cookidoo.save_token(token_file)
69
70 # Info
71 await cookidoo.get_user_info()
72 subscription = await cookidoo.get_active_subscription()
73
74 # Paired Thermomix appliances on the account
75 _devices = await cookidoo.get_devices()
76
77 # Appliances currently available for remote monitoring
78 _monitored = await cookidoo.get_monitored_device_ids()
79 # To receive live cook state, register a push token obtained from your own
80 # FCM client, then decode incoming data messages with
81 # cooking_activity_from_push(...):
82 # await cookidoo.register_push_token(fcm_token, mobile_app_id)
83 # activity = cooking_activity_from_push(message_data)
84
85 # Some features are only available for premium accounts. To get a premium account, you need to subscribe to the Cookidoo service. When creating a new account, you get 1 month of premium for free which is enough to test the premium features :)
86 ENABLE_PREMIUM = subscription and subscription.active
87
88 # Custom collections
89 added_custom_collection = await cookidoo.add_custom_collection(
90 "TEST_COLLECTION"
91 )
92 _custom_collections = await cookidoo.get_custom_collections()
93 await cookidoo.add_recipes_to_custom_collection(
94 added_custom_collection.id, ["r907015"]
95 )
96 _custom_collections = await cookidoo.get_custom_collections()
97 await cookidoo.remove_recipe_from_custom_collection(
98 added_custom_collection.id, "r907015"
99 )
100 _custom_collections = await cookidoo.get_custom_collections()
101 await cookidoo.remove_custom_collection(added_custom_collection.id)
102
103 # # Managed collections
104 _added_managed_collection = await cookidoo.add_managed_collection("col500401")
105 _managed_collections = await cookidoo.get_managed_collections()
106 await cookidoo.remove_managed_collection("col500401")
107
108 # Recipe details
109 _recipe_details = await cookidoo.get_recipe_details("r59322")
110
111 if ENABLE_PREMIUM:
112 # Custom recipe
113 added_custom_recipe = await cookidoo.add_custom_recipe_from(
114 "r59322", _recipe_details.serving_size
115 )
116 _custom_recipe = await cookidoo.get_custom_recipe(added_custom_recipe.id)
117
118 # Calendar recipes
119 _added_recipes_to_calendar = await cookidoo.add_recipes_to_calendar(
120 datetime.now().date(), ["r907015", "r59322"]
121 )
122 if ENABLE_PREMIUM:
123 _added_custom_recipes_to_calendar = (
124 await cookidoo.add_custom_recipes_to_calendar(
125 datetime.now().date(), [added_custom_recipe.id]
126 )
127 )
128 _recipes_in_calendar = await cookidoo.get_recipes_in_calendar_week(
129 datetime.now().date()
130 )
131 _removed_recipes_from_calendar = await cookidoo.remove_recipe_from_calendar(
132 datetime.now().date(), "r907015"
133 )
134 _removed_recipes_from_calendar = await cookidoo.remove_recipe_from_calendar(
135 datetime.now().date(), "r59322"
136 )
137 if ENABLE_PREMIUM:
138 _removed_custom_recipes_from_calendar = (
139 await cookidoo.remove_custom_recipe_from_calendar(
140 datetime.now().date(), added_custom_recipe.id
141 )
142 )
143 _recipes_in_calendar = await cookidoo.get_recipes_in_calendar_week(
144 datetime.now().date()
145 )
146
147 # Shopping list
148 await cookidoo.clear_shopping_list()
149
150 # Ingredients
151 added_ingredients = await cookidoo.add_ingredient_items_for_recipes(
152 ["r59322", "r907016"]
153 )
154 _edited_ingredients = await cookidoo.edit_ingredient_items_ownership(
155 [
156 CookidooIngredientItem(
157 **{**ingredient.__dict__, "is_owned": not ingredient.is_owned},
158 )
159 for ingredient in filter(
160 lambda ingredient: ingredient.name == "Hefe",
161 added_ingredients,
162 )
163 ]
164 )
165 _ingredients = await cookidoo.get_ingredient_items()
166 _recipes = await cookidoo.get_shopping_list_recipes()
167 await cookidoo.remove_ingredient_items_for_recipes(["r59322"])
168
169 if ENABLE_PREMIUM:
170 added_ingredients = await cookidoo.add_ingredient_items_for_custom_recipes(
171 [added_custom_recipe.id]
172 )
173 _edited_ingredients = await cookidoo.edit_ingredient_items_ownership(
174 [
175 CookidooIngredientItem(
176 **{**ingredient.__dict__, "is_owned": not ingredient.is_owned},
177 )
178 for ingredient in filter(
179 lambda ingredient: "Hefe" in ingredient.name,
180 added_ingredients,
181 )
182 ]
183 )
184
185 _ingredients = await cookidoo.get_ingredient_items()
186 _recipes = await cookidoo.get_shopping_list_recipes()
187 await cookidoo.remove_ingredient_items_for_custom_recipes(
188 [added_custom_recipe.id]
189 )
190
191 # Remove after usage
192 await cookidoo.remove_custom_recipe(added_custom_recipe.id)
193
194 # Additional items
195 added_additional_items = await cookidoo.add_additional_items(
196 ["Fleisch", "Fisch"]
197 )
198 edited_additional_items = await cookidoo.edit_additional_items_ownership(
199 [
200 CookidooAdditionalItem(
201 **{
202 **additional_item.__dict__,
203 "is_owned": not additional_item.is_owned,
204 },
205 )
206 for additional_item in filter(
207 lambda additional_item: additional_item.name == "Fisch",
208 added_additional_items,
209 )
210 ]
211 )
212 await cookidoo.edit_additional_items(
213 [
214 CookidooAdditionalItem(
215 **{
216 **additional_item.__dict__,
217 "is_owned": "Vogel",
218 },
219 )
220 for additional_item in filter(
221 lambda additional_item: additional_item.name == "Fisch",
222 edited_additional_items,
223 )
224 ]
225 )
226 _additional_items = await cookidoo.get_additional_items()
227 await cookidoo.remove_additional_items(
228 [
229 added_additional_item.id
230 for added_additional_item in added_additional_items
231 ]
232 )
233
234
235 asyncio.run(main())