178 lines
5.9 KiB
Python
178 lines
5.9 KiB
Python
import datetime
|
||
import json
|
||
from jsonpath_ng import parse
|
||
import pymysql
|
||
import requests
|
||
from newsetting import All_Setting
|
||
import re
|
||
from decimal import Decimal
|
||
mapping_dict = {
|
||
"非及时达标签 1": ["预售", "厂家直发", "云超特卖", "xxH 发货"],
|
||
"非及时达标签 2": ["预约", "商家直发", "云卖场", "xxH 内发货"],
|
||
"非及时达标签 3": ["明日达", "厂商直发", "云超商城", "xx 小时内发快递"],
|
||
"非及时达标签 4": ["明日送达", "快递发货", "云仓特卖", "XX 陆续发货"],
|
||
"非及时达标签 5": ["次日达", "产地寻鲜", "甄选商城", "xx 月 xx 日发货"],
|
||
"非及时达标签 6": ["多日达", "地产量贩", "社区团购", "xx.xx 送达"],
|
||
"非及时达标签 7": ["即将开售", "跨境购", "xx 月 xx 日送达"],
|
||
}
|
||
tag_mapping = {
|
||
312: "冷藏",
|
||
319: "冷冻",
|
||
378: "明日达",
|
||
274: "严选",
|
||
320: "新品",
|
||
399: "地标",
|
||
430: "元宵节",
|
||
318: "冷鲜",
|
||
377: "无抗",
|
||
297: "秒杀价",
|
||
365: "新鲜快手菜",
|
||
198: "辣度2星",
|
||
199: "辣度1星",
|
||
163: "省心价",
|
||
263: "省心价",
|
||
379: "中国有机产品",
|
||
293: "日日鲜",
|
||
359: "黑金系列",
|
||
366: "手工鲜切",
|
||
148: "预定"}
|
||
|
||
def find_all_values_by_key(data, target_key):
|
||
results = []
|
||
if isinstance(data, dict):
|
||
for key, value in data.items():
|
||
if key == target_key:
|
||
results.append(value)
|
||
# 如果值是字典或列表,则递归查找
|
||
if isinstance(value, (dict, list)):
|
||
results.extend(find_all_values_by_key(value, target_key))
|
||
elif isinstance(data, list):
|
||
for item in data:
|
||
results.extend(find_all_values_by_key(item, target_key))
|
||
return results
|
||
|
||
def ocr_1(img_url):
|
||
import requests
|
||
from io import BytesIO
|
||
url = "http://192.168.5.2:9002/infer"
|
||
# img_url = 'https://p0.meituan.net/mallimages/04e59006293302d0808a5e9bff01d17a79341.png'
|
||
img_response = requests.get(img_url, timeout=10)
|
||
img_response.raise_for_status()
|
||
img_file = BytesIO(img_response.content)
|
||
files = {"file": ("your.jpg", img_file, "image/jpeg")}
|
||
r = requests.post(url, files=files, timeout=60)
|
||
return r.json()['full_text']
|
||
|
||
def clean_price(price_str):
|
||
if price_str is None or price_str == "":
|
||
return 0.0
|
||
clean_str = re.sub(r"[^\d.]", "", str(price_str))
|
||
try:
|
||
return float(clean_str)
|
||
except ValueError:
|
||
return 0.0
|
||
|
||
tent2 = All_Setting(host='183.6.7.91', port=33016, db='tk_test')
|
||
|
||
|
||
|
||
|
||
format_today = str(datetime.date.today()).replace("-","")
|
||
|
||
shop_id = 10000900
|
||
work_id = format_today+'测试'
|
||
|
||
|
||
# ["showContents"]["6"][0]["elementInfo"][
|
||
# "singleDataInfo"]["image"]["url"]
|
||
|
||
|
||
sql3 = f"""
|
||
select * FROM category_detail_log where shop_id= '{shop_id}' and work_id = '{work_id}'
|
||
"""
|
||
da123 = tent2.get_shoppingmalls(sql3)
|
||
print(da123)
|
||
input("开始?")
|
||
all_clean_data = []
|
||
for da in da123:
|
||
sql3 = f"""
|
||
select * FROM category_detail where shop_id= '{shop_id}' and one_id='{str(da[3])}' and two_id='{str(da[4])}' limit 1
|
||
"""
|
||
da1 = tent2.get_shoppingmalls(sql3)
|
||
try:data = json.loads(da[5])["data"]["contents"][0]["subModule"]["contents"]
|
||
except:
|
||
continue
|
||
print(f'当前1分类{da1[0][3]},2分类{da1[0][5]}')
|
||
for i in data:
|
||
all_image_urls = find_all_values_by_key(i, 'elementId')
|
||
all_mapping = find_all_values_by_key(i, 'text')
|
||
ocr_results = []
|
||
ocr_mapping = 1
|
||
for item in all_image_urls:
|
||
img_url = None
|
||
try:
|
||
if item in tag_mapping.keys():
|
||
print('elementId:'+str(item))
|
||
ocr_results.append(tag_mapping.get(item))
|
||
except Exception as e:
|
||
continue
|
||
for item in all_mapping:
|
||
img_url = None
|
||
try:
|
||
if any(item in lst for lst in mapping_dict.values()):
|
||
print('text:'+item)
|
||
ocr_mapping=0
|
||
except Exception as e:
|
||
continue
|
||
title = i.get("lxInfo", {}).get("content_name", "")
|
||
goods_id = i.get("logicContents", {}).get("skuId", "")
|
||
desc = i.get("lxInfo", {}).get("label", "")
|
||
price = i.get("lxInfo", {}).get("sale_price", 0.0)
|
||
original_price = i.get("lxInfo", {}).get("origin_price", 0.0)
|
||
goods_tag = '--'.join(ocr_results)
|
||
f_name = da1[0][3]
|
||
f_id = da[3]
|
||
s_name = da1[0][5]
|
||
s_id = da[4]
|
||
station_id = 2101
|
||
image = i.get("showContents", {}).get('6', {})[0].get('elementInfo',{}).get('singleDataInfo',{}).get('image',{}).get('url','')
|
||
# executemany 不能把 Python list 直接当占位符参数,否则会按 SQL 片段展开,整句 VALUES 语法被拆坏(1064)
|
||
is_immediate = 1 if ocr_mapping else 0
|
||
price_tag = '--'.join(ocr_results)
|
||
t_id = None
|
||
t_name = None
|
||
token = 'd7e0759c-8f52-4c82-8102-774da69416d5'
|
||
vip_price = i.get("lxInfo", {}).get("member_price", 0.0)
|
||
da2=(
|
||
work_id,
|
||
goods_id,
|
||
s_id,
|
||
station_id,
|
||
desc,
|
||
f_id,
|
||
f_name,
|
||
goods_tag,
|
||
image,
|
||
is_immediate,
|
||
clean_price(original_price),
|
||
clean_price(price),
|
||
price_tag,
|
||
s_name,
|
||
shop_id,
|
||
t_id,
|
||
t_name,
|
||
title,
|
||
token,
|
||
clean_price(vip_price)
|
||
)
|
||
all_clean_data.append(da2)
|
||
sql = """
|
||
INSERT INTO data_completed
|
||
(work_id, goods_id, s_id, station_id, `desc`, f_id, f_name, goods_tag, image,
|
||
is_immediate, original_price, price, price_tag, s_name, shop_id, t_id, t_name,
|
||
title, token, vip_price)
|
||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||
|
||
"""
|
||
tent2.write_data(sql, all_clean_data)
|