diff --git a/src/__pycache__/downloader.cpython-310.pyc b/src/__pycache__/downloader.cpython-310.pyc index 60e4d65..46d2f79 100644 Binary files a/src/__pycache__/downloader.cpython-310.pyc and b/src/__pycache__/downloader.cpython-310.pyc differ diff --git a/src/__pycache__/utils.cpython-310.pyc b/src/__pycache__/utils.cpython-310.pyc index 6c1dd4e..51a7c56 100644 Binary files a/src/__pycache__/utils.cpython-310.pyc and b/src/__pycache__/utils.cpython-310.pyc differ diff --git a/src/downloader.py b/src/downloader.py index 71ee356..e60207b 100644 --- a/src/downloader.py +++ b/src/downloader.py @@ -1,6 +1,7 @@ import requests import time + def download_file(url, filepath, referer=None, log_callback=None): """ [Data Layer] 文件下载执行器 @@ -9,7 +10,11 @@ def download_file(url, filepath, referer=None, log_callback=None): try: # 根据不同平台可能需要调整 Headers 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 @@ -17,20 +22,14 @@ def download_file(url, filepath, referer=None, log_callback=None): headers["Referer"] = referer else: # 简单的 Referer 区分 (保留旧逻辑作为后备) - if ( - "bilibili.com" in url - or "hdslb.com" in url - or "bilivideo.com" in url - ): + if "bilibili.com" in url or "hdslb.com" in url or "bilivideo.com" in url: headers["Referer"] = "https://www.bilibili.com/" else: headers["Referer"] = "https://www.douyin.com/" for i in range(3): try: - response = requests.get( - url, headers=headers, stream=True, timeout=20 - ) + response = requests.get(url, headers=headers, stream=True, timeout=20) if response.status_code == 200: with open(filepath, "wb") as f: for chunk in response.iter_content(chunk_size=1024 * 1024): @@ -41,7 +40,8 @@ def download_file(url, filepath, referer=None, log_callback=None): if i == 2: if log_callback: log_callback( - f"下载请求失败: Status {response.status_code} | URL: {url[:30]}..." + f"下载请求失败: Status {response.status_code} " + f"| URL: {url[:30]}..." ) except requests.exceptions.RequestException as e: if i == 2: @@ -53,4 +53,4 @@ def download_file(url, filepath, referer=None, log_callback=None): except Exception as e: if log_callback: log_callback(f"下载出错: {e}") - return False \ No newline at end of file + return False diff --git a/src/tasks/__pycache__/bilibili.cpython-310.pyc b/src/tasks/__pycache__/bilibili.cpython-310.pyc index 681eed8..3491c79 100644 Binary files a/src/tasks/__pycache__/bilibili.cpython-310.pyc and b/src/tasks/__pycache__/bilibili.cpython-310.pyc differ diff --git a/src/tasks/__pycache__/douyin.cpython-310.pyc b/src/tasks/__pycache__/douyin.cpython-310.pyc index eb7a7b5..e363497 100644 Binary files a/src/tasks/__pycache__/douyin.cpython-310.pyc and b/src/tasks/__pycache__/douyin.cpython-310.pyc differ diff --git a/src/tasks/bilibili.py b/src/tasks/bilibili.py index be735d0..a57b581 100644 --- a/src/tasks/bilibili.py +++ b/src/tasks/bilibili.py @@ -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 \ No newline at end of file + return None, None diff --git a/src/tasks/douyin.py b/src/tasks/douyin.py index 0b0c892..65ea4c4 100644 --- a/src/tasks/douyin.py +++ b/src/tasks/douyin.py @@ -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}") \ No newline at end of file + log_callback(f"[{index + 1}/{total_count}] {file_name_base} -> 处理出错: {e}") diff --git a/src/ui/__pycache__/app.cpython-310.pyc b/src/ui/__pycache__/app.cpython-310.pyc index f8d649b..d58e264 100644 Binary files a/src/ui/__pycache__/app.cpython-310.pyc and b/src/ui/__pycache__/app.cpython-310.pyc differ diff --git a/src/ui/app.py b/src/ui/app.py index 6657a62..2ec4c98 100644 --- a/src/ui/app.py +++ b/src/ui/app.py @@ -123,12 +123,18 @@ class DouyinDownloaderApp: frame_platform.pack(padx=10, pady=5, fill="x") rb_douyin = tk.Radiobutton( - frame_platform, text="抖音", variable=self.platform_var, value="douyin" + frame_platform, + text="抖音", + variable=self.platform_var, + value="douyin", ) rb_douyin.pack(side="left", padx=10) rb_bilibili = tk.Radiobutton( - frame_platform, text="B站", variable=self.platform_var, value="bilibili" + frame_platform, + text="B站", + variable=self.platform_var, + value="bilibili", ) rb_bilibili.pack(side="left", padx=10) @@ -223,8 +229,8 @@ class DouyinDownloaderApp: thread = threading.Thread( target=self.run_task, args=(url, int(count_str), save_path, browser_path, platform), + daemon=True, ) - thread.daemon = True thread.start() def run_task(self, target_url, target_count, save_root, browser_path, platform): @@ -257,4 +263,4 @@ class DouyinDownloaderApp: ) def finish_task(self, title, message): - self.root.after(0, lambda: messagebox.showinfo(title, message)) \ No newline at end of file + self.root.after(0, lambda: messagebox.showinfo(title, message)) diff --git a/src/utils.py b/src/utils.py index d311c1b..c4d83bd 100644 --- a/src/utils.py +++ b/src/utils.py @@ -13,4 +13,4 @@ def find_edge_path(): for path in possible_paths: if os.path.exists(path): return path - return None \ No newline at end of file + return None diff --git a/video_downloader.py b/video_downloader.py index 78cb352..3acb7fb 100644 --- a/video_downloader.py +++ b/video_downloader.py @@ -2,10 +2,13 @@ import tkinter as tk from src.ui.app import DouyinDownloaderApp # 运行脚本 (使用 my_env 环境): -# D:\ProgramData\anaconda3\envs\my_env\python.exe "D:\Code\doing_exercises\programs\Video Downloader\video_downloader.py" +# D:\ProgramData\anaconda3\envs\my_env\python.exe video_downloader.py # # 打包成 exe (使用 my_env 环境): -# D:\ProgramData\anaconda3\envs\my_env\python.exe -m nuitka --standalone --mingw64 --show-memory --show-progress --output-dir=build_nuitka --enable-plugin=tk-inter --include-data-dir=ico=ico --windows-icon-from-ico=ico\video_downloader.ico --main=video_downloader.py +# D:\ProgramData\anaconda3\envs\my_env\python.exe -m nuitka --standalone ^ +# --mingw64 --show-memory --show-progress --output-dir=build_nuitka ^ +# --enable-plugin=tk-inter --include-data-dir=ico=ico ^ +# --windows-icon-from-ico=ico\video_downloader.ico --main=video_downloader.py # 主函数入口 if __name__ == "__main__": @@ -13,7 +16,7 @@ if __name__ == "__main__": root = tk.Tk() app = DouyinDownloaderApp(root) root.mainloop() - except Exception as e: + except Exception: import traceback with open("error_log.txt", "w") as f: