From 0e12b9bdf796c3840f2299f2642ac3e93adc35f4 Mon Sep 17 00:00:00 2001 From: Dax Harris Date: Sat, 11 Nov 2023 13:41:58 -0500 Subject: [PATCH] more error handling --- open_groceries/adapters/costco.py | 83 ++++++++++++++++--------------- pyproject.toml | 2 +- test.py | 2 +- 3 files changed, 45 insertions(+), 42 deletions(-) diff --git a/open_groceries/adapters/costco.py b/open_groceries/adapters/costco.py index 3a7c37a..0b9e999 100644 --- a/open_groceries/adapters/costco.py +++ b/open_groceries/adapters/costco.py @@ -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 diff --git a/pyproject.toml b/pyproject.toml index e85d901..2d99b2b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] license = "MIT" diff --git a/test.py b/test.py index 0e2f95c..2fb4c46 100644 --- a/test.py +++ b/test.py @@ -2,4 +2,4 @@ groc = OpenGrocery() groc.set_nearest_stores("Rochester Institute of Technology") -print(groc.suggest("pot")) \ No newline at end of file +print(groc.search("pots")) \ No newline at end of file