mirror of
https://github.com/guezoloic/millesima_projetS6.git
synced 2026-03-31 04:11:34 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ff169a4413 | |||
| 785cce1c82 | |||
| 7cd40346c4 |
89
main.py
89
main.py
@@ -6,8 +6,9 @@ from json import loads
|
|||||||
|
|
||||||
|
|
||||||
class _ScraperData:
|
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._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_
|
||||||
@@ -35,91 +36,6 @@ 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:
|
|
||||||
"""
|
|
||||||
Retourne le prix unitaire d'une bouteille (75cl).
|
|
||||||
|
|
||||||
Le JSON contient plusieurs formats de vente dans content["items"] :
|
|
||||||
- bouteille seule : nbunit = 1 et equivbtl = 1 -> prix direct
|
|
||||||
- caisse de plusieurs bouteilles : nbunit > 1 -> on divise le prix total
|
|
||||||
- formats spéciaux (magnum etc.) : equivbtl > 1 -> même calcul
|
|
||||||
|
|
||||||
Formule générale :
|
|
||||||
prix_unitaire = offerPrice / (nbunit * equivbtl)
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
content = self._getcontent()
|
|
||||||
|
|
||||||
# si content n'existe pas -> erreur
|
|
||||||
if content is None:
|
|
||||||
raise ValueError("Contenu introuvable")
|
|
||||||
|
|
||||||
# On récupère la liste des formats disponibles (bouteille, carton...)
|
|
||||||
items = content.get("items")
|
|
||||||
|
|
||||||
# Vérification que items est bien une liste non vide
|
|
||||||
if not isinstance(items, list) or len(items) == 0:
|
|
||||||
raise ValueError("Aucun prix disponible (items vide)")
|
|
||||||
|
|
||||||
# --------------------------
|
|
||||||
# CAS 1 : bouteille unitaire
|
|
||||||
# --------------------------
|
|
||||||
# On cherche un format où nbunit=1 et equivbtl=1 ->bouteille standard 75cl
|
|
||||||
for item in items:
|
|
||||||
|
|
||||||
if not isinstance(item, dict):
|
|
||||||
continue
|
|
||||||
|
|
||||||
# On récupère les attributs du format
|
|
||||||
attrs = item.get("attributes", {})
|
|
||||||
|
|
||||||
# On récupère nbunit et equivbtl
|
|
||||||
nbunit = attrs.get("nbunit", {}).get("value")
|
|
||||||
equivbtl = attrs.get("equivbtl", {}).get("value")
|
|
||||||
|
|
||||||
# Si c'est une bouteille unitaire
|
|
||||||
if nbunit == "1" and equivbtl == "1":
|
|
||||||
|
|
||||||
p = item.get("offerPrice")
|
|
||||||
|
|
||||||
# Vérification que c'est bien un nombre
|
|
||||||
if isinstance(p, (int, float)):
|
|
||||||
return float(p)
|
|
||||||
|
|
||||||
# --------------------------
|
|
||||||
# CAS 2 : caisse ou autre format
|
|
||||||
# --------------------------
|
|
||||||
# On calcule le prix unitaire à partir du prix total
|
|
||||||
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")
|
|
||||||
|
|
||||||
# Vérification que toutes les valeurs existent
|
|
||||||
if isinstance(p, (int, float)) and nbunit and equivbtl:
|
|
||||||
|
|
||||||
# Calcul du nombre total de bouteilles équivalentes
|
|
||||||
denom = float(nbunit) * float(equivbtl)
|
|
||||||
|
|
||||||
# Évite division par zéro
|
|
||||||
if denom > 0:
|
|
||||||
|
|
||||||
# Calcul du prix unitaire
|
|
||||||
prix_unitaire = float(p) / denom
|
|
||||||
|
|
||||||
# Arrondi à 2 décimales
|
|
||||||
return round(prix_unitaire, 2)
|
|
||||||
|
|
||||||
# Si aucun prix trouvé
|
|
||||||
raise ValueError("Impossible de trouver le prix unitaire.")
|
|
||||||
|
|
||||||
def appellation(self) -> str | None:
|
def appellation(self) -> str | None:
|
||||||
"""_summary_
|
"""_summary_
|
||||||
|
|
||||||
@@ -306,4 +222,3 @@ class Scraper:
|
|||||||
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,3 +1,4 @@
|
|||||||
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
|
||||||
|
|
||||||
33
test_main.py
33
test_main.py
@@ -94,18 +94,6 @@ 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",
|
||||||
@@ -175,6 +163,7 @@ def scraper() -> Scraper:
|
|||||||
return Scraper()
|
return Scraper()
|
||||||
|
|
||||||
|
|
||||||
|
# EXO1
|
||||||
def test_soup(scraper: Scraper):
|
def test_soup(scraper: Scraper):
|
||||||
vide = scraper.getsoup("")
|
vide = scraper.getsoup("")
|
||||||
poubelle = scraper.getsoup("poubelle")
|
poubelle = scraper.getsoup("poubelle")
|
||||||
@@ -184,6 +173,7 @@ def test_soup(scraper: Scraper):
|
|||||||
assert str(contenu.find("h1")) == "<h1>MILLESIMA</h1>"
|
assert str(contenu.find("h1")) == "<h1>MILLESIMA</h1>"
|
||||||
|
|
||||||
|
|
||||||
|
# EXO3
|
||||||
def test_appellation(scraper: Scraper):
|
def test_appellation(scraper: Scraper):
|
||||||
vide = scraper.getjsondata("")
|
vide = scraper.getjsondata("")
|
||||||
poubelle = scraper.getjsondata("poubelle")
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
@@ -193,6 +183,7 @@ def test_appellation(scraper: Scraper):
|
|||||||
assert contenu.appellation() == "Sforzato di Valtellina"
|
assert contenu.appellation() == "Sforzato di Valtellina"
|
||||||
|
|
||||||
|
|
||||||
|
# test fonctions privée
|
||||||
def test_fonctionprivee(scraper: Scraper):
|
def test_fonctionprivee(scraper: Scraper):
|
||||||
vide = scraper.getjsondata("")
|
vide = scraper.getjsondata("")
|
||||||
poubelle = scraper.getjsondata("poubelle")
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
@@ -207,7 +198,7 @@ def test_fonctionprivee(scraper: Scraper):
|
|||||||
assert contenu._getattributes() is not None
|
assert contenu._getattributes() is not None
|
||||||
|
|
||||||
|
|
||||||
|
# EXO4-5
|
||||||
def test_critiques(scraper: Scraper):
|
def test_critiques(scraper: Scraper):
|
||||||
vide = scraper.getjsondata("")
|
vide = scraper.getjsondata("")
|
||||||
poubelle = scraper.getjsondata("poubelle")
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
@@ -224,19 +215,3 @@ 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")
|
|
||||||
|
|
||||||
# Cas vide : items == [] -> on ne peut pas calculer -> ValueError
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
_ = vide.prix()
|
|
||||||
|
|
||||||
# Cas poubelle : JSON incomplet -> _getcontent() None -> ValueError
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
_ = poubelle.prix()
|
|
||||||
|
|
||||||
assert contenu.prix() == 65.0
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user