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. The callback keeps
63 # the file in sync afterwards: the tokens change on every login and on
64 # every refresh, including the one a request performs on its own once
65 # the access token has expired, which also rotates the refresh token.
66 token_file = ".token"
67 cookidoo.on_auth_data_update = lambda _auth_data: cookidoo.save_token(
68 token_file
69 )
70 try:
71 cookidoo.load_token(token_file)
72 except Exception:
73 await cookidoo.login()
74
75 # Info
76 await cookidoo.get_user_info()
77 subscription = await cookidoo.get_active_subscription()
78
79 # Paired Thermomix appliances on the account
80 _devices = await cookidoo.get_devices()
81
82 # Appliances currently available for remote monitoring
83 _monitored = await cookidoo.get_monitored_device_ids()
84 # To receive live cook state, register a push token obtained from your own
85 # FCM client, then decode incoming data messages with
86 # cooking_activity_from_push(...):
87 # await cookidoo.register_push_token(fcm_token, mobile_app_id)
88 # activity = cooking_activity_from_push(message_data)
89
90 # 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 :)
91 ENABLE_PREMIUM = subscription and subscription.active
92
93 # Custom collections
94 added_custom_collection = await cookidoo.add_custom_collection(
95 "TEST_COLLECTION"
96 )
97 _custom_collections = await cookidoo.get_custom_collections()
98 await cookidoo.add_recipes_to_custom_collection(
99 added_custom_collection.id, ["r907015"]
100 )
101 _custom_collections = await cookidoo.get_custom_collections()
102 await cookidoo.remove_recipe_from_custom_collection(
103 added_custom_collection.id, "r907015"
104 )
105 _custom_collections = await cookidoo.get_custom_collections()
106 await cookidoo.remove_custom_collection(added_custom_collection.id)
107
108 # # Managed collections
109 _added_managed_collection = await cookidoo.add_managed_collection("col500401")
110 _managed_collections = await cookidoo.get_managed_collections()
111 await cookidoo.remove_managed_collection("col500401")
112
113 # Recipe details
114 _recipe_details = await cookidoo.get_recipe_details("r59322")
115
116 if ENABLE_PREMIUM:
117 # Custom recipe
118 added_custom_recipe = await cookidoo.add_custom_recipe_from(
119 "r59322", _recipe_details.serving_size
120 )
121 _custom_recipe = await cookidoo.get_custom_recipe(added_custom_recipe.id)
122
123 # Calendar recipes
124 _added_recipes_to_calendar = await cookidoo.add_recipes_to_calendar(
125 datetime.now().date(), ["r907015", "r59322"]
126 )
127 if ENABLE_PREMIUM:
128 _added_custom_recipes_to_calendar = (
129 await cookidoo.add_custom_recipes_to_calendar(
130 datetime.now().date(), [added_custom_recipe.id]
131 )
132 )
133 _recipes_in_calendar = await cookidoo.get_recipes_in_calendar_week(
134 datetime.now().date()
135 )
136 _removed_recipes_from_calendar = await cookidoo.remove_recipe_from_calendar(
137 datetime.now().date(), "r907015"
138 )
139 _removed_recipes_from_calendar = await cookidoo.remove_recipe_from_calendar(
140 datetime.now().date(), "r59322"
141 )
142 if ENABLE_PREMIUM:
143 _removed_custom_recipes_from_calendar = (
144 await cookidoo.remove_custom_recipe_from_calendar(
145 datetime.now().date(), added_custom_recipe.id
146 )
147 )
148 _recipes_in_calendar = await cookidoo.get_recipes_in_calendar_week(
149 datetime.now().date()
150 )
151
152 # Shopping list
153 await cookidoo.clear_shopping_list()
154
155 # Ingredients
156 added_ingredients = await cookidoo.add_ingredient_items_for_recipes(
157 ["r59322", "r907016"]
158 )
159 _edited_ingredients = await cookidoo.edit_ingredient_items_ownership(
160 [
161 CookidooIngredientItem(
162 **{**ingredient.__dict__, "is_owned": not ingredient.is_owned},
163 )
164 for ingredient in filter(
165 lambda ingredient: ingredient.name == "Hefe",
166 added_ingredients,
167 )
168 ]
169 )
170 _ingredients = await cookidoo.get_ingredient_items()
171 _recipes = await cookidoo.get_shopping_list_recipes()
172 await cookidoo.remove_ingredient_items_for_recipes(["r59322"])
173
174 if ENABLE_PREMIUM:
175 added_ingredients = await cookidoo.add_ingredient_items_for_custom_recipes(
176 [added_custom_recipe.id]
177 )
178 _edited_ingredients = await cookidoo.edit_ingredient_items_ownership(
179 [
180 CookidooIngredientItem(
181 **{**ingredient.__dict__, "is_owned": not ingredient.is_owned},
182 )
183 for ingredient in filter(
184 lambda ingredient: "Hefe" in ingredient.name,
185 added_ingredients,
186 )
187 ]
188 )
189
190 _ingredients = await cookidoo.get_ingredient_items()
191 _recipes = await cookidoo.get_shopping_list_recipes()
192 await cookidoo.remove_ingredient_items_for_custom_recipes(
193 [added_custom_recipe.id]
194 )
195
196 # Remove after usage
197 await cookidoo.remove_custom_recipe(added_custom_recipe.id)
198
199 # Additional items
200 added_additional_items = await cookidoo.add_additional_items(
201 ["Fleisch", "Fisch"]
202 )
203 edited_additional_items = await cookidoo.edit_additional_items_ownership(
204 [
205 CookidooAdditionalItem(
206 **{
207 **additional_item.__dict__,
208 "is_owned": not additional_item.is_owned,
209 },
210 )
211 for additional_item in filter(
212 lambda additional_item: additional_item.name == "Fisch",
213 added_additional_items,
214 )
215 ]
216 )
217 await cookidoo.edit_additional_items(
218 [
219 CookidooAdditionalItem(
220 **{
221 **additional_item.__dict__,
222 "is_owned": "Vogel",
223 },
224 )
225 for additional_item in filter(
226 lambda additional_item: additional_item.name == "Fisch",
227 edited_additional_items,
228 )
229 ]
230 )
231 _additional_items = await cookidoo.get_additional_items()
232 await cookidoo.remove_additional_items(
233 [
234 added_additional_item.id
235 for added_additional_item in added_additional_items
236 ]
237 )
238
239
240 asyncio.run(main())