Skip to content

Commit

Permalink
more error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dax-dot-gay committed Nov 11, 2023
1 parent 99166d0 commit 0e12b9b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 42 deletions.
83 changes: 43 additions & 40 deletions open_groceries/adapters/costco.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,48 +39,51 @@ def search_groceries(self, search: str) -> list[GroceryItem]:
soup = BeautifulSoup(req.text, features="html.parser")
results = []
for product in soup.select(".product"):
data_script = product.select("script")[1]
sc_tokens = esprima.tokenize(data_script.contents[0])
data_mapping = {}
for token in range(len(sc_tokens)):
try:
if (
sc_tokens[token].type == "Identifier"
and sc_tokens[token + 1].type == "Punctuator"
and sc_tokens[token + 2].type in ["String", "Numeric"]
):
data_mapping[sc_tokens[token].value] = sc_tokens[
token + 2
].value
except:
pass
try:
data_script = product.select("script")[1]
sc_tokens = esprima.tokenize(data_script.contents[0])
data_mapping = {}
for token in range(len(sc_tokens)):
try:
if (
sc_tokens[token].type == "Identifier"
and sc_tokens[token + 1].type == "Punctuator"
and sc_tokens[token + 2].type in ["String", "Numeric"]
):
data_mapping[sc_tokens[token].value] = sc_tokens[
token + 2
].value
except:
pass

metas = {
i.attrs["itemprop"]: i.attrs["content"] for i in product.select("meta")
}
results.append(
GroceryItem(
type="costco",
id=int(data_mapping["SKU"].strip("'")),
name=data_mapping["name"].strip("'").replace("\\", ""),
location=None,
images=[
data_mapping["productImageUrl"].strip("'").replace("\\", "")
],
tags=[
i.contents[0].replace("\\", "").strip()
for i in product.select(".product-features li")
if i.contents and len(i.contents) > 0
],
categories=[],
price=float(data_mapping["priceTotal"]),
ratings=Ratings(
average=float(metas.get("ratingValue", "0")),
count=int(metas.get("reviewCount", "0")),
),
metadata={},
metas = {
i.attrs["itemprop"]: i.attrs["content"] for i in product.select("meta")
}
results.append(
GroceryItem(
type="costco",
id=int(data_mapping["SKU"].strip("'")),
name=data_mapping["name"].strip("'").replace("\\", ""),
location=None,
images=[
data_mapping["productImageUrl"].strip("'").replace("\\", "")
],
tags=[
i.contents[0].replace("\\", "").strip()
for i in product.select(".product-features li")
if i.contents and len(i.contents) > 0
],
categories=[],
price=float(data_mapping["priceTotal"]),
ratings=Ratings(
average=float(metas.get("ratingValue", "0")),
count=int(metas.get("reviewCount", "0")),
),
metadata={},
)
)
)
except:
pass

return results

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "open-groceries"
version = "0.2.1"
version = "0.2.2"
description = "Unified data acquisition across multiple grocery store sites"
authors = ["Dax Harris <[email protected]>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

groc = OpenGrocery()
groc.set_nearest_stores("Rochester Institute of Technology")
print(groc.suggest("pot"))
print(groc.search("pots"))

0 comments on commit 0e12b9b

Please sign in to comment.