From 4c76880cde749c4b600839625dffbb6b31a2e6f1 Mon Sep 17 00:00:00 2001 From: Ching Date: Sun, 13 Oct 2024 14:01:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(api):=20=E4=BF=AE=E5=A4=8D=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=B8=8B=E5=8E=A8=E6=88=BF=E8=8F=9C=E8=B0=B1=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 68d69e8..b384ce3 100644 --- a/app.py +++ b/app.py @@ -344,7 +344,7 @@ def add_recipe_from_url(url): resp = grocy.get_generic_objects_for_type(EntityType.RECIPES, ['name~%s' % recipe_number]) if resp: response_data = {"message": "Recipe already exists"} - return jsonify(response_data), 400 + return jsonify(response_data), 400, None xcf_recipe = get_recipe_from_xiachufang(url) description = "" + '来源' + "" description += "
" @@ -379,10 +379,11 @@ def add_recipe_from_url(url): } grocy.add_generic(EntityType.RECIPES, data_grocy) response_data = {"message": f"{data_grocy['name']}"} + return response_data, 200, data_grocy['name'] except Exception as e: error_message = str(e) response_data = {"message": error_message} - return response_data, 400 + return response_data, 400, None @app.route("/add_recipe", methods=["POST"]) @@ -429,7 +430,12 @@ def consume_stream(): for stream, msgs in messages: for msg_id, msg in msgs: # 打印或处理消息 - logger.info(f"Processing message: {msg['data']}") + if msg.get('url'): + logger.info(f"Processing recipe message: {msg.get('url')}") + elif msg.get('data'): + logger.info(f"Processing message: {msg.get('data')}") + else: + logger.info(f"Processing message: {msg}") if msg.get('data'): if not msg['data'].isnumeric(): if msg['data'] == consume_product_flag_key: @@ -475,10 +481,10 @@ def consume_stream(): if msg.get('url'): - resp, status_code = add_recipe_from_url(msg['url']) + resp, status_code, recipe_name = add_recipe_from_url(msg['url']) if status_code == 200: r.xack(STREAM_KEY, CONSUMER_GROUP, msg_id) - bark_push(resp['message'], '来源 %s' % msg['url']) + bark_push(f'{recipe_name} 添加成功') else: logger.error(f"Failed to process message: {msg['url']} {resp}") bark_push('食谱添加失败', '%s 来源 %s' % (resp['message'], msg['url']))