mirror of
https://github.com/guezoloic/millesima_projetS6.git
synced 2026-03-28 19:13:42 +00:00
merge exo2 et commentaire exo7
This commit is contained in:
110
main.py
110
main.py
@@ -35,44 +35,24 @@ 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:
|
def prix(self) -> float | None:
|
||||||
"""
|
"""
|
||||||
Retourne le prix unitaire d'une bouteille (75cl).
|
Retourne le prix unitaire d'une bouteille (75cl).
|
||||||
|
|
||||||
Le JSON contient plusieurs formats de vente dans content["items"] :
|
Si aucun prix n'est disponible, retourne None.
|
||||||
- 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()
|
content = self._getcontent()
|
||||||
|
|
||||||
if content is None:
|
if content is None:
|
||||||
raise ValueError("Contenu introuvable")
|
return None
|
||||||
|
|
||||||
items = content.get("items")
|
items = content.get("items")
|
||||||
|
|
||||||
|
# Vérifie que items existe et n'est pas vide
|
||||||
if not isinstance(items, list) or len(items) == 0:
|
if not isinstance(items, list) or len(items) == 0:
|
||||||
raise ValueError("Aucun prix disponible (items vide)")
|
return None
|
||||||
|
|
||||||
for item in items:
|
prix_calcule: float | None = None
|
||||||
|
|
||||||
if not isinstance(item, dict):
|
|
||||||
continue
|
|
||||||
|
|
||||||
attrs = item.get("attributes", {})
|
|
||||||
|
|
||||||
nbunit = attrs.get("nbunit", {}).get("value")
|
|
||||||
equivbtl = attrs.get("equivbtl", {}).get("value")
|
|
||||||
|
|
||||||
if nbunit == "1" and equivbtl == "1":
|
|
||||||
p = item.get("offerPrice")
|
|
||||||
|
|
||||||
if isinstance(p, (int, float)):
|
|
||||||
return float(p)
|
|
||||||
|
|
||||||
for item in items:
|
for item in items:
|
||||||
if not isinstance(item, dict):
|
if not isinstance(item, dict):
|
||||||
@@ -84,13 +64,21 @@ class _ScraperData:
|
|||||||
nbunit = attrs.get("nbunit", {}).get("value")
|
nbunit = attrs.get("nbunit", {}).get("value")
|
||||||
equivbtl = attrs.get("equivbtl", {}).get("value")
|
equivbtl = attrs.get("equivbtl", {}).get("value")
|
||||||
|
|
||||||
if isinstance(p, (int, float)) and nbunit and equivbtl:
|
if not isinstance(p, (int, float)) or not nbunit or not equivbtl:
|
||||||
denom = float(nbunit) * float(equivbtl)
|
continue
|
||||||
|
|
||||||
if denom > 0:
|
nb = float(nbunit)
|
||||||
prix_unitaire = float(p) / denom
|
eq = float(equivbtl)
|
||||||
return round(prix_unitaire, 2)
|
|
||||||
raise ValueError("Impossible de trouver le prix unitaire.")
|
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_
|
||||||
@@ -153,7 +141,10 @@ class _ScraperData:
|
|||||||
parker = self.parker()
|
parker = self.parker()
|
||||||
robinson = self.robinson()
|
robinson = self.robinson()
|
||||||
suckling = self.suckling()
|
suckling = self.suckling()
|
||||||
prix = self.prix()
|
try:
|
||||||
|
prix = self.prix()
|
||||||
|
except ValueError:
|
||||||
|
prix = None
|
||||||
|
|
||||||
return f"{appellation},{parker},{robinson},{suckling},{prix}"
|
return f"{appellation},{parker},{robinson},{suckling},{prix}"
|
||||||
|
|
||||||
@@ -293,26 +284,41 @@ class Scraper:
|
|||||||
|
|
||||||
return _ScraperData(cast(dict[str, object], current_data))
|
return _ScraperData(cast(dict[str, object], current_data))
|
||||||
|
|
||||||
|
# def _geturlsearch(self, subdir: str, index: int) -> str | None:
|
||||||
|
# data: dict[str, object] = self.getjsondata(subdir).getdata()
|
||||||
|
|
||||||
def getvins(subdir: str, n: int) -> None:
|
# for element in ["initialReduxState", "categ", "content"]:
|
||||||
"""_summary_
|
# data = cast(dict[str, object], data.get(element))
|
||||||
|
# if data is None or not isinstance(data, dict):
|
||||||
|
# return None
|
||||||
|
|
||||||
Args:
|
# products = data.get("products")
|
||||||
subdir (str): _description_
|
# if not isinstance(products, list) or index >= len(products):
|
||||||
n (int): nombre de page recherché
|
# return None
|
||||||
"""
|
|
||||||
scraper: Scraper = Scraper()
|
|
||||||
for i in range(1, n+1):
|
|
||||||
j = 0
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
var = scraper.getjsondata(subdir=f"{subdir}?page={i}").getdata()["initialReduxState"]["categ"]["content"]["products"][j]["seoKeyword"]
|
|
||||||
print(scraper.getjsondata(var).informations())
|
|
||||||
j+=1
|
|
||||||
except:
|
|
||||||
break
|
|
||||||
|
|
||||||
print(f"--- fin {i}e page ---")
|
# product = products[index]
|
||||||
# https://www.millesima.fr/bordeaux.html?page=1
|
# if isinstance(product, dict):
|
||||||
|
# return str(product.get("seoKeyword"))
|
||||||
|
|
||||||
getvins("bordeaux.html", 1)
|
# return None
|
||||||
|
|
||||||
|
# def getvins(self, subdir: str) -> None:
|
||||||
|
# cache: set[str] = set[str]()
|
||||||
|
|
||||||
|
# for page in range(1, 2):
|
||||||
|
# index_link = 1
|
||||||
|
# while True:
|
||||||
|
# link: str | None = self._geturlsearch(
|
||||||
|
# subdir=f"{subdir}?page={page}", index=index_link
|
||||||
|
# )
|
||||||
|
|
||||||
|
# index_link+=1
|
||||||
|
# if link is None:
|
||||||
|
# break
|
||||||
|
|
||||||
|
# if link not in cache:
|
||||||
|
# print(self.getjsondata(link).informations())
|
||||||
|
# cache.add(link)
|
||||||
|
|
||||||
|
|
||||||
|
# Scraper().getvins("bordeaux.html")
|
||||||
|
|||||||
20
test_main.py
20
test_main.py
@@ -225,22 +225,20 @@ def test_critiques(scraper: Scraper):
|
|||||||
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):
|
def test_prix(scraper: Scraper):
|
||||||
vide = scraper.getjsondata("")
|
vide = scraper.getjsondata("")
|
||||||
poubelle = scraper.getjsondata("poubelle")
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
|
assert vide.prix() is None
|
||||||
# Cas vide : items == [] -> on ne peut pas calculer -> ValueError
|
assert poubelle.prix() is None
|
||||||
with pytest.raises(ValueError):
|
|
||||||
_ = vide.prix()
|
|
||||||
|
|
||||||
# Cas poubelle : JSON incomplet -> _getcontent() None -> ValueError
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
_ = poubelle.prix()
|
|
||||||
|
|
||||||
assert contenu.prix() == 65.0
|
assert contenu.prix() == 65.0
|
||||||
|
|
||||||
|
|
||||||
def test_informations(scraper: Scraper):
|
def test_informations(scraper: Scraper):
|
||||||
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
assert contenu.informations() == "Sforzato di Valtellina,91,17,93.5,65.0"
|
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