Bring Amazon collection tasks into dev/koko

Merge origin/master into dev/koko while preserving the patrol delete task added on dev/koko. The task dispatcher now recognizes both patrol deletion and appearance patent collection, and the master-side SpiderTask implementation is included.

Constraint: origin fetch and push over SSH are blocked in this environment by Gitee publickey authentication

Rejected: Overwrite dev/koko with master | would drop the patrol delete branch work

Confidence: medium

Scope-risk: moderate

Directive: Do not remove either patrol-delete-run or appearance-patent-run without checking queued task producers

Tested: python -m py_compile app/amazon/approve.py app/amazon/asin_status.py app/amazon/detail_spider.py app/amazon/main.py app/amazon/match_action.py app/amazon/price_match.py

Not-tested: Remote push, blocked by SSH publickey authentication
This commit is contained in:
koko
2026-04-27 17:36:30 +08:00
12 changed files with 7139 additions and 121 deletions

Binary file not shown.

View File

@@ -483,8 +483,8 @@ class AmzoneApprove(AmamzonBase):
load_ele = self.tab.eles("xpath://div[contains(@class,'Loader-module__loader')]",timeout=5) load_ele = self.tab.eles("xpath://div[contains(@class,'Loader-module__loader')]",timeout=5)
if len(load_ele) > 0: if len(load_ele) > 0:
load_ele[0].wait.deleted(timeout=3, raise_err=False) load_ele[0].wait.deleted(timeout=3, raise_err=False)
time.sleep(0.5)
time.sleep(0.5)
# 获取当前页码 # 获取当前页码
try: try:
page_pamel = self.tab.eles('xpath://kat-pagination',timeout=5) page_pamel = self.tab.eles('xpath://kat-pagination',timeout=5)
@@ -1065,6 +1065,8 @@ class ApproveTask:
# 更新已处理国家数 # 更新已处理国家数
if task_id in runing_task: if task_id in runing_task:
runing_task[task_id]["processed_countries"] += 1 runing_task[task_id]["processed_countries"] += 1
self.log(f"{task_id}任务处理完成")
# 最后回传,标记完成 # 最后回传,标记完成
try: try:
self.post_result(task_id, shop_name, country_code, "", "", is_done=True) self.post_result(task_id, shop_name, country_code, "", "", is_done=True)

View File

@@ -432,6 +432,8 @@ class StatusTask:
# 关闭店铺 # 关闭店铺
driver.close_store() driver.close_store()
self.log(f"{task_id}任务处理完成")
# 最后回传,标记完成 # 最后回传,标记完成
try: try:
# self.post_result(task_id, shop_name, country_code, "", "", is_done=True) # self.post_result(task_id, shop_name, country_code, "", "", is_done=True)

6907
app/amazon/detail_spider.py Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -11,6 +11,7 @@ from amazon.match_action import MatchTak
from amazon.price_match import PriceTask from amazon.price_match import PriceTask
from amazon.asin_status import StatusTask from amazon.asin_status import StatusTask
from amazon.product import ProductTask from amazon.product import ProductTask
from amazon.detail_spider import SpiderTask
from amazon.tool import get_shop_info,show_notification from amazon.tool import get_shop_info,show_notification
@@ -59,7 +60,8 @@ class TaskMonitor:
"shop-match-run" : "匹配价格", "shop-match-run" : "匹配价格",
"price-track-run" : "跟价", "price-track-run" : "跟价",
"query-asin-run" : "状态查询", "query-asin-run" : "状态查询",
"patrol-delete-run" : "巡店删除" "patrol-delete-run" : "巡店删除",
"appearance-patent-run" : "亚马逊采集",
} }
try: try:
while self.running: while self.running:
@@ -128,7 +130,8 @@ class TaskMonitor:
"匹配价格" : MatchTak, "匹配价格" : MatchTak,
"跟价" : PriceTask, "跟价" : PriceTask,
"状态查询" : StatusTask, "状态查询" : StatusTask,
"巡店删除" : ProductTask "巡店删除" : ProductTask,
"亚马逊采集" : SpiderTask,
} }
self.log(f"线程 {id(task_data)} 开始处理产品风险审批任务...") self.log(f"线程 {id(task_data)} 开始处理产品风险审批任务...")
# 创建ApproveTask实例并处理任务 # 创建ApproveTask实例并处理任务

View File

@@ -451,7 +451,7 @@ class MatchTak:
# 更新已处理国家数 # 更新已处理国家数
if task_id in runing_task: if task_id in runing_task:
runing_task[task_id]["processed_countries"] += 1 runing_task[task_id]["processed_countries"] += 1
self.log(f"{task_id}任务处理完成")
# 最后回传,标记完成 # 最后回传,标记完成
try: try:
# self.post_result(task_id, shop_name, country_code, "", "", is_done=True) # self.post_result(task_id, shop_name, country_code, "", "", is_done=True)

View File

@@ -17,6 +17,97 @@ from amazon.tool import show_notification,get_shop_info,remove_special_character
from config import runing_task, runing_shop,base_dir,DELETE_BRAND_API_BASE from config import runing_task, runing_shop,base_dir,DELETE_BRAND_API_BASE
class RepricingLogic:
@staticmethod
def situation_1_general_decrease(my_price, rank1_price, is_first_time=False):
"""
情况1常规递减跟价 (最左侧独立长图块)
当不是自己购物车时,按总价当作第一名递减。
"""
if is_first_time:
return rank1_price - 0.3
diff = abs(my_price - rank1_price)
# 精简了原图中长串的阶梯逻辑
if diff <= 0.3:
return rank1_price - 0.5
elif diff <= 0.8:
return rank1_price - 0.7
elif diff <= 10.5:
# 0.8 到 10.5 之间原图逻辑全部是“减1”
return rank1_price - 1.0
else:
return None # 相差10.5以上,跳过
@staticmethod
def situation_2_own_buybox(my_price, rank2_price):
"""
情况2已经是自己购物车
逻辑:判断与第二名的差价,如果拉开足够差距,则稍微降一点点(减0.5)保持优势,防止卖太便宜。
"""
diff = abs(rank2_price - my_price)
# 1. 绝对差价2欧以上
if diff >= 2.0:
return rank2_price - 0.3
# 2. 产品售价区间判定 (是否要提高价格)
if (20 <= my_price < 30 and diff >= 4) or \
(30 <= my_price < 60 and diff >= 8) or \
(60 <= my_price <= 150 and diff >= 15):
return rank2_price - 0.3 # 满足区间大差价,提价至第二名之下
return my_price # 不满足条件则保持原价
@staticmethod
def situation_3_not_own_buybox(my_price, rank1_price):
"""
情况3不是自己购物车 (常规情况)
"""
diff = abs(my_price - rank1_price)
if diff <= 0.3:
return rank1_price - 0.5
elif 0.5 <= diff <= 0.8:
return rank1_price - 0.7
elif 0.8 < diff <= 2.5:
return rank1_price - 1.0
elif diff > 2.5: # 2.5-3.5以上跳过
return None
return rank1_price - 0.3 # 默认按第一名减0.3
@staticmethod
def situation_4_encounter_amz_us_1st(my_price, amz_us_price):
"""
情况4第一名是 Amazon US 卖家
"""
diff = abs(my_price - amz_us_price)
if diff <= 3:
return amz_us_price - 2
elif 4 <= diff <= 6:
return amz_us_price - 3
elif 8 <= diff <= 12:
return None # 跳过不跟价
return amz_us_price - 3 # 默认直接按照 Amazon US 减去3
@staticmethod
def situation_5_im_1st_amz_us_2nd(my_price, amz_us_price):
"""
情况5自己是第一名第二名是 Amazon US 卖家
"""
diff = abs(amz_us_price - my_price)
if diff <= 7:
return None # 相差5以内跳过 (保持原价)
else:
return amz_us_price - 5
def calculate_target_price( def calculate_target_price(
front_end_data,current_Price,current_shop_name, front_end_data,current_Price,current_shop_name,
recommended_price,recommended_shipping recommended_price,recommended_shipping
@@ -35,65 +126,44 @@ def calculate_target_price(
# --- Step 2: 紫鸟后台特殊判定 (最高优先级) --- # --- Step 2: 紫鸟后台特殊判定 (最高优先级) ---
if recommended_shipping > 0: if recommended_shipping > 0:
backend_base_price = recommended_price price_1 = float(front_end_data.get("top_sellers")[0].get("price")) # 实际第一名价格
backend_shipping = recommended_shipping return RepricingLogic.situation_1_general_decrease(my_price,price_1,is_my_buybox)
total_price = backend_base_price + backend_shipping # backend_base_price = recommended_price
# backend_shipping = recommended_shipping
if is_my_buybox: # total_price = backend_base_price + backend_shipping
# 🌟【逻辑更新】:如果是自己的购物车,直接跳过,不做任何价格调整 #
return None # if is_my_buybox:
else: # # 🌟【逻辑更新】:如果是自己的购物车,直接跳过,不做任何价格调整
# 不是自己的购物车:直接将紫鸟的总和作为"第一名"价格,强制抛入阶梯跟价逻辑 # return None
return calculate_standard_competitor_pricing(my_price, total_price) # else:
# # 不是自己的购物车:直接将紫鸟的总和作为"第一名"价格,强制抛入阶梯跟价逻辑
# return calculate_standard_competitor_pricing(my_price, total_price)
# # 第一名是自己, 第二名 Amazon US 卖家 的情况 # # 第一名是自己, 第二名 Amazon US 卖家 的情况
shop_name_1 = front_end_data.get("top_sellers")[0].get("shop_name") shop_name_1 = front_end_data.get("top_sellers")[0].get("shop_name")
shop_name_2 = front_end_data.get("top_sellers")[1].get("shop_name") if len(front_end_data.get("top_sellers")) >=2 else "" shop_name_2 = front_end_data.get("top_sellers")[1].get("shop_name") if len(front_end_data.get("top_sellers")) >=2 else ""
if shop_name_1 == current_shop_name and "Amazon" in shop_name_2: if shop_name_1 == current_shop_name and "Amazon" in shop_name_2:
price_2 = float(front_end_data.get("top_sellers")[1].get("price")) # 实际第二名价格 price_2 = float(front_end_data.get("top_sellers")[1].get("price")) # 实际第二名价格
diff = abs(my_price - price_2) return RepricingLogic.situation_5_im_1st_amz_us_2nd(my_price,price_2)
if diff < 5:
return price_2 -3
elif 5.0 < diff <= 8.0:
return price_2 -3
# --- Step 3: 常规核心分流逻辑 (如果没有紫鸟后台数据) --- # --- Step 3: 常规核心分流逻辑 (如果没有紫鸟后台数据) ---
# 分支 A第一名是 Amazon US 卖家 (特殊强敌优先) # 分支 A第一名是 Amazon US 卖家 (特殊强敌优先)
price_1 = float(front_end_data.get("top_sellers")[0].get("price")) # 实际第一名价格 price_1 = float(front_end_data.get("top_sellers")[0].get("price")) # 实际第一名价格
if "Amazon." in cart_seller: if "Amazon" in shop_name_1:
diff = abs(my_price - price_1) return RepricingLogic.isituation_4_encounter_amz_us_1st(my_price,price_1)
if diff <= 5.0 :
return None
elif 5.0 < diff <= 8.0:
return price_1 - 3.0
elif diff > 8.0:
return price_1 - 6 # 跳过,不跟价
# 分支 B不是 Amazon US且目前是自己的购物车 # 分支 B不是 Amazon US且目前是自己的购物车
elif is_my_buybox: elif is_my_buybox:
price_2 = float(front_end_data.get("top_sellers")[1].get("price")) # 实际第二名价格 price_2 = float(front_end_data.get("top_sellers")[1].get("price")) # 实际第二名价格
diff_with_2nd = price_2 - my_price return RepricingLogic.situation_2_own_buybox(my_price,price_2)
if diff_with_2nd >= 2.0:
return price_2 - 0.3
else:
# 提价区间判定
if (20 <= my_price <= 30) and diff_with_2nd >= 4:
return price_2 - 0.3
elif (30 <= my_price <= 60) and diff_with_2nd >= 8:
return price_2 - 0.3
elif (60 <= my_price <= 150) and diff_with_2nd >= 15:
return price_2 - 0.3
else:
return None
# 分支 C不是 Amazon US也不是自己的购物车 # 分支 C不是 Amazon US也不是自己的购物车
else: else:
# 正常情况下的普通跟价,调用阶梯逻辑 # 正常情况下的普通跟价,调用阶梯逻辑
return calculate_standard_competitor_pricing(my_price, price_1) return RepricingLogic.situation_3_not_own_buybox(my_price, price_1)
# ===================================================================== # =====================================================================
@@ -107,7 +177,7 @@ def calculate_standard_competitor_pricing(my_current_price, target_competitor_pr
diff = abs(my_current_price - target_competitor_price) diff = abs(my_current_price - target_competitor_price)
# 根据阶梯执行跟价扣减 # 根据阶梯执行跟价扣减
if diff <= 0.3: if 0 < diff < 0.5:
return base_target - 0.5 return base_target - 0.5
elif 0.5 <= diff <= 0.8: elif 0.5 <= diff <= 0.8:
return base_target - 0.7 return base_target - 0.7
@@ -117,12 +187,13 @@ def calculate_standard_competitor_pricing(my_current_price, target_competitor_pr
return base_target - 1.0 return base_target - 1.0
elif 2.5 < diff <= 3 : elif 2.5 < diff <= 3 :
return None # 差距过大,跳过不跟价 return None # 差距过大,跳过不跟价
elif diff > 3: # elif diff > 3:
# return base_target
else:
return base_target return base_target
class ChromeAmzone: class ChromeAmzone:
mark_name = "亚马逊详情采集" mark_name = "亚马逊详情采集"
@@ -531,8 +602,8 @@ class AmzonePriceMatch(AmamzonBase):
current_country_ele = self.tab.ele('xpath://div[@class="dropdown-account-switcher-header-label"]/span[last()]', current_country_ele = self.tab.ele('xpath://div[@class="dropdown-account-switcher-header-label"]/span[last()]',
timeout=20) timeout=20)
current_country = current_country_ele.text.strip() current_country = current_country_ele.text.strip()
max_retry_num = 5
while retry_num < 3: # 最多重试3次 while retry_num < max_retry_num: # 最多重试3次
try: try:
if appoint_asin is not None: if appoint_asin is not None:
print(self.mark_name,"指定asin操作",appoint_asin) print(self.mark_name,"指定asin操作",appoint_asin)
@@ -548,7 +619,7 @@ class AmzonePriceMatch(AmamzonBase):
page_pamel = self.tab.eles('xpath://kat-pagination', timeout=5) page_pamel = self.tab.eles('xpath://kat-pagination', timeout=5)
if len(page_pamel) > 0: if len(page_pamel) > 0:
current_page = page_pamel[0].sr('xpath:.//ul[@class="pages"]//li[@aria-current="true"]').text current_page = page_pamel[0].sr('xpath:.//ul[@class="pages"]//li[@aria-current="true"]').text
# yield (None,{"page":int(current_page.strip())})
# 总页数 # 总页数
total_page = page_pamel[0].sr.eles( total_page = page_pamel[0].sr.eles(
'xpath:.//ul[@class="pages"]//span[@class="page__inner"][last()]') 'xpath:.//ul[@class="pages"]//span[@class="page__inner"][last()]')
@@ -567,6 +638,14 @@ class AmzonePriceMatch(AmamzonBase):
sku_ls = self.tab.eles("xpath://div[@data-sku]", timeout=10) sku_ls = self.tab.eles("xpath://div[@data-sku]", timeout=10)
print(f"{self.mark_name}】获取到 {len(sku_ls)}") print(f"{self.mark_name}】获取到 {len(sku_ls)}")
if len(sku_ls) == 0:
retry_num += 1
print(f"没有获取到 ASIN 商品,重试{retry_num}/{max_retry_num}")
self.tab.refresh()
self.tab.wait.doc_loaded(raise_err=False, timeout=120)
continue
# for sku_ele in sku_ls[0:2]: # for sku_ele in sku_ls[0:2]:
for sku_ele in sku_ls: for sku_ele in sku_ls:
@@ -636,6 +715,7 @@ class AmzonePriceMatch(AmamzonBase):
chrome = ChromeAmzone(tab=new_tab) chrome = ChromeAmzone(tab=new_tab)
front_end_data = chrome.run(country=current_country) front_end_data = chrome.run(country=current_country)
chrome.close() chrome.close()
self.clear_tab()
print(self.mark_name,"亚马逊前台抓取到数据",front_end_data) print(self.mark_name,"亚马逊前台抓取到数据",front_end_data)
if front_end_data is None or len(front_end_data.get("top_sellers")) == 0: if front_end_data is None or len(front_end_data.get("top_sellers")) == 0:
yield (asin, { yield (asin, {
@@ -698,8 +778,28 @@ class AmzonePriceMatch(AmamzonBase):
"firstPlace": price_1, "firstPlace": price_1,
"secondPlace": price_2, "secondPlace": price_2,
"cartShopName" : cartShopName, "cartShopName" : cartShopName,
"shippingFee" : f"{shipping_fee}",
"priceChangeStatus" : "改价成功", "priceChangeStatus" : "改价成功",
}) })
else:
recommendedPrice = recommend_price + shipping_fee
price_1 = float(front_end_data.get("top_sellers")[0].get("price")) if len(
front_end_data.get("top_sellers")) >= 2 else "" # 实际第一名价格
price_2 = float(front_end_data.get("top_sellers")[1].get("price")) if len(
front_end_data.get("top_sellers")) >= 2 else "" # 实际第二名价格
cartShopName = front_end_data.get("cart_seller")
yield (asin, {
"statu": "跳过,无需改价",
"currentPrice": current_price,
"recommendedPrice": f"{recommendedPrice}",
"minimumPrice": bottom_price, # 最低价格
"firstPlace": price_1,
"secondPlace": price_2,
"cartShopName": cartShopName,
"shippingFee": f"{shipping_fee}",
"priceChangeStatus": "跳过,无需改价",
})
already_asin.add(asin) already_asin.add(asin)
# 判断是否存在需要翻页的情况 # 判断是否存在需要翻页的情况
@@ -714,6 +814,7 @@ class AmzonePriceMatch(AmamzonBase):
num += 1 num += 1
print(f"{self.mark_name}】【程序计算】正在翻页,已翻 {num} 页...") print(f"{self.mark_name}】【程序计算】正在翻页,已翻 {num} 页...")
already_asin = set() already_asin = set()
retry_num = 0
except Exception as e: except Exception as e:
print(f"{self.mark_name}】处理跟价操作异常", e) print(f"{self.mark_name}】处理跟价操作异常", e)
@@ -1006,6 +1107,7 @@ class PriceTask:
self.post_stage_finished(task_id, user_id, stage_index) self.post_stage_finished(task_id, user_id, stage_index)
except Exception as e: except Exception as e:
self.log(f"回传结果失败: {str(e)}", "ERROR") self.log(f"回传结果失败: {str(e)}", "ERROR")
self.log(f"{task_id}任务处理完成")
# 从正在执行中的店铺列表中移除 # 从正在执行中的店铺列表中移除
if shop_name in runing_shop: if shop_name in runing_shop:
@@ -1136,12 +1238,14 @@ class PriceTask:
else: else:
_shopMallName = shopMallName _shopMallName = shopMallName
ap_asin = None ap_asin = None
for _ in range(max_retries):
for asin, status in driver.run_page_action( for asin, status in driver.run_page_action(
current_shop_name=_shopMallName, current_shop_name=_shopMallName,
appoint_asin=ap_asin,skip_asin=skip_asin,mode=mode, appoint_asin=ap_asin,skip_asin=skip_asin,mode=mode,
miniprice_info=miniprice_info miniprice_info=miniprice_info
): ):
if asin is None:
continue
# 检查是否收到暂停请求 # 检查是否收到暂停请求
if task_id in runing_task and runing_task[task_id].get("stop_requested", False): if task_id in runing_task and runing_task[task_id].get("stop_requested", False):
self.log(f"检测到任务 {task_id} 的暂停请求停止处理ASIN", "WARNING") self.log(f"检测到任务 {task_id} 的暂停请求停止处理ASIN", "WARNING")
@@ -1162,6 +1266,7 @@ class PriceTask:
except Exception as e: except Exception as e:
self.log(f"回传结果失败: {str(e)}", "ERROR") self.log(f"回传结果失败: {str(e)}", "ERROR")
break
self.log(f"国家 {country_name} 处理完成") self.log(f"国家 {country_name} 处理完成")
@@ -1216,18 +1321,6 @@ class PriceTask:
""" """
url = f"{DELETE_BRAND_API_BASE}/api/price-track/tasks/{task_id}/result" url = f"{DELETE_BRAND_API_BASE}/api/price-track/tasks/{task_id}/result"
"""
(asin,{
"statu" : "改价成功",
"currentPrice" : current_price,
"recommendedPrice" : f"{recommendedPrice}",
"minimumPrice" : bottom_price, #最低价格
"firstPlace": price_1,
"secondPlace": price_2,
"cartShopName" : current_shop_name,
"priceChangeStatus" : "改价成功",
})
"""
payload = { payload = {
"shops": [ "shops": [
@@ -1248,6 +1341,7 @@ class PriceTask:
"deleteSkipAsin": status.get("deleteSkipAsin",False), "deleteSkipAsin": status.get("deleteSkipAsin",False),
"removeAsin": status.get("removeAsin") if status.get("removeAsin") else "", "removeAsin": status.get("removeAsin") if status.get("removeAsin") else "",
# "modifyCount": "2", # "modifyCount": "2",
"shippingFee" : status.get("shippingFee") if status.get("shippingFee") else "",
"status": status.get("statu") "status": status.get("statu")
} }
] ]
@@ -1296,34 +1390,44 @@ class PriceTask:
if __name__ == '__main__': if __name__ == '__main__':
# 使用示例 # 使用示例
user_info = { # user_info = {
"company": "尾号5578的公司115", # "company": "尾号5578的公司115",
"username": "自动化_Robot", # "username": "自动化_Robot",
"password": "#20zsg25" # "password": "#20zsg25"
} # }
shop_name = "林建华" # shop_name = "刘建煌"
country = "德国" # country = "德国"
kill_process('v6') # kill_process('v6')
driver = AmzonePriceMatch(user_info) # driver = AmzonePriceMatch(user_info)
browser = driver.open_shop(shop_name) # browser = driver.open_shop(shop_name)
sw_suc = driver.SwitchingCountries(country) # sw_suc = driver.SwitchingCountries(country)
driver.SwitchPage() # driver.SwitchPage()
risk_listing_filter = "Active" # risk_listing_filter = "Active"
_shopMallName = "WEIZHENFENG168" # _shopMallName = "Jianhuang 888"
ap_asin ="B0FHGQY18W" # ap_asin ="B0BGHRP6BS"
skip_asin = [] # skip_asin = []
for _ in range(3): # for _ in range(3):
try: # try:
sku_ls = driver.search(filter_type=risk_listing_filter) # sku_ls = driver.search(filter_type=risk_listing_filter)
break # break
except Exception as e: # except Exception as e:
print(e) # print(e)
driver.tab.refresh() # driver.tab.refresh()
if len(sku_ls) > 0: # if len(sku_ls) > 0:
print("有数据,开始操作") # print("有数据,开始操作")
for asin, status in driver.run_page_action( # for asin, status in driver.run_page_action(
current_shop_name=_shopMallName, # current_shop_name=_shopMallName,
appoint_asin=ap_asin,skip_asin=skip_asin,mode="asin" # appoint_asin=ap_asin,skip_asin=skip_asin,mode="asin"
): # ):
print(f"ASIN {asin} 的处理结果: {status}") # print(f"ASIN {asin} 的处理结果: {status}")
print("已完成操作") # print("已完成操作")
front_end_data = {"top_sellers": [{"rank": 1, "price": "36.81", "stock": "5", "shop_name": "Windera"}, {"rank": 2, "price": "36.50", "stock": "100", "shop_name": "Jianhuang 888"}], "cart_seller": "Windera", "timestamp": "2026-04-25 09:32:58"}
current_Price = 36.50
current_shop_name = "Jianhuang 888"
recommended_price = 36.81
recommended_shipping = 0
calculate_target_price(
front_end_data, current_Price, current_shop_name,
recommended_price, recommended_shipping
)