mirror of
https://github.com/guezoloic/millesima-ai-engine.git
synced 2026-03-31 11:11:37 +00:00
ajout(main.py): initialise la fonction getvin
This commit is contained in:
52
main.py
52
main.py
@@ -46,53 +46,35 @@ class _ScraperData:
|
|||||||
|
|
||||||
Formule générale :
|
Formule générale :
|
||||||
prix_unitaire = offerPrice / (nbunit * equivbtl)
|
prix_unitaire = offerPrice / (nbunit * equivbtl)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
content = self._getcontent()
|
content = self._getcontent()
|
||||||
|
|
||||||
# si content n'existe pas -> erreur
|
|
||||||
if content is None:
|
if content is None:
|
||||||
raise ValueError("Contenu introuvable")
|
raise ValueError("Contenu introuvable")
|
||||||
|
|
||||||
# On récupère la liste des formats disponibles (bouteille, carton...)
|
|
||||||
items = content.get("items")
|
items = content.get("items")
|
||||||
|
|
||||||
# Vérification que items est bien une liste non 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)")
|
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:
|
for item in items:
|
||||||
|
|
||||||
if not isinstance(item, dict):
|
if not isinstance(item, dict):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# On récupère les attributs du format
|
|
||||||
attrs = item.get("attributes", {})
|
attrs = item.get("attributes", {})
|
||||||
|
|
||||||
# On récupère nbunit et equivbtl
|
|
||||||
nbunit = attrs.get("nbunit", {}).get("value")
|
nbunit = attrs.get("nbunit", {}).get("value")
|
||||||
equivbtl = attrs.get("equivbtl", {}).get("value")
|
equivbtl = attrs.get("equivbtl", {}).get("value")
|
||||||
|
|
||||||
# Si c'est une bouteille unitaire
|
|
||||||
if nbunit == "1" and equivbtl == "1":
|
if nbunit == "1" and equivbtl == "1":
|
||||||
|
|
||||||
p = item.get("offerPrice")
|
p = item.get("offerPrice")
|
||||||
|
|
||||||
# Vérification que c'est bien un nombre
|
|
||||||
if isinstance(p, (int, float)):
|
if isinstance(p, (int, float)):
|
||||||
return float(p)
|
return float(p)
|
||||||
|
|
||||||
# --------------------------
|
|
||||||
# CAS 2 : caisse ou autre format
|
|
||||||
# --------------------------
|
|
||||||
# On calcule le prix unitaire à partir du prix total
|
|
||||||
for item in items:
|
for item in items:
|
||||||
|
|
||||||
if not isinstance(item, dict):
|
if not isinstance(item, dict):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -102,22 +84,12 @@ 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")
|
||||||
|
|
||||||
# Vérification que toutes les valeurs existent
|
|
||||||
if isinstance(p, (int, float)) and nbunit and equivbtl:
|
if isinstance(p, (int, float)) and nbunit and equivbtl:
|
||||||
|
|
||||||
# Calcul du nombre total de bouteilles équivalentes
|
|
||||||
denom = float(nbunit) * float(equivbtl)
|
denom = float(nbunit) * float(equivbtl)
|
||||||
|
|
||||||
# Évite division par zéro
|
|
||||||
if denom > 0:
|
if denom > 0:
|
||||||
|
|
||||||
# Calcul du prix unitaire
|
|
||||||
prix_unitaire = float(p) / denom
|
prix_unitaire = float(p) / denom
|
||||||
|
|
||||||
# Arrondi à 2 décimales
|
|
||||||
return round(prix_unitaire, 2)
|
return round(prix_unitaire, 2)
|
||||||
|
|
||||||
# Si aucun prix trouvé
|
|
||||||
raise ValueError("Impossible de trouver le prix unitaire.")
|
raise ValueError("Impossible de trouver le prix unitaire.")
|
||||||
|
|
||||||
def appellation(self) -> str | None:
|
def appellation(self) -> str | None:
|
||||||
@@ -186,7 +158,6 @@ class _ScraperData:
|
|||||||
return f"{appellation},{parker},{robinson},{suckling},{prix}"
|
return f"{appellation},{parker},{robinson},{suckling},{prix}"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Scraper:
|
class Scraper:
|
||||||
"""
|
"""
|
||||||
Scraper est une classe qui permet de gerer
|
Scraper est une classe qui permet de gerer
|
||||||
@@ -322,3 +293,26 @@ class Scraper:
|
|||||||
|
|
||||||
return _ScraperData(cast(dict[str, object], current_data))
|
return _ScraperData(cast(dict[str, object], current_data))
|
||||||
|
|
||||||
|
|
||||||
|
def getvins(subdir: str, n: int) -> None:
|
||||||
|
"""_summary_
|
||||||
|
|
||||||
|
Args:
|
||||||
|
subdir (str): _description_
|
||||||
|
n (int): nombre de page recherché
|
||||||
|
"""
|
||||||
|
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 ---")
|
||||||
|
# https://www.millesima.fr/bordeaux.html?page=1
|
||||||
|
|
||||||
|
getvins("bordeaux.html", 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user