From a0791f8b585e6788723deec94983dff3147a7720 Mon Sep 17 00:00:00 2001 From: Crinibus <57172157+Crinibus@users.noreply.github.com> Date: Sat, 27 Aug 2022 17:00:51 +0200 Subject: [PATCH 1/4] Update function search_product_name - add website name and product id to the string to append to the return list --- scraper/search_data.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/scraper/search_data.py b/scraper/search_data.py index ad07d024..32a4ab03 100644 --- a/scraper/search_data.py +++ b/scraper/search_data.py @@ -25,19 +25,21 @@ def search(queries: List[str]): print() -def search_product_name(product_name_search: str): +def search_product_name(product_name_search: str) -> List[str]: records_data = Filemanager.get_record_data() - matched_product_names = [] + matched_products = [] - for category_info in records_data.values(): - for product_name in category_info.keys(): + for category_dict in records_data.values(): + for product_name, product_dict in category_dict.items(): if product_name_search.lower() in product_name.lower(): - matched_product_names.append(product_name) - return matched_product_names + for website_name, website_dict in product_dict.items(): + id = website_dict["info"]["id"] + matched_products.append(f"{product_name} - {website_name.capitalize()} - {id}") + return matched_products -def search_categories(category_search: str): +def search_categories(category_search: str) -> List[str]: records_data = Filemanager.get_record_data() matched_categories = [] From f0c677f11cab4c698d440b686cb52d86d206dfe5 Mon Sep 17 00:00:00 2001 From: Crinibus <57172157+Crinibus@users.noreply.github.com> Date: Sat, 27 Aug 2022 17:03:00 +0200 Subject: [PATCH 2/4] Read records data in the main search function and pass it to the specific search functions --- scraper/search_data.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/scraper/search_data.py b/scraper/search_data.py index 32a4ab03..a5a96ad6 100644 --- a/scraper/search_data.py +++ b/scraper/search_data.py @@ -5,6 +5,8 @@ def search(queries: List[str]): print("Searching...") + records_data = Filemanager.get_record_data() + for query in queries: search_functions = [search_product_name, search_categories] searching_for_names = [ @@ -13,7 +15,7 @@ def search(queries: List[str]): ] for search_function, searching_for_name in zip(search_functions, searching_for_names): - results = search_function(query) + results = search_function(query, records_data) if not results: print(f"\nFound nothing for search term '{query}' when searching for {searching_for_name[0]}") @@ -25,9 +27,7 @@ def search(queries: List[str]): print() -def search_product_name(product_name_search: str) -> List[str]: - records_data = Filemanager.get_record_data() - +def search_product_name(product_name_search: str, records_data: dict) -> List[str]: matched_products = [] for category_dict in records_data.values(): @@ -39,9 +39,7 @@ def search_product_name(product_name_search: str) -> List[str]: return matched_products -def search_categories(category_search: str) -> List[str]: - records_data = Filemanager.get_record_data() - +def search_categories(category_search: str, records_data: dict) -> List[str]: matched_categories = [] for category_name in records_data.keys(): From 4490a0afa259c183623b073fe01d283ac779bfe9 Mon Sep 17 00:00:00 2001 From: Crinibus <57172157+Crinibus@users.noreply.github.com> Date: Mon, 12 Sep 2022 22:00:37 +0200 Subject: [PATCH 3/4] Update format of print when searching for name Print the product name once and the websites and the id of each website as a new line: ``` > product_name -- website_1 - id_1 -- website_2 - id_2 ``` --- scraper/search_data.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/scraper/search_data.py b/scraper/search_data.py index a5a96ad6..1bbfa25e 100644 --- a/scraper/search_data.py +++ b/scraper/search_data.py @@ -33,9 +33,14 @@ def search_product_name(product_name_search: str, records_data: dict) -> List[st for category_dict in records_data.values(): for product_name, product_dict in category_dict.items(): if product_name_search.lower() in product_name.lower(): + + product_websites = [] for website_name, website_dict in product_dict.items(): id = website_dict["info"]["id"] - matched_products.append(f"{product_name} - {website_name.capitalize()} - {id}") + product_websites.append(f"-- {website_name.capitalize()} - {id}") + + product_websites_string = "\n".join(product_websites) + matched_products.append(f"{product_name}\n{product_websites_string}") return matched_products From e50dd9ebe74dd866897954e58f7a74bf6ba73b21 Mon Sep 17 00:00:00 2001 From: Crinibus <57172157+Crinibus@users.noreply.github.com> Date: Fri, 16 Sep 2022 06:35:01 +0200 Subject: [PATCH 4/4] Change print prefix of list of websites when searching with name From "-- ...." to " - ....": - two hyphens to one hyphen --- scraper/search_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scraper/search_data.py b/scraper/search_data.py index 1bbfa25e..d2e9b18c 100644 --- a/scraper/search_data.py +++ b/scraper/search_data.py @@ -37,7 +37,7 @@ def search_product_name(product_name_search: str, records_data: dict) -> List[st product_websites = [] for website_name, website_dict in product_dict.items(): id = website_dict["info"]["id"] - product_websites.append(f"-- {website_name.capitalize()} - {id}") + product_websites.append(f" - {website_name.capitalize()} - {id}") product_websites_string = "\n".join(product_websites) matched_products.append(f"{product_name}\n{product_websites_string}")