Skip to content

Bulk asynchronous downloader for multiple file hosts

add get_json

Jules-WinnfieldX1d1d1aebb0d0
diff --git a/cyberdrop_dl/clients/scraper_client.py b/cyberdrop_dl/clients/scraper_client.py
index 86cb7a19c..ee96ec752 100644
--- a/cyberdrop_dl/clients/scraper_client.py
+++ b/cyberdrop_dl/clients/scraper_client.py
@@ -2,7 +2,7 @@ from __future__ import annotations
import aiohttp
from functools import wraps
-from typing import TYPE_CHECKING
+from typing import TYPE_CHECKING, Dict
from aiohttp import ClientSession
from bs4 import BeautifulSoup
@@ -52,3 +52,15 @@ class ScraperClient:
raise InvalidContentTypeFailure(message=f"Received {content_type}, was expecting text")
text = await response.text()
return BeautifulSoup(text, 'html.parser')
+
+ @limiter
+ async def get_json(self, domain: str, url: URL, client_session: ClientSession) -> Dict:
+ """Returns a JSON 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:
+ await self.client_manager.check_http_status(response.status, response.headers, response.url)
+ content_type = response.headers.get('Content-Type')
+ assert content_type is not None
+ if 'json' not in content_type.lower():
+ raise InvalidContentTypeFailure(message=f"Received {content_type}, was expecting JSON")
+ return await response.json()