Skip to content

Bulk asynchronous downloader for multiple file hosts

Update scraper_client.py

Jacobde188ed3bd5c
diff --git a/cyberdrop_dl/clients/scraper_client.py b/cyberdrop_dl/clients/scraper_client.py
index 4b2df5f3e..2379245ba 100644
--- a/cyberdrop_dl/clients/scraper_client.py
+++ b/cyberdrop_dl/clients/scraper_client.py
@@ -30,8 +30,8 @@ def limiter(func):
await domain_limiter.acquire()
async with aiohttp.ClientSession(headers=self._headers, raise_for_status=False,
- cookie_jar=self.client_manager.cookies, timeout=self._timeouts,
- trace_configs=self.trace_configs) as client:
+ cookie_jar=self.client_manager.cookies, timeout=self._timeouts,
+ trace_configs=self.trace_configs) as client:
kwargs['client_session'] = client
return await func(self, *args, **kwargs)
@@ -45,7 +45,7 @@ class ScraperClient:
self.client_manager = client_manager
self._headers = {"user-agent": client_manager.user_agent}
self._timeouts = aiohttp.ClientTimeout(total=client_manager.connection_timeout + 60,
- connect=client_manager.connection_timeout)
+ connect=client_manager.connection_timeout)
self._global_limiter = self.client_manager.global_rate_limiter
self.trace_configs = []
@@ -64,7 +64,8 @@ class ScraperClient:
self.trace_configs.append(trace_config)
@limiter
- async def flaresolverr(self, domain: str, url: URL, client_session: ClientSession, origin: Optional[ ScrapeItem | URL] = None) -> str:
+ async def flaresolverr(self, domain: str, url: URL, client_session: ClientSession,
+ origin: Optional[ScrapeItem | URL] = None) -> str:
"""Returns the resolved URL from the given URL"""
if not self.client_manager.flaresolverr:
raise DDOSGuardFailure(message="FlareSolverr is not configured", origin=origin)
@@ -73,8 +74,8 @@ class ScraperClient:
data = {"cmd": "request.get", "url": str(url), "maxTimeout": 60000}
async with client_session.post(f"http://{self.client_manager.flaresolverr}/v1", headers=headers,
- ssl=self.client_manager.ssl_context,
- proxy=self.client_manager.proxy, json=data) as response:
+ ssl=self.client_manager.ssl_context,
+ proxy=self.client_manager.proxy, json=data) as response:
json_obj: dict = await response.json()
status = json_obj.get("status")
if status != "ok":
@@ -83,10 +84,11 @@ class ScraperClient:
return json_obj.get("solution").get("response")
@limiter
- async def get_BS4(self, domain: str, url: URL, client_session: ClientSession, origin: Optional[ ScrapeItem | URL] = None) -> BeautifulSoup:
+ async def get_BS4(self, domain: str, url: URL, client_session: ClientSession,
+ origin: Optional[ScrapeItem | URL] = None) -> BeautifulSoup:
"""Returns a BeautifulSoup object from the given URL"""
async with client_session.get(url, headers=self._headers, ssl=self.client_manager.ssl_context,
- proxy=self.client_manager.proxy) as response:
+ proxy=self.client_manager.proxy) as response:
try:
await self.client_manager.check_http_status(response, origin=origin)
except DDOSGuardFailure:
@@ -95,16 +97,17 @@ class ScraperClient:
content_type = response.headers.get('Content-Type')
assert content_type is not None
if not any(s in content_type.lower() for s in ("html", "text")):
- raise InvalidContentTypeFailure(message=f"Received {content_type}, was expecting text", origin=origin )
+ raise InvalidContentTypeFailure(message=f"Received {content_type}, was expecting text", origin=origin)
text = await response.text()
return BeautifulSoup(text, 'html.parser')
@limiter
- async def get_BS4_and_return_URL(self, domain: str, url: URL, client_session: ClientSession, origin: Optional[ ScrapeItem | URL] = None) -> tuple[
+ async def get_BS4_and_return_URL(self, domain: str, url: URL, client_session: ClientSession,
+ origin: Optional[ScrapeItem | URL] = None) -> tuple[
BeautifulSoup, URL]:
"""Returns a BeautifulSoup object and response URL from the given URL"""
async with client_session.get(url, headers=self._headers, ssl=self.client_manager.ssl_context,
- proxy=self.client_manager.proxy) as response:
+ proxy=self.client_manager.proxy) as response:
await self.client_manager.check_http_status(response, origin=origin)
content_type = response.headers.get('Content-Type')
assert content_type is not None
@@ -115,12 +118,12 @@ class ScraperClient:
@limiter
async def get_json(self, domain: str, url: URL, params: Optional[Dict] = None, headers_inc: Optional[Dict] = None,
- client_session: ClientSession = None, origin: Optional[ ScrapeItem | URL] = None) -> Dict:
+ client_session: ClientSession = None, origin: Optional[ScrapeItem | URL] = None) -> Dict:
"""Returns a JSON object from the given URL"""
headers = {**self._headers, **headers_inc} if headers_inc else self._headers
async with client_session.get(url, headers=headers, ssl=self.client_manager.ssl_context,
- proxy=self.client_manager.proxy, params=params) as response:
+ proxy=self.client_manager.proxy, params=params) as response:
await self.client_manager.check_http_status(response, origin=origin)
content_type = response.headers.get('Content-Type')
assert content_type is not None
@@ -129,10 +132,11 @@ class ScraperClient:
return await response.json()
@limiter
- async def get_text(self, domain: str, url: URL, client_session: ClientSession, origin: Optional[ ScrapeItem | URL] = None) -> str:
+ async def get_text(self, domain: str, url: URL, client_session: ClientSession,
+ origin: Optional[ScrapeItem | URL] = None) -> str:
"""Returns a text object from the given URL"""
async with client_session.get(url, headers=self._headers, ssl=self.client_manager.ssl_context,
- proxy=self.client_manager.proxy) as response:
+ proxy=self.client_manager.proxy) as response:
try:
await self.client_manager.check_http_status(response, origin=origin)
except DDOSGuardFailure:
@@ -143,10 +147,11 @@ class ScraperClient:
@limiter
async def post_data(self, domain: str, url: URL, client_session: ClientSession, data: Dict,
- req_resp: bool = True, raw: Optional[bool] = False, origin: Optional[ ScrapeItem | URL] = None) -> Dict:
+ req_resp: bool = True, raw: Optional[bool] = False,
+ origin: Optional[ScrapeItem | URL] = None) -> Dict:
"""Returns a JSON object from the given URL when posting data. If raw == True, returns raw binary data of response"""
async with client_session.post(url, headers=self._headers, ssl=self.client_manager.ssl_context,
- proxy=self.client_manager.proxy, data=data) as response:
+ proxy=self.client_manager.proxy, data=data) as response:
await self.client_manager.check_http_status(response, origin=origin)
if req_resp:
content = await response.content.read()
@@ -160,5 +165,5 @@ class ScraperClient:
async def get_head(self, domain: str, url: URL, client_session: ClientSession) -> CIMultiDictProxy[str]:
"""Returns the headers from the given URL"""
async with client_session.head(url, headers=self._headers, ssl=self.client_manager.ssl_context,
- proxy=self.client_manager.proxy) as response:
+ proxy=self.client_manager.proxy) as response:
return response.headers