mirror of
https://github.com/guezoloic/millesima_projetS6.git
synced 2026-03-31 12:21:34 +00:00
Compare commits
9 Commits
exo5
...
version_de
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cd1e266f25 | ||
|
|
2aa99453a0 | ||
|
|
bfc39db652 | ||
|
|
717fce6ca4 | ||
| 9914e8af41 | |||
|
|
5785d571b2 | ||
| 2bc5d57a31 | |||
| 8f21e48b28 | |||
|
|
9e0cb9737e |
69
main.py
69
main.py
@@ -6,9 +6,8 @@ from json import loads
|
|||||||
|
|
||||||
|
|
||||||
class _ScraperData:
|
class _ScraperData:
|
||||||
def __init__(self, data: dict[str, object], scraper: Scraper | None = None) -> None:
|
def __init__(self, data: dict[str, object]) -> None:
|
||||||
self._data: dict[str, object] = data
|
self._data: dict[str, object] = data
|
||||||
self._scraper: Scraper | None = scraper
|
|
||||||
|
|
||||||
def _getcontent(self) -> dict[str, object] | None:
|
def _getcontent(self) -> dict[str, object] | None:
|
||||||
"""_summary_
|
"""_summary_
|
||||||
@@ -36,6 +35,52 @@ class _ScraperData:
|
|||||||
return None
|
return None
|
||||||
return cast(dict[str, object], current_data.get("attributes"))
|
return cast(dict[str, object], current_data.get("attributes"))
|
||||||
|
|
||||||
|
def prix(self) -> float | None:
|
||||||
|
"""
|
||||||
|
Retourne le prix unitaire d'une bouteille (75cl).
|
||||||
|
|
||||||
|
Si aucun prix n'est disponible, retourne None.
|
||||||
|
"""
|
||||||
|
|
||||||
|
content = self._getcontent()
|
||||||
|
if content is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
items = content.get("items")
|
||||||
|
|
||||||
|
# Vérifie que items existe et n'est pas vide
|
||||||
|
if not isinstance(items, list) or len(items) == 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
prix_calcule: float | None = None
|
||||||
|
|
||||||
|
for item in items:
|
||||||
|
|
||||||
|
if not isinstance(item, dict):
|
||||||
|
continue
|
||||||
|
|
||||||
|
p = item.get("offerPrice")
|
||||||
|
attrs = item.get("attributes", {})
|
||||||
|
|
||||||
|
nbunit = attrs.get("nbunit", {}).get("value")
|
||||||
|
equivbtl = attrs.get("equivbtl", {}).get("value")
|
||||||
|
|
||||||
|
if not isinstance(p, (int, float)) or not nbunit or not equivbtl:
|
||||||
|
continue
|
||||||
|
|
||||||
|
nb = float(nbunit)
|
||||||
|
eq = float(equivbtl)
|
||||||
|
|
||||||
|
if nb <= 0 or eq <= 0:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if nb == 1 and eq == 1:
|
||||||
|
return float(p)
|
||||||
|
|
||||||
|
prix_calcule = round(float(p) / (nb * eq), 2)
|
||||||
|
|
||||||
|
return prix_calcule
|
||||||
|
|
||||||
def appellation(self) -> str | None:
|
def appellation(self) -> str | None:
|
||||||
"""_summary_
|
"""_summary_
|
||||||
|
|
||||||
@@ -87,6 +132,23 @@ class _ScraperData:
|
|||||||
def getdata(self) -> dict[str, object]:
|
def getdata(self) -> dict[str, object]:
|
||||||
return self._data
|
return self._data
|
||||||
|
|
||||||
|
def informations(self) -> str:
|
||||||
|
"""
|
||||||
|
Retourne toutes les informations sous la forme :
|
||||||
|
"Appelation,Parker,J.Robinson,J.Suckling,Prix"
|
||||||
|
"""
|
||||||
|
|
||||||
|
appellation = self.appellation()
|
||||||
|
parker = self.parker()
|
||||||
|
robinson = self.robinson()
|
||||||
|
suckling = self.suckling()
|
||||||
|
try:
|
||||||
|
prix = self.prix()
|
||||||
|
except ValueError:
|
||||||
|
prix = None
|
||||||
|
|
||||||
|
return f"{appellation},{parker},{robinson},{suckling},{prix}"
|
||||||
|
|
||||||
|
|
||||||
class Scraper:
|
class Scraper:
|
||||||
"""
|
"""
|
||||||
@@ -221,4 +283,5 @@ class Scraper:
|
|||||||
continue
|
continue
|
||||||
raise ValueError(f"Clé manquante dans le JSON : {key}")
|
raise ValueError(f"Clé manquante dans le JSON : {key}")
|
||||||
|
|
||||||
return _ScraperData(cast(dict[str, object], current_data))
|
return _ScraperData(cast(dict[str, object], current_data))
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
31
test_main.py
31
test_main.py
@@ -94,6 +94,18 @@ def mock_site():
|
|||||||
"type": "CHECKBOX",
|
"type": "CHECKBOX",
|
||||||
"isSpirit": False,
|
"isSpirit": False,
|
||||||
},
|
},
|
||||||
|
"equivbtl": {
|
||||||
|
"valueId": "1",
|
||||||
|
"name": "equivbtl",
|
||||||
|
"value": "1",
|
||||||
|
"isSpirit": False,
|
||||||
|
},
|
||||||
|
"nbunit": {
|
||||||
|
"valueId": "6",
|
||||||
|
"name": "nbunit",
|
||||||
|
"value": "6",
|
||||||
|
"isSpirit": False,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
"stock": 12,
|
"stock": 12,
|
||||||
"availability": "2026-02-05",
|
"availability": "2026-02-05",
|
||||||
@@ -195,6 +207,7 @@ def test_fonctionprivee(scraper: Scraper):
|
|||||||
assert contenu._getattributes() is not None
|
assert contenu._getattributes() is not None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_critiques(scraper: Scraper):
|
def test_critiques(scraper: Scraper):
|
||||||
vide = scraper.getjsondata("")
|
vide = scraper.getjsondata("")
|
||||||
poubelle = scraper.getjsondata("poubelle")
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
@@ -211,3 +224,21 @@ def test_critiques(scraper: Scraper):
|
|||||||
assert contenu.robinson() == "17"
|
assert contenu.robinson() == "17"
|
||||||
assert contenu.suckling() == "93.5"
|
assert contenu.suckling() == "93.5"
|
||||||
assert contenu._getcritiques("test_ts") is None
|
assert contenu._getcritiques("test_ts") is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_prix(scraper: Scraper):
|
||||||
|
vide = scraper.getjsondata("")
|
||||||
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
|
assert vide.prix() is None
|
||||||
|
assert poubelle.prix() is None
|
||||||
|
assert contenu.prix() == 65.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_informations(scraper: Scraper):
|
||||||
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
|
assert contenu.informations() == "Sforzato di Valtellina,91,17,93.5,65.0"
|
||||||
|
vide = scraper.getjsondata("")
|
||||||
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
|
assert vide.informations() == "None,None,None,None,None"
|
||||||
|
assert poubelle.informations() == "None,None,None,None,None"
|
||||||
|
|||||||
Reference in New Issue
Block a user