diff --git a/module/download_stat.py b/module/download_stat.py index fec8220..a48a66e 100644 --- a/module/download_stat.py +++ b/module/download_stat.py @@ -18,6 +18,8 @@ class DownloadState(Enum): _download_result: dict = {} _paused_messages: set = set() # (chat_id, message_id) 单条暂停 _skipped_messages: set = set() # (chat_id, message_id) 单条跳过 +# 全局暂停时被手动点「继续」的消息,允许它们突破全局暂停继续下载 +_individual_run_messages: set = set() _total_download_speed: int = 0 _total_download_size: int = 0 _last_download_time: float = time.time() @@ -203,22 +205,31 @@ def set_download_state(state: DownloadState): """set download state""" global _download_state _download_state = state + # 全局恢复下载时清空豁免集合:所有消息都能跑,豁免不再有意义,防止脏状态残留 + if state == DownloadState.Downloading: + _individual_run_messages.clear() def pause_message(chat_id: str, message_id: int): """暂停单条消息下载""" _paused_messages.add((chat_id, message_id)) + # 单独暂停意味着用户想让这条停下,移除豁免状态(如有) + _individual_run_messages.discard((chat_id, message_id)) def resume_message(chat_id: str, message_id: int): - """继续单条消息下载""" + """继续单条消息下载;若全局正处于暂停,赋予这条豁免通行权""" _paused_messages.discard((chat_id, message_id)) + # 仅在全局暂停时 resume 才需要豁免;全局正常时 resume 只是从单条暂停恢复,无需进豁免集合 + if _download_state == DownloadState.StopDownload: + _individual_run_messages.add((chat_id, message_id)) def skip_message(chat_id: str, message_id: int): """跳过单条消息下载""" _skipped_messages.add((chat_id, message_id)) _paused_messages.discard((chat_id, message_id)) + _individual_run_messages.discard((chat_id, message_id)) def is_message_paused(chat_id: str, message_id: int) -> bool: @@ -229,6 +240,11 @@ def is_message_skipped(chat_id: str, message_id: int) -> bool: return (chat_id, message_id) in _skipped_messages +def is_message_individual_run(chat_id: str, message_id: int) -> bool: + """该消息是否在全局暂停下被手动放行""" + return (chat_id, message_id) in _individual_run_messages + + def clear_skipped_message(chat_id: str, message_id: int): """清除跳过标记(下载流程退出时调用)""" _skipped_messages.discard((chat_id, message_id)) @@ -276,8 +292,11 @@ async def update_download_status( return await asyncio.sleep(0.5) - # 全局暂停 - while get_download_state() == DownloadState.StopDownload: + # 全局暂停:被手动点过「继续」的单条消息享有豁免,可以突破全局暂停继续下载 + while ( + get_download_state() == DownloadState.StopDownload + and not is_message_individual_run(*_msg_key) + ): if node.is_stop_transmission: client.stop_transmission() await asyncio.sleep(1)