File: example.py

package info (click to toggle)
cookidoo-api 0.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,040 kB
  • sloc: python: 7,434; sh: 14; makefile: 3
file content (215 lines) | stat: -rwxr-xr-x 7,906 bytes parent folder | download
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
#!/usr/bin/env python3
"""Example script for cookidoo-api."""

import asyncio
from datetime import datetime
import logging
import os
import sys

import aiohttp
from dotenv import load_dotenv

from cookidoo_api import Cookidoo
from cookidoo_api.helpers import (
    get_country_options,
    get_language_options,
    get_localization_options,
)
from cookidoo_api.types import (
    CookidooAdditionalItem,
    CookidooConfig,
    CookidooIngredientItem,
)

load_dotenv()

# Configure the root logger
logging.basicConfig(
    level=logging.DEBUG,  # Set the logging level (DEBUG, INFO, WARNING, etc.)
    format="%(asctime)s [%(levelname)8s] %(name)s:%(lineno)s %(message)s",  # Format of the log messages
    handlers=[  # Specify the handlers for the logger
        logging.StreamHandler(sys.stdout)  # Output to stdout
    ],
)


async def main():
    """Run main example function."""
    async with aiohttp.ClientSession() as session:
        # Show all country_codes, languages and some localizations
        _country_codes = await get_country_options()
        _languages = await get_language_options()
        _localizations_ch = await get_localization_options(country="ch")
        _localizations_en = await get_localization_options(language="en")

        # Create Cookidoo instance with email and password
        cookidoo = Cookidoo(
            session,
            cfg=CookidooConfig(
                email=os.environ["EMAIL"],
                password=os.environ["PASSWORD"],
                localization=(
                    await get_localization_options(country="ie", language="en-GB")
                )[0],
            ),
        )
        # Login
        await cookidoo.login()
        await cookidoo.refresh_token()

        # Info
        await cookidoo.get_user_info()
        subscription = await cookidoo.get_active_subscription()

        # 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 :)
        ENABLE_PREMIUM = subscription and subscription.active

        # Custom collections
        added_custom_collection = await cookidoo.add_custom_collection(
            "TEST_COLLECTION"
        )
        _custom_collections = await cookidoo.get_custom_collections()
        await cookidoo.add_recipes_to_custom_collection(
            added_custom_collection.id, ["r907015"]
        )
        _custom_collections = await cookidoo.get_custom_collections()
        await cookidoo.remove_recipe_from_custom_collection(
            added_custom_collection.id, "r907015"
        )
        _custom_collections = await cookidoo.get_custom_collections()
        await cookidoo.remove_custom_collection(added_custom_collection.id)

        # # Managed collections
        _added_managed_collection = await cookidoo.add_managed_collection("col500401")
        _managed_collections = await cookidoo.get_managed_collections()
        await cookidoo.remove_managed_collection("col500401")

        # Recipe details
        _recipe_details = await cookidoo.get_recipe_details("r59322")

        if ENABLE_PREMIUM:
            # Custom recipe
            added_custom_recipe = await cookidoo.add_custom_recipe_from(
                "r59322", _recipe_details.serving_size
            )
            _custom_recipe = await cookidoo.get_custom_recipe(added_custom_recipe.id)

        # Calendar recipes
        _added_recipes_to_calendar = await cookidoo.add_recipes_to_calendar(
            datetime.now().date(), ["r907015", "r59322"]
        )
        if ENABLE_PREMIUM:
            _added_custom_recipes_to_calendar = (
                await cookidoo.add_custom_recipes_to_calendar(
                    datetime.now().date(), [added_custom_recipe.id]
                )
            )
        _recipes_in_calendar = await cookidoo.get_recipes_in_calendar_week(
            datetime.now().date()
        )
        _removed_recipes_from_calendar = await cookidoo.remove_recipe_from_calendar(
            datetime.now().date(), "r907015"
        )
        _removed_recipes_from_calendar = await cookidoo.remove_recipe_from_calendar(
            datetime.now().date(), "r59322"
        )
        if ENABLE_PREMIUM:
            _removed_custom_recipes_from_calendar = (
                await cookidoo.remove_custom_recipe_from_calendar(
                    datetime.now().date(), added_custom_recipe.id
                )
            )
        _recipes_in_calendar = await cookidoo.get_recipes_in_calendar_week(
            datetime.now().date()
        )

        # Shopping list
        await cookidoo.clear_shopping_list()

        # Ingredients
        added_ingredients = await cookidoo.add_ingredient_items_for_recipes(
            ["r59322", "r907016"]
        )
        _edited_ingredients = await cookidoo.edit_ingredient_items_ownership(
            [
                CookidooIngredientItem(
                    **{**ingredient.__dict__, "is_owned": not ingredient.is_owned},
                )
                for ingredient in filter(
                    lambda ingredient: ingredient.name == "Hefe",
                    added_ingredients,
                )
            ]
        )
        _ingredients = await cookidoo.get_ingredient_items()
        _recipes = await cookidoo.get_shopping_list_recipes()
        await cookidoo.remove_ingredient_items_for_recipes(["r59322"])

        if ENABLE_PREMIUM:
            added_ingredients = await cookidoo.add_ingredient_items_for_custom_recipes(
                [added_custom_recipe.id]
            )
            _edited_ingredients = await cookidoo.edit_ingredient_items_ownership(
                [
                    CookidooIngredientItem(
                        **{**ingredient.__dict__, "is_owned": not ingredient.is_owned},
                    )
                    for ingredient in filter(
                        lambda ingredient: "Hefe" in ingredient.name,
                        added_ingredients,
                    )
                ]
            )

            _ingredients = await cookidoo.get_ingredient_items()
            _recipes = await cookidoo.get_shopping_list_recipes()
            await cookidoo.remove_ingredient_items_for_custom_recipes(
                [added_custom_recipe.id]
            )

            # Remove after usage
            await cookidoo.remove_custom_recipe(added_custom_recipe.id)

        # Additional items
        added_additional_items = await cookidoo.add_additional_items(
            ["Fleisch", "Fisch"]
        )
        edited_additional_items = await cookidoo.edit_additional_items_ownership(
            [
                CookidooAdditionalItem(
                    **{
                        **additional_item.__dict__,
                        "is_owned": not additional_item.is_owned,
                    },
                )
                for additional_item in filter(
                    lambda additional_item: additional_item.name == "Fisch",
                    added_additional_items,
                )
            ]
        )
        await cookidoo.edit_additional_items(
            [
                CookidooAdditionalItem(
                    **{
                        **additional_item.__dict__,
                        "is_owned": "Vogel",
                    },
                )
                for additional_item in filter(
                    lambda additional_item: additional_item.name == "Fisch",
                    edited_additional_items,
                )
            ]
        )
        _additional_items = await cookidoo.get_additional_items()
        await cookidoo.remove_additional_items(
            [
                added_additional_item.id
                for added_additional_item in added_additional_items
            ]
        )


asyncio.run(main())