fix: correction et ajout de tests

This commit is contained in:
2026-02-09 23:11:15 +01:00
parent c62a5e6a76
commit ae66e94d6c
2 changed files with 131 additions and 60 deletions

69
main.py
View File

@@ -6,10 +6,11 @@ from json import loads
class _ScraperData:
def __init__(self, data: dict[str, object]) -> None:
def __init__(self, data: dict[str, object], scraper: Scraper | None = None) -> None:
self._data: dict[str, object] = data
self._scraper: Scraper | None = scraper
def _getcontent(self) -> dict[str, object]:
def _getcontent(self) -> dict[str, object] | None:
"""_summary_
Returns:
@@ -17,31 +18,39 @@ class _ScraperData:
"""
current_data: dict[str, object] = self._data
for key in ["initialReduxState", "product", "content"]:
current_data = cast(dict[str, object], current_data[key])
new_data: object | None = current_data.get(key)
if new_data is None:
return None
current_data: dict[str, object] = cast(dict[str, object], new_data)
return current_data
def _getattributes(self) -> dict[str, object]:
def _getattributes(self) -> dict[str, object] | None:
"""_summary_
Returns:
dict[str, object]: _description_
"""
current_data: object = self._getcontent()["attributes"]
return cast(dict[str, object], current_data)
current_data: object = self._getcontent()
if current_data is None:
return None
return cast(dict[str, object], current_data.get("attributes"))
def appellation(self) -> str:
def appellation(self) -> str | None:
"""_summary_
Returns:
str: _description_
"""
current_value: dict[str, object] = self._getattributes()
app_dict: dict[str, object] = cast(
dict[str, object], current_value["appellation"]
)
return cast(str, app_dict["value"])
attrs: dict[str, object] | None = self._getattributes()
def _getvin(self, name: str) -> str | None:
if attrs is not None:
app_dict: object | None = attrs.get("appellation")
if isinstance(app_dict, dict):
return cast(str, app_dict.get("value"))
return None
def _getcritiques(self, name: str) -> str | None:
"""_summary_
Args:
@@ -50,28 +59,30 @@ class _ScraperData:
Returns:
str | None: _description_
"""
current_value: dict[str, object] = self._getattributes()
app_dict: dict[str, object] | None = cast(
dict[str, object] | None, current_value.get(name)
)
if app_dict is None:
return None
current_value: dict[str, object] | None = self._getattributes()
if current_value is not None:
app_dict: dict[str, object] = cast(
dict[str, object], current_value.get(name)
)
if not app_dict:
return None
val: list[str] = cast(str, app_dict.get("attributes")).rstrip("+").split("-")
# dans le cas où 93-94 -> [93, 94] -> 93.5
if len(val) > 1:
val[0] = str((int(val[0]) + int(val[1])) / 2)
return val[0]
val = cast(str, app_dict.get("value")).rstrip("+").split("-")
if len(val) > 1:
val[0] = str((int(val[0]) + int(val[1])) / 2)
return val[0]
return None
def parker(self) -> str | None:
return self._getvin("note_rp")
return self._getcritiques("note_rp")
def robinson(self) -> str | None:
return self._getvin("note_jr")
return self._getcritiques("note_jr")
def suckling(self) -> str | None:
return self._getvin("note_js")
return self._getcritiques("note_js")
def getdata(self) -> dict[str, object]:
return self._data
@@ -210,6 +221,4 @@ class Scraper:
continue
raise ValueError(f"Clé manquante dans le JSON : {key}")
return _ScraperData(cast(dict[str, object], current_data))
# print(Scraper().getjsondata("bordeaux.html?page=1").getdata())
return _ScraperData(cast(dict[str, object], current_data))