|
10 | 10 | from module.utils import gen_save_path |
11 | 11 |
|
12 | 12 | from .client import AuthorizationError, BaseDownloader |
| 13 | +from .download_queue import download_queue |
13 | 14 |
|
14 | 15 | logger = logging.getLogger(__name__) |
15 | 16 |
|
@@ -48,7 +49,6 @@ async def wrapper(self, *args, **kwargs): |
48 | 49 | if lock_task.done() and not lock_task.cancelled(): |
49 | 50 | self._api_lock.release() |
50 | 51 | raise asyncio.CancelledError("API call cancelled") |
51 | | - |
52 | 52 | # 成功获取锁 |
53 | 53 | break |
54 | 54 |
|
@@ -145,19 +145,19 @@ async def login(self): |
145 | 145 | self.login_success_event.clear() # 重置事件状态 |
146 | 146 | self.is_authenticating = True # 设置正在认证状态 |
147 | 147 | logger.debug("[Downloader Client] attempting authentication") |
148 | | - |
| 148 | + if not await self.check_host(): |
| 149 | + logger.error("[Downloader Client] Downloader host check failed") |
| 150 | + return |
| 151 | + logger.debug("[Downloader Client] Downloader host check passed") |
149 | 152 | if await self.downloader.auth(): |
150 | 153 | self.login_success_event.set() # 设置认证成功事件 |
151 | | - logger.info("[Downloader Client] authentication success") |
152 | | - else: |
153 | | - # 保持 login_success_event 为 clear 状态,表示认证失败 |
154 | | - logger.warning("[Downloader Client] authentication failed") |
155 | 154 |
|
156 | 155 | except Exception as e: |
157 | 156 | logger.error(f"[Downloader Client] authentication error: {e}") |
158 | 157 | # 保持 login_success_event 为 clear 状态,表示认证失败 |
159 | 158 | finally: |
160 | 159 | self.is_authenticating = False |
| 160 | + self.login_task = None |
161 | 161 |
|
162 | 162 | async def wait_for_login(self) -> bool: |
163 | 163 | """等待认证完成,返回是否可以继续API调用""" |
@@ -238,14 +238,18 @@ async def add_torrent(self, torrent: Torrent, bangumi) -> bool: |
238 | 238 | ) |
239 | 239 | except AuthorizationError: |
240 | 240 | self.start_login() |
| 241 | + #TODO: 重试太多了怎么办? |
| 242 | + # https://mikanani.me/Home/Episode/4294fd53bcd1bfe2ff3b5796004ee3ccb1ba0d0e 这是个死种 |
| 243 | + download_queue.add(torrent, bangumi) # 重新放回队列 |
| 244 | + logger.debug( f"[Downloader] Add torrent failed, re-adding to queue: {torrent.name}") |
241 | 245 | return False |
242 | 246 |
|
243 | 247 | @api_rate_limit |
244 | 248 | async def move_torrent(self, hashes: list[str] | str, location: str) -> bool: |
245 | 249 | if not await self.wait_for_login(): |
246 | 250 | return False # 登录失败时返回False |
247 | | - |
248 | 251 | try: |
| 252 | + #TODO: 好像是用 | 连起来就行,但现在好像用不上了 |
249 | 253 | result = await self.downloader.move(hashes=hashes, new_location=location) |
250 | 254 | if result: |
251 | 255 | logger.info(f"[Downloader] Move torrents {hashes} to {location}") |
@@ -333,7 +337,7 @@ async def get_torrent_files(self, _hash: str) -> list[str] | None: |
333 | 337 | return [] |
334 | 338 |
|
335 | 339 | def start_login(self): |
336 | | - if not self.is_authenticating: |
| 340 | + if not self.is_authenticating and self.login_task is None: |
337 | 341 | self.is_authenticating = True # 设置认证状态 |
338 | 342 | self.login_task = asyncio.create_task(self.login(), name="login") |
339 | 343 |
|
@@ -371,7 +375,8 @@ def get_waiting_api_count(self) -> int: |
371 | 375 | async def stop(self): |
372 | 376 | logger.info("[Download Client] Stopping download client") |
373 | 377 | self.cancel_all_api_calls() # 先取消所有API调用 |
374 | | - await self.downloader.logout() |
| 378 | + if self.login_success_event.is_set(): |
| 379 | + await self.downloader.logout() |
375 | 380 | if self.login_task: |
376 | 381 | self.login_task.cancel() |
377 | 382 |
|
|
0 commit comments