chore: snapshot local changes
This commit is contained in:
@@ -0,0 +1,437 @@
|
||||
import datetime
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from jsonpath_ng import parse
|
||||
import pymysql
|
||||
import requests
|
||||
from newsetting import All_Setting
|
||||
import re
|
||||
import time
|
||||
|
||||
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: "预定",
|
||||
191: "临期特惠",
|
||||
}
|
||||
|
||||
|
||||
def normalize_tag_text(text):
|
||||
text = str(text or "").strip()
|
||||
if not text:
|
||||
return ""
|
||||
return re.sub(r"_(折|赠)$", "", text)
|
||||
|
||||
|
||||
def append_unique_tag(tags, seen, text):
|
||||
text = normalize_tag_text(text)
|
||||
if not text or text in {"折", "赠"}:
|
||||
return
|
||||
if text not in seen:
|
||||
seen.add(text)
|
||||
tags.append(text)
|
||||
|
||||
|
||||
def extract_group_tag_texts(group_data):
|
||||
texts = []
|
||||
for part in group_data or []:
|
||||
text = part.get("singleDataInfo", {}).get("text", "")
|
||||
text = normalize_tag_text(text)
|
||||
if text:
|
||||
texts.append(text)
|
||||
if len(texts) > 1:
|
||||
texts = [text for text in texts if text not in {"折", "赠"}]
|
||||
return texts
|
||||
|
||||
|
||||
def build_goods_tag(item, image_tags):
|
||||
tags = []
|
||||
seen = set()
|
||||
|
||||
for tag in image_tags:
|
||||
append_unique_tag(tags, seen, tag)
|
||||
|
||||
show_contents = item.get("showContents", {})
|
||||
|
||||
for node in show_contents.get("7", []):
|
||||
element_info = node.get("elementInfo", {})
|
||||
if element_info.get("constructionType") == "Text":
|
||||
text = element_info.get("singleDataInfo", {}).get("text", "")
|
||||
append_unique_tag(tags, seen, text)
|
||||
|
||||
for node in show_contents.get("9", []):
|
||||
element_info = node.get("elementInfo", {})
|
||||
if element_info.get("constructionType") == "Text":
|
||||
text = element_info.get("singleDataInfo", {}).get("text", "")
|
||||
append_unique_tag(tags, seen, text)
|
||||
|
||||
for node in show_contents.get("10", []):
|
||||
element_info = node.get("elementInfo", {})
|
||||
if element_info.get("constructionType") != "Tag":
|
||||
continue
|
||||
for text in extract_group_tag_texts(element_info.get("groupDataInfo", [])):
|
||||
append_unique_tag(tags, seen, text)
|
||||
|
||||
for node in show_contents.get("14", []):
|
||||
element_info = node.get("elementInfo", {})
|
||||
if element_info.get("constructionType") != "Tag":
|
||||
continue
|
||||
for part in element_info.get("groupDataInfo", []):
|
||||
text = part.get("singleDataInfo", {}).get("text", "")
|
||||
append_unique_tag(tags, seen, text)
|
||||
|
||||
for node in show_contents.get("11", []):
|
||||
element_info = node.get("elementInfo", {})
|
||||
if element_info.get("constructionType") == "Text":
|
||||
text = element_info.get("singleDataInfo", {}).get("text", "")
|
||||
if text == "出清价":
|
||||
append_unique_tag(tags, seen, text)
|
||||
elif element_info.get("constructionType") == "Tag":
|
||||
for text in extract_group_tag_texts(element_info.get("groupDataInfo", [])):
|
||||
append_unique_tag(tags, seen, text)
|
||||
|
||||
label = item.get("lxInfo", {}).get("label", "")
|
||||
for part in str(label).split(","):
|
||||
part = normalize_tag_text(part)
|
||||
if part == "出清价" or "折" in part:
|
||||
append_unique_tag(tags, seen, part)
|
||||
|
||||
return "--".join(tags)
|
||||
|
||||
|
||||
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 None
|
||||
clean_str = re.sub(r"[^\d.]", "", str(price_str))
|
||||
try:
|
||||
return float(clean_str)
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
tent2 = All_Setting(host="183.6.7.91", port=33016, db="tk_test")
|
||||
|
||||
|
||||
format_today = str(datetime.date.today()).replace("-", "")
|
||||
|
||||
shop_id_list = [
|
||||
# 10000439,
|
||||
# 10000900,
|
||||
# 10000880,
|
||||
# 10000665,
|
||||
# 10000839,
|
||||
# 10000519,
|
||||
# 10001060,
|
||||
# 10000163,
|
||||
# 10000952,
|
||||
# 10000956,
|
||||
# 10001084,
|
||||
# 10001075,
|
||||
# 10001107,
|
||||
# 10001195,
|
||||
# 10001222,
|
||||
# 10001400,
|
||||
# 10001506,
|
||||
# 10001575,
|
||||
|
||||
10000745, # 长春桥店, 正常不跑
|
||||
]
|
||||
id_dict = {
|
||||
10000439: 238,
|
||||
10000900: 2101,
|
||||
10000880: 26,
|
||||
10000665: 825,
|
||||
10000839: 2048,
|
||||
10000519: 646,
|
||||
10001060: 2150,
|
||||
10000163: 1971,
|
||||
10000952: 2123,
|
||||
10000956: 2127,
|
||||
10001084: 2155,
|
||||
10001075: 2151,
|
||||
10001107: 2158,
|
||||
10001195: 2161,
|
||||
10001222: 2162,
|
||||
10001400: 2177,
|
||||
10001506: 2188,
|
||||
10001575: 2187,
|
||||
10000745: 0000,
|
||||
}
|
||||
work_id = format_today
|
||||
city_dict = {
|
||||
10000439: "上海",
|
||||
10000900: "杭州",
|
||||
10000880: "北京",
|
||||
10000665: "深圳",
|
||||
10000839: "苏州",
|
||||
10000519: "广州",
|
||||
10001060: "南京",
|
||||
10000163: "佛山",
|
||||
10000952: "东莞",
|
||||
10000956: "无锡",
|
||||
10001084: "宁波",
|
||||
10001075: "常州",
|
||||
10001107: "绍兴",
|
||||
10001195: "嘉兴",
|
||||
10001222: "南通",
|
||||
10001400: "扬州",
|
||||
10001506: "金华",
|
||||
10001575: "温州",
|
||||
}
|
||||
# work_id = '第四次测试'
|
||||
item_list = []
|
||||
export_columns = [
|
||||
"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",
|
||||
"skuBarCode",
|
||||
"json_data",
|
||||
]
|
||||
export_rows = []
|
||||
|
||||
def get_price(content):
|
||||
price = content.get("lxInfo", {}).get("sale_price", None)
|
||||
original_price = content.get("lxInfo", {}).get("origin_price", None)
|
||||
if price is None and original_price is None:
|
||||
price_list = content["showContents"]["12"]
|
||||
for i in price_list:
|
||||
if i['elementId']==72:
|
||||
groupDataInfo = i['elementInfo']["groupDataInfo"]
|
||||
for j in groupDataInfo:
|
||||
if j['constructionType'] == 'PriceText':
|
||||
price = j['singleDataInfo']["text"]
|
||||
return price, original_price
|
||||
|
||||
|
||||
def get_currentPage(price_str,da):
|
||||
if price_str == '?':
|
||||
print(da)
|
||||
print(type(da))
|
||||
da = json.loads(da)
|
||||
print(type(da['data']))
|
||||
pageInfo=da['data']["pageInfo"]
|
||||
currentPage = pageInfo['currentPage']
|
||||
return currentPage
|
||||
return None
|
||||
|
||||
|
||||
# ["showContents"]["6"][0]["elementInfo"][
|
||||
# "singleDataInfo"]["image"]["url"]
|
||||
|
||||
shop_ids_sql = ",".join(map(str, shop_id_list))
|
||||
|
||||
sql_category = f"""
|
||||
SELECT shop_id, one_id, two_id, one_name, two_name
|
||||
FROM category_detail
|
||||
WHERE work_id LIKE '%{work_id}%'
|
||||
AND shop_id IN ({shop_ids_sql})
|
||||
"""
|
||||
category_rows = tent2.get_shoppingmalls(sql_category)
|
||||
category_map = {
|
||||
(row[0], row[1], row[2]): (row[3], row[4])
|
||||
for row in category_rows
|
||||
}
|
||||
print(f"分类映射加载完成: {len(category_map)} 条")
|
||||
start_time = time.time()
|
||||
for shop_id in shop_id_list:
|
||||
sql3 = f"""
|
||||
SELECT shop_id, one_id, two_id, req_json
|
||||
FROM category_detail_log
|
||||
WHERE shop_id = '{shop_id}'
|
||||
AND work_id LIKE '{work_id}%'
|
||||
and one_id = 40485
|
||||
"""
|
||||
da123 = tent2.get_shoppingmalls(sql3)
|
||||
print(f"shop_id={shop_id}, 日志 {len(da123)} 条")
|
||||
all_clean_data = []
|
||||
for da in da123:
|
||||
log_shop_id, one_id, two_id, req_json_text = da
|
||||
|
||||
names = category_map.get((log_shop_id, one_id, two_id))
|
||||
if not names:
|
||||
print(f"缺少分类映射: shop_id={log_shop_id}, one_id={one_id}, two_id={two_id}")
|
||||
input(6666)
|
||||
continue
|
||||
|
||||
one_name, two_name = names
|
||||
try:
|
||||
data = json.loads(req_json_text)["data"]["contents"][0]["subModule"]["contents"]
|
||||
except:
|
||||
continue
|
||||
print(f"当前1分类{one_name},2分类{two_name}")
|
||||
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
|
||||
if '明日达' in ocr_results:
|
||||
print("text:明日达")
|
||||
ocr_mapping = 0
|
||||
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", None)
|
||||
# original_price = i.get("lxInfo", {}).get("origin_price", None)
|
||||
price,original_price = get_price(i)
|
||||
goods_tag = build_goods_tag(i, ocr_results)
|
||||
f_name = one_name
|
||||
f_id = one_id
|
||||
s_name = two_name
|
||||
s_id = two_id
|
||||
station_id = id_dict[shop_id]
|
||||
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 = goods_tag
|
||||
t_id = None
|
||||
t_name = None
|
||||
token = "d7e0759c-8f52-4c82-8102-774da69416d5"
|
||||
vip_price = i.get("lxInfo", {}).get("member_price", None)
|
||||
skuBarCode = i.get("logicContents", {}).get("skuBarCode", None)
|
||||
da2 = (
|
||||
work_id,
|
||||
# "第12次测试",
|
||||
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),
|
||||
skuBarCode,
|
||||
json.dumps(i),
|
||||
)
|
||||
all_clean_data.append(da2)
|
||||
export_rows.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,json_data)
|
||||
# VALUES (%s, %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)
|
||||
|
||||
end_time = time.time()
|
||||
print(f"运行时间是:{end_time - start_time}")
|
||||
if export_rows:
|
||||
import pandas as pd
|
||||
|
||||
output_path = Path(__file__).with_name(f"小象清洗结果_{work_id}.xlsx")
|
||||
pd.DataFrame(export_rows, columns=export_columns).to_excel(output_path, index=False)
|
||||
print(f"Excel导出完成: {output_path}, 共 {len(export_rows)} 条")
|
||||
else:
|
||||
print("没有可导出的清洗数据")
|
||||
# for i in item_list:
|
||||
# print(i)
|
||||
# for i in list(set(item_list)):
|
||||
# print(i)
|
||||
Reference in New Issue
Block a user