diff --git a/app.py b/app.py index 034dbe7..cfc2dc8 100644 --- a/app.py +++ b/app.py @@ -70,7 +70,7 @@ def add_product(dict_good, location): elif "description_cn" in dict_good: good_name = dict_good["description_cn"] if not good_name: - return False + return False, good_name if 'specification' in dict_good: good_name = good_name + " - " + dict_good['specification'] @@ -152,7 +152,7 @@ def add_product(dict_good, location): print("Request error:", err) grocy.add_product_by_barcode(dict_good["gtin"], 1.0, 0.0) - return True + return True, good_name def gpc_best_before_days_and_location(Code, locations): @@ -241,32 +241,34 @@ def handle_add_product(barcode, location): barcode_ = product_barcode break if barcode_: - amount = barcode_.amount + if hasattr(barcode_, "amount") and barcode_.amount: + amount = barcode_.amount grocy.add_product_by_barcode(barcode, amount, 0.0) response_data = {"message": "Item added successfully"} - return response_data, 200 + return response_data, 200, product.name except: spider = BarcodeSpider(x_rapidapi_key=X_RapidAPI_Key) good = spider.get_good(barcode) if not good: response_data = {"message": "Item not found"} - return response_data, 400 + return response_data, 400, None try: - if add_product(good, location): + added, good_name = add_product(good, location) + if added: response_data = {"message": "New item added successfully"} - return response_data, 200 + return response_data, 200, good_name else: response_data = {"message": "Fail to add new item"} - return response_data, 400 + return response_data, 400, None except Exception as e: if hasattr(e, "message"): error_message = e.message else: error_message = str(e) response_data = {"message": error_message} - return response_data, 400 + return response_data, 400, None @app.route("/") def index(): @@ -384,11 +386,13 @@ def consume_stream(): logger.info(f"Skip non-numeric barcode: {msg['data']}") continue - resp, status_code = handle_add_product(msg['data'], '') + resp, status_code, good_name = handle_add_product(msg['data'], '') + if not good_name: + good_name = '' if status_code == 200: # 消费完成后确认消息 r.xack(STREAM_KEY, CONSUMER_GROUP, msg_id) - bark_push('商品添加成功', '条形码 %s' % msg['data']) + bark_push('%s 添加成功' % good_name, '条形码 %s' % msg['data']) else: logger.error(f"Failed to process message: {msg['data']} {resp}") bark_push('商品添加失败', '条形码 %s' % msg['data'])