From c597a78191231d99e1dacf9a75353db610889868 Mon Sep 17 00:00:00 2001 From: Zachary Hampton <69336300+ZacharyHampton@users.noreply.github.com> Date: Wed, 18 Oct 2023 16:32:43 -0700 Subject: [PATCH] - None address bug fix --- homeharvest/core/scrapers/realtor/__init__.py | 33 +++++++++++-------- tests/test_realtor.py | 10 ++++++ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/homeharvest/core/scrapers/realtor/__init__.py b/homeharvest/core/scrapers/realtor/__init__.py index f345a27..1023bf2 100644 --- a/homeharvest/core/scrapers/realtor/__init__.py +++ b/homeharvest/core/scrapers/realtor/__init__.py @@ -587,21 +587,28 @@ def _parse_neighborhoods(result: dict) -> Optional[str]: return ", ".join(neighborhoods_list) if neighborhoods_list else None @staticmethod - def _parse_address(result: dict, search_type): + def handle_none_safely(address_part): + if address_part is None: + return "" + + return address_part + + def _parse_address(self, result: dict, search_type): if search_type == "general_search": - return Address( - street=f"{result['location']['address']['street_number']} {result['location']['address']['street_name']} {result['location']['address']['street_suffix']}", - unit=result["location"]["address"]["unit"], - city=result["location"]["address"]["city"], - state=result["location"]["address"]["state_code"], - zip=result["location"]["address"]["postal_code"], - ) + address = result['location']['address'] + else: + address = result["address"] + return Address( - street=f"{result['address']['street_number']} {result['address']['street_name']} {result['address']['street_suffix']}", - unit=result["address"]["unit"], - city=result["address"]["city"], - state=result["address"]["state_code"], - zip=result["address"]["postal_code"], + street=" ".join([ + self.handle_none_safely(address.get('street_number')), + self.handle_none_safely(address.get('street_name')), + self.handle_none_safely(address.get('street_suffix')), + ]).strip(), + unit=address["unit"], + city=address["city"], + state=address["state_code"], + zip=address["postal_code"], ) @staticmethod diff --git a/tests/test_realtor.py b/tests/test_realtor.py index 5b3e878..ea18852 100644 --- a/tests/test_realtor.py +++ b/tests/test_realtor.py @@ -50,6 +50,16 @@ def test_realtor_pending_comps(): assert len(set([len(result) for result in results])) == len(results) +def test_realtor_sold_past(): + result = scrape_property( + location="San Diego, CA", + past_days=30, + listing_type="sold", + ) + + assert result is not None and len(result) > 0 + + def test_realtor_comps(): result = scrape_property( location="2530 Al Lipscomb Way",