feat(api): 增加扫码后 bark 推送消息功能
This commit is contained in:
parent
c46ea5bd16
commit
2f35ef2acb
19
app.py
19
app.py
@ -37,6 +37,7 @@ GROCY_DEFAULT_BEST_BEFORE_DAYS = os.environ.get("GROCY_DEFAULT_BEST_BEFORE_DAYS"
|
|||||||
X_RapidAPI_Key = os.environ.get("X_RapidAPI_Key")
|
X_RapidAPI_Key = os.environ.get("X_RapidAPI_Key")
|
||||||
REDIS_HOST = os.environ.get("REDIS_HOST")
|
REDIS_HOST = os.environ.get("REDIS_HOST")
|
||||||
REDIS_PORT = os.environ.get("REDIS_PORT")
|
REDIS_PORT = os.environ.get("REDIS_PORT")
|
||||||
|
BARK_DEVICE_KEY = os.environ.get("BARK_DEVICE_KEY")
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
grocy = Grocy(GROCY_URL, GROCY_API_KEY, GROCY_PORT, verify_ssl=True)
|
grocy = Grocy(GROCY_URL, GROCY_API_KEY, GROCY_PORT, verify_ssl=True)
|
||||||
@ -46,6 +47,17 @@ def get_locations():
|
|||||||
locations = grocy.get_generic_objects_for_type(EntityType.LOCATIONS)
|
locations = grocy.get_generic_objects_for_type(EntityType.LOCATIONS)
|
||||||
return locations
|
return locations
|
||||||
|
|
||||||
|
def bark_push(title, text):
|
||||||
|
icon = 'https://styles.redditmedia.com/t5_10dd8f/styles/communityIcon_rwl05b14iveb1.png'
|
||||||
|
data = {
|
||||||
|
"title": title,
|
||||||
|
"body": text,
|
||||||
|
"device_key": BARK_DEVICE_KEY,
|
||||||
|
"icon": icon,
|
||||||
|
"group": "Grocy",
|
||||||
|
"url": ""
|
||||||
|
}
|
||||||
|
requests.post('http://bark.tunpok.com/push/', json=data)
|
||||||
|
|
||||||
def add_product(dict_good, location):
|
def add_product(dict_good, location):
|
||||||
good_name = ""
|
good_name = ""
|
||||||
@ -373,11 +385,18 @@ def add_to_stream():
|
|||||||
data = request.json
|
data = request.json
|
||||||
if not data:
|
if not data:
|
||||||
return jsonify({"message": "No data provided"}), 400
|
return jsonify({"message": "No data provided"}), 400
|
||||||
|
if not data.get('data'):
|
||||||
|
return jsonify({"message": "No barcode data provided"}), 400
|
||||||
|
|
||||||
# 将数据添加到 Redis Stream 中
|
# 将数据添加到 Redis Stream 中
|
||||||
# 自动生成 ID ('*' 表示让 Redis 生成唯一 ID)
|
# 自动生成 ID ('*' 表示让 Redis 生成唯一 ID)
|
||||||
stream_id = r.xadd(STREAM_KEY, data)
|
stream_id = r.xadd(STREAM_KEY, data)
|
||||||
|
|
||||||
|
if data['data'].isnumeric():
|
||||||
|
bark_push('扫码成功', '条形码 %s' % data['data'])
|
||||||
|
else:
|
||||||
|
bark_push('扫码成功', '二维码 %s' % data['data'])
|
||||||
|
|
||||||
return jsonify({"message": "Data added to Redis Stream", "stream_id": stream_id}), 200
|
return jsonify({"message": "Data added to Redis Stream", "stream_id": stream_id}), 200
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user