ajout: changement des fonctions pour retourner un None si err

This commit is contained in:
2026-02-09 21:42:03 +01:00
parent 9da0159869
commit 8f21e48b28
3 changed files with 31 additions and 21 deletions

38
main.py
View File

@@ -9,7 +9,7 @@ class _ScraperData:
def __init__(self, data: dict[str, object]) -> None:
self._data: dict[str, object] = data
def _getcontent(self) -> dict[str, object]:
def _getcontent(self) -> dict[str, object] | None:
"""_summary_
Returns:
@@ -17,27 +17,37 @@ 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()
current_value: dict[str, object] | None = self._getattributes()
if current_value is None:
return None
app_dict: dict[str, object] = cast(
dict[str, object], current_value["appellation"]
dict[str, object], current_value.get("appellation")
)
return cast(str, app_dict["value"])
@@ -50,14 +60,15 @@ 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)
)
current_value: dict[str, object] | None = self._getattributes()
if app_dict is None:
if current_value is None:
return None
app_dict: dict[str, object] = cast(
dict[str, object], current_value.get(name)
)
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:
@@ -212,4 +223,3 @@ class Scraper:
return _ScraperData(cast(dict[str, object], current_data))
# print(Scraper().getjsondata("bordeaux.html?page=1").getdata())

View File

@@ -1,4 +1,3 @@
requests>=2.32.5
requests-mock>=1.12.1
beautifulsoup4>=4.14.3

View File

@@ -2,7 +2,7 @@ from json import dumps
from bs4 import Tag
import pytest
from requests_mock import Mocker
from main import Scraper, ScraperData
from main import Scraper
@pytest.fixture(autouse=True)
@@ -89,7 +89,7 @@ def mock_site():
"valueId": "93-94",
"name": "J. Suckling",
"value": "93-94",
"isSpirit": False
"isSpirit": False,
},
},
}
@@ -137,13 +137,14 @@ def test_soup(scraper: Scraper):
def test_appellation(scraper: Scraper):
appellation: ScraperData = scraper.getjsondata(
appellation = scraper.getjsondata(
"nino-negri-5-stelle-sfursat-2022.html"
)
assert appellation.appellation() == "Sforzato di Valtellina"
def test_critiques(scraper: Scraper):
critiques: ScraperData = scraper.getjsondata(
critiques = scraper.getjsondata(
"nino-negri-5-stelle-sfursat-2022.html"
)
assert critiques.parker() == "91"