Better logic to handle json responses

This commit is contained in:
Karim shoair
2024-11-14 00:03:26 +02:00
parent d4b896b4e9
commit eaa7da27c6
2 changed files with 20 additions and 16 deletions
+13 -1
View File
@@ -4,8 +4,9 @@ from itertools import chain
# Using cache on top of a class is brilliant way to achieve Singleton design pattern without much code
from functools import lru_cache as cache # functools.cache is available on Python 3.9+ only so let's keep lru_cache
from scrapling.core._types import Dict, Iterable, Any
from scrapling.core._types import Dict, Iterable, Any, Union
import orjson
from lxml import html
html_forbidden = {html.HtmlComment, }
@@ -18,6 +19,17 @@ logging.basicConfig(
)
def is_jsonable(content: Union[bytes, str]) -> bool:
if type(content) is bytes:
content = content.decode()
try:
_ = orjson.loads(content)
return True
except orjson.JSONDecodeError:
return False
@cache(None, typed=True)
def setup_basic_logging(level: str = 'debug'):
levels = {