fix(api): 修复添加下厨房菜谱报错的问题
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ching 2024-10-13 14:01:45 +08:00
parent 168bbb9352
commit 4c76880cde

16
app.py
View File

@ -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 = "<a href=\"" + url + "\">" + '来源' + "</a>"
description += "<br>"
@ -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']))