style: 统一代码格式并改进异常处理

- 将多行字符串调整为符合PEP 8的格式
- 统一使用显式的异常捕获(except Exception)
- 改进线程的daemon设置方式
- 移除未使用的异常变量和导入
- 修复文件末尾的换行符问题
This commit is contained in:
2026-02-10 16:57:31 +08:00
parent 92619edcdb
commit bccffc006a
11 changed files with 71 additions and 36 deletions
Binary file not shown.
Binary file not shown.
+14 -6
View File
@@ -5,13 +5,17 @@ import json
import requests
import concurrent.futures
from datetime import datetime
from tkinter import messagebox
from DrissionPage import ChromiumPage, ChromiumOptions
from src.downloader import download_file
def run_bilibili_task(
target_url, target_count, save_root, browser_path, log_callback, finish_callback
target_url,
target_count,
save_root,
browser_path,
log_callback,
finish_callback=None,
):
"""
[Control Layer] B站核心业务流程
@@ -85,7 +89,7 @@ def run_bilibili_task(
next_btn.click()
no_new_data_count = 0
time.sleep(2)
except:
except Exception:
pass
else:
no_new_data_count = 0
@@ -146,7 +150,7 @@ def run_bilibili_task(
if dp:
try:
dp.close()
except:
except Exception:
pass
finally:
# 这里需要一种机制通知UI线程结束,或者由UI层处理
@@ -218,7 +222,11 @@ def get_bilibili_play_url(bvid):
"""
url = f"https://www.bilibili.com/video/{bvid}"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/120.0.0.0 Safari/537.36"
),
"Referer": "https://www.bilibili.com/",
}
try:
@@ -242,4 +250,4 @@ def get_bilibili_play_url(bvid):
return video_url, audio_url
except Exception:
pass
return None, None
return None, None
+29 -11
View File
@@ -5,7 +5,15 @@ from datetime import datetime
from DrissionPage import ChromiumPage, ChromiumOptions
from src.downloader import download_file
def run_douyin_task(target_url, target_count, save_root, browser_path, log_callback, finish_callback):
def run_douyin_task(
target_url,
target_count,
save_root,
browser_path,
log_callback,
finish_callback,
):
"""
[Control Layer] 抖音核心业务流程
1. 启动浏览器
@@ -53,7 +61,7 @@ def run_douyin_task(target_url, target_count, save_root, browser_path, log_callb
):
collected_works.append(aweme)
found_new = True
except:
except Exception:
pass
log_callback(f"已获取作品信息: {len(collected_works)}/{target_count}")
@@ -99,7 +107,11 @@ def run_douyin_task(target_url, target_count, save_root, browser_path, log_callb
file_name_base = f"{date_str}({count_idx})"
download_tasks.append(
{"work": work, "index": index, "file_name_base": file_name_base}
{
"work": work,
"index": index,
"file_name_base": file_name_base,
}
)
# 使用线程池执行下载
@@ -115,7 +127,7 @@ def run_douyin_task(target_url, target_count, save_root, browser_path, log_callb
len(works_to_process),
save_root,
task["file_name_base"],
log_callback
log_callback,
)
)
@@ -133,12 +145,15 @@ def run_douyin_task(target_url, target_count, save_root, browser_path, log_callb
if dp:
try:
dp.close()
except:
except Exception:
pass
finally:
pass
def process_douyin_work(work, index, total_count, save_root, file_name_base, log_callback):
def process_douyin_work(
work, index, total_count, save_root, file_name_base, log_callback
):
"""
[Data Layer] 单个任务处理逻辑 (Worker)
判断作品类型(视频/图文),生成路径并调用下载器
@@ -148,9 +163,11 @@ def process_douyin_work(work, index, total_count, save_root, file_name_base, log
if "images" in work and work["images"]:
is_video = False
log_callback(
f"[{index + 1}/{total_count}] {file_name_base} | {'视频' if is_video else '图文'} | 下载中..."
msg = (
f"[{index + 1}/{total_count}] {file_name_base} | "
f"{'视频' if is_video else '图文'} | 下载中..."
)
log_callback(msg)
if is_video:
video_url = work["video"]["play_addr"]["url_list"][0]
@@ -158,7 +175,7 @@ def process_douyin_work(work, index, total_count, save_root, file_name_base, log
if not os.path.exists(file_path):
if download_file(video_url, file_path, log_callback=log_callback):
log_callback(
f"[{index + 1}/{total_count}] {file_name_base} -> 下载完成"
f"[{index + 1}/{total_count}] {file_name_base} " "-> 下载完成"
)
else:
log_callback(
@@ -166,7 +183,8 @@ def process_douyin_work(work, index, total_count, save_root, file_name_base, log
)
else:
log_callback(
f"[{index + 1}/{total_count}] {file_name_base} -> 文件已存在,跳过"
f"[{index + 1}/{total_count}] {file_name_base} "
"-> 文件已存在,跳过"
)
else:
img_folder = os.path.join(save_root, file_name_base)
@@ -185,4 +203,4 @@ def process_douyin_work(work, index, total_count, save_root, file_name_base, log
)
except Exception as e:
log_callback(f"[{index + 1}/{total_count}] {file_name_base} -> 处理出错: {e}")
log_callback(f"[{index + 1}/{total_count}] {file_name_base} -> 处理出错: {e}")