From 97845bc2dc3dd1a0e78b86f866c2b4b9ddd2f236 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Mon, 17 Jun 2024 19:40:38 +0200 Subject: [PATCH 1/3] brewfatherapiv2test --- cbpi/__init__.py | 2 +- cbpi/controller/upload_controller.py | 57 ++++++++++++++++++---------- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 9906eab..2c39745 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1,3 +1,3 @@ -__version__ = "4.4.1" +__version__ = "4.4.2.a1" __codename__ = "Yeast Starter" diff --git a/cbpi/controller/upload_controller.py b/cbpi/controller/upload_controller.py index 2c54e4b..51efffd 100644 --- a/cbpi/controller/upload_controller.py +++ b/cbpi/controller/upload_controller.py @@ -23,7 +23,6 @@ from ..api.step import StepMove, StepResult, StepState import re import base64 - class UploadController: def __init__(self, cbpi): @@ -70,9 +69,12 @@ class UploadController: return [] async def get_brewfather_recipes(self,offset=0): + limit = 50 + loop=0 + repeat = True brewfather = True result=[] - self.url="https://api.brewfather.app/v1/recipes" + self.url="https://api.brewfather.app/v2/recipes" brewfather_user_id = self.cbpi.config.get("brewfather_user_id", None) if brewfather_user_id == "" or brewfather_user_id is None: brewfather = False @@ -84,25 +86,40 @@ class UploadController: if brewfather == True: encodedData = base64.b64encode(bytes(f"{brewfather_user_id}:{brewfather_api_key}", "ISO-8859-1")).decode("ascii") headers={"Authorization": "Basic %s" % encodedData} - parameters={"limit": 50, 'offset': offset} - async with aiohttp.ClientSession(headers=headers) as bf_session: - async with bf_session.get(self.url, params=parameters) as r: - bf_recipe_list = await r.json() - await bf_session.close() + parameters={"limit": limit} + while repeat == True: + try: + async with aiohttp.ClientSession(headers=headers) as bf_session: + async with bf_session.get(self.url, params=parameters) as r: + bf_recipe_list = await r.json() + await bf_session.close() + except Exception as e: + logging.error(e) - if bf_recipe_list: - for row in bf_recipe_list: - recipe_id = row['_id'] - name = row['name'] - element = {'value': recipe_id, 'label': name} - result.append(element) - return result - else: - return [] - - else: - return [] + if bf_recipe_list: + loop +=1 + for row in bf_recipe_list: + recipe_id = row['_id'] + name = row['name'] + element = {'value': recipe_id, 'label': name} + result.append(element) + else: + repeat = False + if len(bf_recipe_list) != limit: + logging.info(loop) + repeat = False + else: + parameters={"limit": limit, 'start_after': recipe_id} + + logging.info(len(result)) + try: + newlist = sorted(result, key=lambda d: d['label']) + logging.error(newlist) + return newlist + except: + return result + def get_creation_path(self): creation_path = self.cbpi.config.get("RECIPE_CREATION_PATH", "upload") @@ -738,7 +755,7 @@ class UploadController: brewfather = True result=[] - self.bf_url="https://api.brewfather.app/v1/recipes/" + Recipe_ID + self.bf_url="https://api.brewfather.app/v2/recipes/" + Recipe_ID brewfather_user_id = self.cbpi.config.get("brewfather_user_id", None) if brewfather_user_id == "" or brewfather_user_id is None: brewfather = False From 45592733da9822e70951b496c4a53f0399c1e94f Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Tue, 18 Jun 2024 06:57:53 +0200 Subject: [PATCH 2/3] reduce logging on bf v2 api calls --- cbpi/__init__.py | 2 +- cbpi/controller/upload_controller.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 2c39745..2214a03 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1,3 +1,3 @@ -__version__ = "4.4.2.a1" +__version__ = "4.4.2.a2" __codename__ = "Yeast Starter" diff --git a/cbpi/controller/upload_controller.py b/cbpi/controller/upload_controller.py index 51efffd..6e967ed 100644 --- a/cbpi/controller/upload_controller.py +++ b/cbpi/controller/upload_controller.py @@ -70,7 +70,7 @@ class UploadController: async def get_brewfather_recipes(self,offset=0): limit = 50 - loop=0 + #loop=0 repeat = True brewfather = True result=[] @@ -97,7 +97,7 @@ class UploadController: logging.error(e) if bf_recipe_list: - loop +=1 + #loop +=1 for row in bf_recipe_list: recipe_id = row['_id'] name = row['name'] @@ -107,15 +107,15 @@ class UploadController: repeat = False if len(bf_recipe_list) != limit: - logging.info(loop) + #logging.info(loop) repeat = False else: parameters={"limit": limit, 'start_after': recipe_id} - logging.info(len(result)) + #logging.info(len(result)) try: newlist = sorted(result, key=lambda d: d['label']) - logging.error(newlist) + #logging.info(newlist) return newlist except: return result From 31aa275d9632c265fb7ba67e030a3dc93e618234 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Tue, 18 Jun 2024 17:28:31 +0200 Subject: [PATCH 3/3] add parameter for bf recipe list length --- cbpi/__init__.py | 2 +- cbpi/extension/ConfigUpdate/__init__.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 2214a03..517bf0f 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1,3 +1,3 @@ -__version__ = "4.4.2.a2" +__version__ = "4.4.2.a3" __codename__ = "Yeast Starter" diff --git a/cbpi/extension/ConfigUpdate/__init__.py b/cbpi/extension/ConfigUpdate/__init__.py index eba146a..95f2a6c 100644 --- a/cbpi/extension/ConfigUpdate/__init__.py +++ b/cbpi/extension/ConfigUpdate/__init__.py @@ -61,6 +61,7 @@ class ConfigUpdate(CBPiExtension): AddMashIn = self.cbpi.config.get("AddMashInStep", None) bfuserid = self.cbpi.config.get("brewfather_user_id", None) bfapikey = self.cbpi.config.get("brewfather_api_key", None) + bflistlength = self.cbpi.config.get("brewfather_list_length", None) RecipeCreationPath = self.cbpi.config.get("RECIPE_CREATION_PATH", None) BoilKettle = self.cbpi.config.get("BoilKettle", None) CONFIG_STATUS = self.cbpi.config.get("CONFIG_STATUS", None) @@ -244,6 +245,21 @@ class ConfigUpdate(CBPiExtension): await self.cbpi.config.add("brewfather_api_key", "", type=ConfigType.STRING, description="Brewfather API Key", source="craftbeerpi") except: logger.warning('Unable to update config') + + ## Check if Brewfather API Key is in config + + if bflistlength is None: + logger.info("INIT Brewfather Recipe List Length") + try: + await self.cbpi.config.add("brewfather_list_length", 50, type=ConfigType.SELECT, description="Brewfather Recipe List length", + source="craftbeerpi", + options= [{"label": "5", "value": 5}, + {"label": "10", "value": 10}, + {"label": "25", "value": 25}, + {"label": "50", "value": 50}, + {"label": "100", "value": 100}]) + except: + logger.warning('Unable to update config') ## Check if Brewfather API Key is in config