From 8f21e48b288ae117558a7244ace4cc62492087a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Mon, 9 Feb 2026 21:42:03 +0100 Subject: [PATCH] ajout: changement des fonctions pour retourner un None si err --- main.py | 38 ++++++++++++++++++++++++-------------- requirements.txt | 3 +-- test_main.py | 11 ++++++----- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/main.py b/main.py index 5face8d..aaba686 100644 --- a/main.py +++ b/main.py @@ -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()) diff --git a/requirements.txt b/requirements.txt index ab2cd33..0ce08da 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ requests>=2.32.5 requests-mock>=1.12.1 -beautifulsoup4>=4.14.3 - \ No newline at end of file +beautifulsoup4>=4.14.3 \ No newline at end of file diff --git a/test_main.py b/test_main.py index 0ee81cc..2766b4e 100644 --- a/test_main.py +++ b/test_main.py @@ -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,15 +137,16 @@ 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" assert critiques.robinson() == "17" - assert critiques.suckling() == "93.5" \ No newline at end of file + assert critiques.suckling() == "93.5"