mirror of
https://github.com/guezoloic/millesima-ai-engine.git
synced 2026-03-28 18:03:47 +00:00
Merge branch 'optimisation' of https://github.com/guezoloic/millesima_projetS6 into optimisation
This commit is contained in:
59
main.py
59
main.py
@@ -6,10 +6,11 @@ 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]:
|
def _getcontent(self) -> dict[str, object] | None:
|
||||||
"""_summary_
|
"""_summary_
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@@ -17,31 +18,39 @@ class _ScraperData:
|
|||||||
"""
|
"""
|
||||||
current_data: dict[str, object] = self._data
|
current_data: dict[str, object] = self._data
|
||||||
for key in ["initialReduxState", "product", "content"]:
|
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
|
return current_data
|
||||||
|
|
||||||
def _getattributes(self) -> dict[str, object]:
|
def _getattributes(self) -> dict[str, object] | None:
|
||||||
"""_summary_
|
"""_summary_
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict[str, object]: _description_
|
dict[str, object]: _description_
|
||||||
"""
|
"""
|
||||||
current_data: object = self._getcontent()["attributes"]
|
current_data: object = self._getcontent()
|
||||||
return cast(dict[str, object], current_data)
|
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_
|
"""_summary_
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: _description_
|
str: _description_
|
||||||
"""
|
"""
|
||||||
current_value: dict[str, object] = self._getattributes()
|
attrs: dict[str, object] | None = self._getattributes()
|
||||||
app_dict: dict[str, object] = cast(
|
|
||||||
dict[str, object], current_value["appellation"]
|
|
||||||
)
|
|
||||||
return cast(str, app_dict["value"])
|
|
||||||
|
|
||||||
def _getvin(self, name: str) -> str | None:
|
if attrs is not None:
|
||||||
|
app_dict: object | None = attrs.get("appellation")
|
||||||
|
if isinstance(app_dict, dict):
|
||||||
|
return cast(str, app_dict.get("value"))
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _getcritiques(self, name: str) -> str | None:
|
||||||
"""_summary_
|
"""_summary_
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -50,28 +59,30 @@ class _ScraperData:
|
|||||||
Returns:
|
Returns:
|
||||||
str | None: _description_
|
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)
|
|
||||||
)
|
|
||||||
|
|
||||||
if app_dict is None:
|
current_value: dict[str, object] | None = self._getattributes()
|
||||||
|
if current_value is not None:
|
||||||
|
app_dict: dict[str, object] = cast(
|
||||||
|
dict[str, object], current_value.get(name)
|
||||||
|
)
|
||||||
|
if not app_dict:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
val: list[str] = cast(str, app_dict.get("attributes")).rstrip("+").split("-")
|
val = cast(str, app_dict.get("value")).rstrip("+").split("-")
|
||||||
# dans le cas où 93-94 -> [93, 94] -> 93.5
|
|
||||||
if len(val) > 1:
|
if len(val) > 1:
|
||||||
val[0] = str((int(val[0]) + int(val[1])) / 2)
|
val[0] = str((int(val[0]) + int(val[1])) / 2)
|
||||||
|
|
||||||
return val[0]
|
return val[0]
|
||||||
|
return None
|
||||||
|
|
||||||
def parker(self) -> str | None:
|
def parker(self) -> str | None:
|
||||||
return self._getvin("note_rp")
|
return self._getcritiques("note_rp")
|
||||||
|
|
||||||
def robinson(self) -> str | None:
|
def robinson(self) -> str | None:
|
||||||
return self._getvin("note_jr")
|
return self._getcritiques("note_jr")
|
||||||
|
|
||||||
def suckling(self) -> str | None:
|
def suckling(self) -> str | None:
|
||||||
return self._getvin("note_js")
|
return self._getcritiques("note_js")
|
||||||
|
|
||||||
def getdata(self) -> dict[str, object]:
|
def getdata(self) -> dict[str, object]:
|
||||||
return self._data
|
return self._data
|
||||||
@@ -211,5 +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))
|
||||||
|
|
||||||
# print(Scraper().getjsondata("bordeaux.html?page=1").getdata())
|
|
||||||
|
|||||||
114
test_main.py
114
test_main.py
@@ -10,7 +10,47 @@ def mock_site():
|
|||||||
with Mocker() as m:
|
with Mocker() as m:
|
||||||
m.get(
|
m.get(
|
||||||
"https://www.millesima.fr/",
|
"https://www.millesima.fr/",
|
||||||
text="<html><body><h1>MILLESIMA</h1></body></html>",
|
text=f"""
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<script id="__NEXT_DATA__" type="application/json">
|
||||||
|
{dumps({
|
||||||
|
"props": {
|
||||||
|
"pageProps": {
|
||||||
|
"initialReduxState": {
|
||||||
|
"product": {
|
||||||
|
"content": {
|
||||||
|
"items": [],
|
||||||
|
"attributes": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
""",
|
||||||
|
)
|
||||||
|
|
||||||
|
m.get(
|
||||||
|
"https://www.millesima.fr/poubelle",
|
||||||
|
text=f"""
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>POUBELLE</h1>
|
||||||
|
<script id="__NEXT_DATA__" type="application/json">
|
||||||
|
{dumps({
|
||||||
|
"props": {
|
||||||
|
"pageProps": {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
json_data = {
|
json_data = {
|
||||||
@@ -92,16 +132,16 @@ def mock_site():
|
|||||||
"isSpirit": False,
|
"isSpirit": False,
|
||||||
},
|
},
|
||||||
"note_jr": {
|
"note_jr": {
|
||||||
"valueId": "17",
|
"valueId": "17+",
|
||||||
"name": "J. Robinson",
|
"name": "J. Robinson",
|
||||||
"value": "17",
|
"value": "17+",
|
||||||
"isSpirit": False,
|
"isSpirit": False,
|
||||||
},
|
},
|
||||||
"note_js": {
|
"note_js": {
|
||||||
"valueId": "93-94",
|
"valueId": "93-94",
|
||||||
"name": "J. Suckling",
|
"name": "J. Suckling",
|
||||||
"value": "93-94",
|
"value": "93-94",
|
||||||
"isSpirit": False
|
"isSpirit": False,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -113,6 +153,8 @@ def mock_site():
|
|||||||
|
|
||||||
html_product = f"""
|
html_product = f"""
|
||||||
<html>
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>MILLESIMA</h1>
|
||||||
<script id="__NEXT_DATA__" type="application/json">
|
<script id="__NEXT_DATA__" type="application/json">
|
||||||
{dumps(json_data)}
|
{dumps(json_data)}
|
||||||
</script>
|
</script>
|
||||||
@@ -134,30 +176,50 @@ def scraper() -> Scraper:
|
|||||||
|
|
||||||
|
|
||||||
def test_soup(scraper: Scraper):
|
def test_soup(scraper: Scraper):
|
||||||
h1: Tag | None = scraper.getsoup("").find("h1")
|
vide = scraper.getsoup("")
|
||||||
|
poubelle = scraper.getsoup("poubelle")
|
||||||
assert isinstance(h1, Tag)
|
contenu = scraper.getsoup("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
assert h1.text == "MILLESIMA"
|
assert vide.find("h1") is None
|
||||||
|
assert str(poubelle.find("h1")) == "<h1>POUBELLE</h1>"
|
||||||
|
assert str(contenu.find("h1")) == "<h1>MILLESIMA</h1>"
|
||||||
# def test_getProductName(scraper: Scraper):
|
|
||||||
# jsondata = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
|
||||||
# assert jsondata["productName"] == "Nino Negri : 5 Stelle Sfursat 2022"
|
|
||||||
# assert isinstance(jsondata["items"], list)
|
|
||||||
# assert len(jsondata["items"]) > 0
|
|
||||||
# assert jsondata["items"][0]["offerPrice"] == 390
|
|
||||||
|
|
||||||
|
|
||||||
def test_appellation(scraper: Scraper):
|
def test_appellation(scraper: Scraper):
|
||||||
appellation = scraper.getjsondata(
|
vide = scraper.getjsondata("")
|
||||||
"nino-negri-5-stelle-sfursat-2022.html"
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
)
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
assert appellation.appellation() == "Sforzato di Valtellina"
|
assert vide.appellation() is None
|
||||||
|
assert poubelle.appellation() is None
|
||||||
|
assert contenu.appellation() == "Sforzato di Valtellina"
|
||||||
|
|
||||||
|
|
||||||
|
def test_fonctionprivee(scraper: Scraper):
|
||||||
|
vide = scraper.getjsondata("")
|
||||||
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
|
assert vide._getattributes() is not None
|
||||||
|
assert vide._getattributes() == {}
|
||||||
|
assert vide._getcontent() is not None
|
||||||
|
assert vide._getcontent() == {"items": [], "attributes": {}}
|
||||||
|
assert poubelle._getattributes() is None
|
||||||
|
assert poubelle._getcontent() is None
|
||||||
|
assert contenu._getcontent() is not None
|
||||||
|
assert contenu._getattributes() is not None
|
||||||
|
|
||||||
|
|
||||||
def test_critiques(scraper: Scraper):
|
def test_critiques(scraper: Scraper):
|
||||||
critiques = scraper.getjsondata(
|
vide = scraper.getjsondata("")
|
||||||
"nino-negri-5-stelle-sfursat-2022.html"
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
)
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
assert critiques.parker() == "91"
|
assert vide.parker() is None
|
||||||
assert critiques.robinson() == "17"
|
assert vide.robinson() is None
|
||||||
assert critiques.suckling() == "93.5"
|
assert vide.suckling() is None
|
||||||
|
assert vide._getcritiques("test_ts") is None
|
||||||
|
assert poubelle.parker() is None
|
||||||
|
assert poubelle.robinson() is None
|
||||||
|
assert poubelle.suckling() is None
|
||||||
|
assert poubelle._getcritiques("test_ts") is None
|
||||||
|
assert contenu.parker() == "91"
|
||||||
|
assert contenu.robinson() == "17"
|
||||||
|
assert contenu.suckling() == "93.5"
|
||||||
|
assert contenu._getcritiques("test_ts") is None
|
||||||
|
|||||||
Reference in New Issue
Block a user