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: def __init__(self, data: dict[str, object]) -> None:
self._data: dict[str, object] = data self._data: dict[str, object] = data
def _getcontent(self) -> dict[str, object]: def _getcontent(self) -> dict[str, object] | None:
"""_summary_ """_summary_
Returns: Returns:
@@ -17,27 +17,37 @@ class _ScraperData:
""" """
current_data: dict[str, object] = self._data current_data: dict[str, object] = self._data
for key in ["initialReduxState", "product", "content"]: 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 return current_data
def _getattributes(self) -> dict[str, object]: def _getattributes(self) -> dict[str, object] | None:
"""_summary_ """_summary_
Returns: Returns:
dict[str, object]: _description_ dict[str, object]: _description_
""" """
current_data: object = self._getcontent()["attributes"] current_data: object = self._getcontent()
return cast(dict[str, object], current_data) 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_ """_summary_
Returns: Returns:
str: _description_ 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( 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"]) return cast(str, app_dict["value"])
@@ -50,14 +60,15 @@ class _ScraperData:
Returns: Returns:
str | None: _description_ str | None: _description_
""" """
current_value: dict[str, object] = self._getattributes() current_value: dict[str, object] | None = self._getattributes()
app_dict: dict[str, object] | None = cast(
dict[str, object] | None, current_value.get(name)
)
if app_dict is None: if current_value is None:
return 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("-") val: list[str] = cast(str, app_dict.get("attributes")).rstrip("+").split("-")
# dans le cas où 93-94 -> [93, 94] -> 93.5 # dans le cas où 93-94 -> [93, 94] -> 93.5
if len(val) > 1: if len(val) > 1:
@@ -212,4 +223,3 @@ class Scraper:
return _ScraperData(cast(dict[str, object], current_data)) 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>=2.32.5
requests-mock>=1.12.1 requests-mock>=1.12.1
beautifulsoup4>=4.14.3 beautifulsoup4>=4.14.3

View File

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