解决浏览器使用downloadKit下载时线程被强制关闭的问题

This commit is contained in:
g1879 2023-01-21 22:52:55 +08:00
parent 33ab90fe52
commit 3b09e69d43
2 changed files with 19 additions and 8 deletions

View File

@ -5,6 +5,7 @@
"""
from pathlib import Path
from platform import system
from threading import Thread
from time import perf_counter, sleep
from DownloadKit import DownloadKit
@ -402,6 +403,7 @@ class ChromiumDownloadSetter(DownloadSetter):
def __init__(self, page):
super().__init__(page)
self._behavior = 'allow'
self._download_th = None
def save_path(self, path):
"""设置下载路径
@ -436,12 +438,17 @@ class ChromiumDownloadSetter(DownloadSetter):
self._behavior = 'deny'
def _download_by_DownloadKit(self, **kwargs):
gid = kwargs['guid']
self._page.run_cdp('Browser.cancelDownload', guid=gid, not_change=True)
url = kwargs['url']
name = kwargs['suggestedFilename']
print(f'下载:{url}')
self._page.download.add(url, goal_path=self._page.download_path, rename=name)
"""拦截浏览器下载并用downloadKit下载"""
if self._download_th is None or not self._download_th.is_alive():
self._download_th = Thread(target=self._wait_download_complete, daemon=False)
self._download_th.start()
self._page.download.add(kwargs['url'], self._page.download_path, kwargs['suggestedFilename'])
self._page.run_cdp('Browser.cancelDownload', guid=kwargs['guid'], not_change=True)
print(f'下载:{kwargs["url"]}')
def _wait_download_complete(self):
"""等待下载完成"""
self._page.download.wait()
class Alert(object):

View File

@ -5,6 +5,7 @@
"""
from os import popen
from pathlib import Path
from threading import Thread
from typing import Union, Tuple, List
from DownloadKit import DownloadKit
@ -37,12 +38,12 @@ class ChromiumPage(ChromiumBase):
addr_driver_opts: Union[str, ChromiumDriver, DriverOptions] = None,
tab_id: str = None) -> None: ...
def _init_page(self, tab_id: str = None) -> None: ...
def _set_options(self) -> None: ...
def _set_chromium_options(self) -> None: ...
def _init_page(self, tab_id: str = None) -> None: ...
@property
def tabs_count(self) -> int: ...
@ -108,6 +109,9 @@ class ChromiumDownloadSetter(object):
def __init__(self, page: ChromiumPage):
self._page: ChromiumPage = ...
self._behavior: str = ...
self._download_th: Thread = ...
def _wait_download_complete(self) -> None: ...
def save_path(self, path) -> None: ...