feat(api): 添加ShowApiSpider类用于查询商品信息
All checks were successful
continuous-integration/drone Build is passing
All checks were successful
continuous-integration/drone Build is passing
- 添加ShowApiSpider类,用于通过条形码查询商品信息 - 修改app.py,引入ShowApiSpider类 - 修改add_product函数,使用ShowApiSpider查询商品信息 - 修改add_product函数,处理ShowApiSpider返回的商品信息
This commit is contained in:
parent
e8293c0a74
commit
d7fe6ffb14
36
app.py
36
app.py
@ -14,7 +14,7 @@ import redis
|
||||
import requests
|
||||
|
||||
|
||||
from barcode import BarcodeSpider
|
||||
from barcode import BarcodeSpider, ShowApiSpider
|
||||
from recipe import get_recipe_from_xiachufang
|
||||
|
||||
logger = loguru.logger
|
||||
@ -38,6 +38,7 @@ GROCY_DEFAULT_QUANTITY_UNIT_ID_PURCHASE = os.environ.get("GROCY_DEFAULT_QUANTITY
|
||||
GROCY_DEFAULT_QUANTITY_UNIT_ID_STOCK = os.environ.get("GROCY_DEFAULT_QUANTITY_UNIT_ID_STOCK")
|
||||
GROCY_DEFAULT_BEST_BEFORE_DAYS = os.environ.get("GROCY_DEFAULT_BEST_BEFORE_DAYS")
|
||||
X_RapidAPI_Key = os.environ.get("X_RapidAPI_Key")
|
||||
ShowApi_AppKey = os.environ.get("ShowApi_AppKey")
|
||||
REDIS_HOST = os.environ.get("REDIS_HOST")
|
||||
REDIS_PORT = os.environ.get("REDIS_PORT")
|
||||
BARK_DEVICE_KEY = os.environ.get("BARK_DEVICE_KEY")
|
||||
@ -86,13 +87,17 @@ def bark_push(title, text):
|
||||
|
||||
def add_product(dict_good, location):
|
||||
good_name = ""
|
||||
if "description" in dict_good:
|
||||
if 'goodsName' in dict_good:
|
||||
good_name = dict_good['goodsName']
|
||||
elif "description" in dict_good:
|
||||
good_name = dict_good["description"]
|
||||
elif "description_cn" in dict_good:
|
||||
good_name = dict_good["description_cn"]
|
||||
if not good_name:
|
||||
return False, good_name
|
||||
if 'specification' in dict_good:
|
||||
if 'spec' in dict_good:
|
||||
good_name = good_name + " - " + dict_good['spec']
|
||||
elif 'specification' in dict_good:
|
||||
good_name = good_name + " - " + dict_good['specification']
|
||||
|
||||
locations = get_locations()
|
||||
@ -133,17 +138,22 @@ def add_product(dict_good, location):
|
||||
)
|
||||
|
||||
# add barcode, ex. 06921168593910
|
||||
barcode = None
|
||||
if 'gtin' in dict_good:
|
||||
barcode = dict_good['gtin']
|
||||
elif 'code' in dict_good:
|
||||
barcode = dict_good['code']
|
||||
data_barcode = {
|
||||
"product_id": int(response_grocy["created_object_id"]),
|
||||
"barcode": dict_good["gtin"],
|
||||
"barcode": barcode,
|
||||
"amount": 1.0,
|
||||
}
|
||||
grocy.add_generic(EntityType.PRODUCT_BARCODES, data_barcode)
|
||||
# add barcode, EAN-13, ex. 6921168593910
|
||||
if dict_good["gtin"].startswith("0"):
|
||||
if barcode.startswith("0"):
|
||||
data_barcode = {
|
||||
"product_id": int(response_grocy["created_object_id"]),
|
||||
"barcode": dict_good["gtin"].lstrip("0"),
|
||||
"barcode": barcode.lstrip("0"),
|
||||
"amount": 1.0,
|
||||
}
|
||||
grocy.add_generic(EntityType.PRODUCT_BARCODES, data_barcode)
|
||||
@ -154,6 +164,8 @@ def add_product(dict_good, location):
|
||||
pic_url = dict_good["picfilename"]
|
||||
elif ("picture_filename" in dict_good) and dict_good["picture_filename"]:
|
||||
pic_url = dict_good["picture_filename"]
|
||||
elif 'img' in dict_good and dict_good['img']:
|
||||
pic_url = dict_good['img']
|
||||
|
||||
if pic_url:
|
||||
try:
|
||||
@ -172,7 +184,7 @@ def add_product(dict_good, location):
|
||||
except requests.exceptions.RequestException as err:
|
||||
print("Request error:", err)
|
||||
|
||||
grocy.add_product_by_barcode(dict_good["gtin"], 1.0, 0.0)
|
||||
grocy.add_product_by_barcode(barcode, 1.0, 0.0)
|
||||
return True, good_name
|
||||
|
||||
|
||||
@ -269,12 +281,18 @@ def handle_add_product(barcode, location):
|
||||
response_data = {"message": "Item added successfully"}
|
||||
return response_data, 200, product.name
|
||||
except:
|
||||
spider = BarcodeSpider(x_rapidapi_key=X_RapidAPI_Key)
|
||||
spiders = []
|
||||
spiders.append(ShowApiSpider(ShowApi_AppKey.split(",")))
|
||||
spiders.append(BarcodeSpider(x_rapidapi_key=X_RapidAPI_Key))
|
||||
|
||||
good = spider.get_good(barcode)
|
||||
for spider in spiders:
|
||||
good = spider.get_good(barcode)
|
||||
if good:
|
||||
break
|
||||
if not good:
|
||||
response_data = {"message": "Item not found"}
|
||||
return response_data, 400, None
|
||||
|
||||
try:
|
||||
added, good_name = add_product(good, location)
|
||||
if added:
|
||||
|
||||
58
barcode.py
58
barcode.py
@ -185,6 +185,64 @@ class BarcodeSpider:
|
||||
return self.get_imported_good(barcode)
|
||||
|
||||
|
||||
|
||||
class ShowApiSpider:
|
||||
"""
|
||||
{
|
||||
"spec": "300毫升",
|
||||
"sptmImg": "",
|
||||
"remark": "查询成功!",
|
||||
"img": "http://hj2.co/barcode/img/36be2cb461d5aa0e213755368be93af3",
|
||||
"ycg": "",
|
||||
"nw": "",
|
||||
"ret_code": "0",
|
||||
"description": "",
|
||||
"qs": "",
|
||||
"manuAddress": "",
|
||||
"note": "checkResult:1;备注:logout_flag:0;login_date:Jul 28 1993 12:00:00:000AM;valid_date:Jul 28 2023 12:00:00:000AM;宽:7.8;单位:CM;高:16.1;深:3.7;英文名称:Johnson's milk+rice bath 300ml;关键字:沐浴露;销售单位:BX;形态描述:个人护理用品;毛重:339;上市时间:2018-08-01;产地:上海;",
|
||||
"goodsType": "服装、箱包、个人护理用品>>个人护理用品>>洗浴、身体护理品>>皮肤护理品",
|
||||
"gpcType": "身体清洁/洗涤/香皂用品",
|
||||
"gw": "",
|
||||
"keyword": "沐浴露",
|
||||
"width": "",
|
||||
"gpc": "10000330",
|
||||
"code": "6907376500056",
|
||||
"hight": "",
|
||||
"depth": "",
|
||||
"manuName": "强生(中国)有限公司",
|
||||
"price": "",
|
||||
"flag": true,
|
||||
"imgList": [],
|
||||
"trademark": "强生婴儿",
|
||||
"goodsName": "强生婴儿牛奶沐浴露300毫升"
|
||||
}"""
|
||||
def __init__(self, app_keys):
|
||||
self.app_keys = app_keys
|
||||
self.base_url = "https://route.showapi.com/66-22"
|
||||
|
||||
def get_good(self, barcode):
|
||||
if barcode.startswith('069') or barcode.startswith('69'):
|
||||
# lstrip去掉左边的0
|
||||
barcode = barcode.lstrip('0')
|
||||
else:
|
||||
return None
|
||||
|
||||
for app_key in self.app_keys:
|
||||
url = self.base_url + "?appKey=%s" % app_key
|
||||
payload = {
|
||||
"code": str(barcode),
|
||||
}
|
||||
response = requests.get(url, params=payload)
|
||||
resp_data = response.json()
|
||||
if resp_data["showapi_res_code"] == 0 and resp_data['showapi_res_body']['ret_code'] == '0':
|
||||
good = resp_data["showapi_res_body"]
|
||||
del good["ret_code"]
|
||||
del good["remark"]
|
||||
del good["flag"]
|
||||
return good
|
||||
#TODO: raise exception and log
|
||||
return None
|
||||
|
||||
def main():
|
||||
# 国产商品
|
||||
# good = BarCodeSpider.get_good('06917878036526')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user