Commit 8d390169 by baiquan

优化上传图片与视频,添加关闭所有hub浏览器方法

parent 128bb4f2
......@@ -123,6 +123,22 @@ async def checkBrowserStatus(browser_id: str = ""):
return await sendRequest("POST", url=url, json_data=data)
async def closeAllBrowser():
"""
关闭所有浏览器
:return:
"""
url = '/api/v1/browser/stop-all'
data = {
"clearOpening": True
}
try:
return await sendRequest("POST", url=url, json_data=data)
except Exception as e:
logger.error(f"关闭所有浏览器异常-->{e}")
if __name__ == '__main__':
# data = asyncio.run(envList({"containerName": "希音"}))
data = asyncio.run(openExe())
......
......@@ -139,16 +139,14 @@ async def uploadImageAndVideo(task: dict = None):
# 准备SKU图片上传
sku_image_list = []
for sku in skus:
for key, value in sku.items():
sku_id = sku.get('skuid')
if isinstance(value, dict):
img_url = value.get('image')
if img_url:
# md5_key = hashlib.md5(img_url.encode()).hexdigest()
local_path = get_local_path(item_id, img_url)
check_image_size(local_path)
local_path = convert_rect_to_square(local_path)
sku_image_list.append({sku_id: local_path})
sku_id = sku.get('sku_id')
img_url = sku.get('image')
if img_url:
# md5_key = hashlib.md5(img_url.encode()).hexdigest()
local_path = get_local_path(item_id, img_url)
check_image_size(local_path)
local_path = convert_rect_to_square(local_path)
sku_image_list.append({sku_id: local_path})
# 准备主图上传
image_list = []
......@@ -169,12 +167,13 @@ async def uploadImageAndVideo(task: dict = None):
description_list.append({md5_key: local_path})
try:
# 并行处理所有上传任务
sku_image_dict, image_dict, description_dict, video_dict = await asyncio.gather(
sku_image_dict, image_dict, description_dict = await asyncio.gather(
run_in_executor(upload_image_by_bytes, cookies, headers, proxies, sku_image_list),
run_in_executor(upload_image_by_bytes, cookies, headers, proxies, image_list),
run_in_executor(upload_image_by_bytes, cookies, headers, proxies, description_list),
upload_videos(task, item_id)
)
image_list = list(image_dict.values())
video_dict = await upload_videos(task, item_id, image_list)
except Exception as e:
logger.error(f"上传过程中发生错误: {str(e)}")
return None
......@@ -196,7 +195,7 @@ async def run_in_executor(func, *args):
return await loop.run_in_executor(None, func, *args)
async def upload_videos(task: dict, item_id: str):
async def upload_videos(task: dict, item_id: str, customized_images: list):
"""异步上传所有视频"""
video_dict = {}
video_tasks = []
......@@ -204,9 +203,12 @@ async def upload_videos(task: dict, item_id: str):
for video_url in task.get('video_list', []):
logger.info(f"开始处理视频:{video_url}")
local_video_url = get_local_path(item_id, video_url)
video_task = upload_single_video(task.copy(), local_video_url, video_url)
video_task = upload_single_video(task.copy(), local_video_url, video_url, customized_images)
video_tasks.append(video_task)
# 并行执行所有视频上传任务
if not task.get('video_list', []):
video_task = upload_single_video(task.copy(), "", "", customized_images)
video_tasks.append(video_task)
results = await asyncio.gather(*video_tasks)
# 合并结果
......@@ -217,11 +219,19 @@ async def upload_videos(task: dict, item_id: str):
return video_dict
async def upload_single_video(task: dict, local_path: any, original_url: str):
async def upload_single_video(task: dict, local_path: any, original_url: str, customized_images: list):
"""上传单个视频"""
task['file_path'] = local_path
task['video_url'] = original_url
md5_key = hashlib.md5(original_url.encode()).hexdigest()
product_name = task['title'].encode('utf-8').decode('unicode_escape')
task['generate_video_data'] = {
"product_name": product_name,
"customized_images": customized_images
}
if original_url:
md5_key = hashlib.md5(original_url.encode()).hexdigest()
else:
md5_key = "generated_video"
try:
result = await run_in_executor(upload_video_with_multithreading, task)
......
......@@ -684,11 +684,12 @@ def prepare_video_file(task):
if not task.get("file_path"):
if not os.path.exists(VIDEO_PATH):
os.makedirs(VIDEO_PATH)
file_name = f"{task['file_name']}"
if not task.get("file_name"):
raise VideoError("file_name is None")
file_name = task['file_name']
file_path = os.path.join(VIDEO_PATH, file_name)
else:
file_path = task.get("file_path")
if not os.path.exists(file_path):
logger.info(f"文件 {file_path} 不存在,开始下载")
download_video(task['video_url'], file_path, headers=task['headers'])
......
......@@ -216,12 +216,13 @@ async def get_task(params: dict = None):
获取任务
"""
publish_task = requests.get(f"{settings.DOMAIN}/api/collection/task/getPublishTask", headers=DEFAULT_HEADER).json()
# collection_task = requests.get(f'{settings.DOMAIN}/api/collection/task', headers=DEFAULT_HEADER, params=params).json()
# if collection_task.get('data', []):
# collection_task['data'].extend(publish_task_data)
# return collection_task
# else:
return publish_task
collection_task = requests.get(f'{settings.DOMAIN}/api/collection/task', headers=DEFAULT_HEADER, params=params).json()
publish_task_data = publish_task.get('data', [])
if publish_task_data:
collection_task['data'].extend(publish_task_data)
return collection_task
else:
return publish_task
async def callback_task(data: dict, task_type:int):
"""
......@@ -233,7 +234,7 @@ async def callback_task(data: dict, task_type:int):
if task_type == 1:
return requests.post(f"{settings.DOMAIN}/api/collection/task", json=data, headers=DEFAULT_HEADER)
elif task_type == 2:
return requests.post(f"{settings.DOMAIN}/api/collection/task/callBackPublishTask", json=data)
return requests.post(f"{settings.DOMAIN}/api/collection/task/callBackPublishTask", json=data, headers=DEFAULT_HEADER)
else:
raise Exception("task_type error")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment