HomeHarvest/homeharvest/__init__.py

125 lines
3.6 KiB
Python
Raw Normal View History

2023-09-15 15:17:37 -07:00
from .core.scrapers.redfin import RedfinScraper
2023-09-15 20:58:54 -07:00
from .core.scrapers.realtor import RealtorScraper
2023-09-17 13:06:31 -07:00
from .core.scrapers.zillow import ZillowScraper
2023-09-18 11:38:17 -07:00
from .core.scrapers.models import ListingType, Property, SiteName
2023-09-15 15:17:37 -07:00
from .core.scrapers import ScraperInput
from .exceptions import InvalidSite, InvalidListingType
2023-09-17 13:06:31 -07:00
from typing import Union
2023-09-17 16:30:37 -07:00
import pandas as pd
2023-09-15 15:17:37 -07:00
_scrapers = {
"redfin": RedfinScraper,
2023-09-17 13:06:31 -07:00
"realtor.com": RealtorScraper,
"zillow": ZillowScraper,
2023-09-15 15:17:37 -07:00
}
2023-09-17 16:52:34 -07:00
def validate_input(site_name: str, listing_type: str) -> None:
2023-09-15 15:21:29 -07:00
if site_name.lower() not in _scrapers:
2023-09-15 15:17:37 -07:00
raise InvalidSite(f"Provided site, '{site_name}', does not exist.")
if listing_type.upper() not in ListingType.__members__:
2023-09-17 13:06:31 -07:00
raise InvalidListingType(
f"Provided listing type, '{listing_type}', does not exist."
)
2023-09-15 15:17:37 -07:00
2023-09-17 16:52:34 -07:00
2023-09-18 11:38:17 -07:00
def get_ordered_properties(result: Property) -> list[str]:
return [
"property_url",
"site_name",
"listing_type",
"property_type",
"status_text",
"currency",
"price",
"apt_min_price",
"tax_assessed_value",
"square_feet",
"price_per_sqft",
"beds",
"baths",
"lot_area_value",
"lot_area_unit",
"street_address",
"unit",
"city",
"state",
"zip_code",
"country",
"posted_time",
"bldg_min_beds",
"bldg_min_baths",
"bldg_min_area",
"bldg_unit_count",
"bldg_name",
"stories",
"year_built",
"agent_name",
"mls_id",
"description",
"img_src",
"latitude",
"longitude",
]
def process_result(result: Property) -> pd.DataFrame:
2023-09-17 16:52:34 -07:00
prop_data = result.__dict__
prop_data["site_name"] = prop_data["site_name"].value
2023-09-18 11:38:17 -07:00
prop_data["listing_type"] = prop_data["listing_type"].value.lower()
if "property_type" in prop_data and prop_data["property_type"] is not None:
prop_data["property_type"] = prop_data["property_type"].value.lower()
else:
prop_data["property_type"] = None
2023-09-18 11:38:17 -07:00
if "address" in prop_data:
address_data = prop_data["address"]
prop_data["street_address"] = address_data.street_address
prop_data["unit"] = address_data.unit
prop_data["city"] = address_data.city
prop_data["state"] = address_data.state
prop_data["zip_code"] = address_data.zip_code
prop_data["country"] = address_data.country
2023-09-17 16:52:34 -07:00
2023-09-18 11:38:17 -07:00
del prop_data["address"]
2023-09-17 16:52:34 -07:00
properties_df = pd.DataFrame([prop_data])
properties_df = properties_df[get_ordered_properties(result)]
return properties_df
def scrape_property(
location: str,
site_name: str,
listing_type: str = "for_sale", #: for_sale, for_rent, sold
2023-09-18 08:16:59 -07:00
) -> pd.DataFrame:
2023-09-18 08:37:07 -07:00
"""
Scrape property from various sites from a given location and listing type.
:returns: pd.DataFrame
:param location: US Location (e.g. 'San Francisco, CA', 'Cook County, IL', '85281', '2530 Al Lipscomb Way')
:param site_name: Site name (e.g. 'realtor.com', 'zillow', 'redfin')
:param listing_type: Listing type (e.g. 'for_sale', 'for_rent', 'sold')
:return: pd.DataFrame containing properties
"""
2023-09-17 16:52:34 -07:00
validate_input(site_name, listing_type)
2023-09-15 15:17:37 -07:00
scraper_input = ScraperInput(
location=location,
listing_type=ListingType[listing_type.upper()],
site_name=SiteName.get_by_value(site_name.lower()),
2023-09-15 15:17:37 -07:00
)
2023-09-15 15:21:29 -07:00
site = _scrapers[site_name.lower()](scraper_input)
2023-09-17 16:30:37 -07:00
results = site.search()
2023-09-15 15:17:37 -07:00
2023-09-17 16:52:34 -07:00
properties_dfs = [process_result(result) for result in results]
2023-09-18 11:38:17 -07:00
if not properties_dfs:
return pd.DataFrame()
2023-09-17 16:30:37 -07:00
return pd.concat(properties_dfs, ignore_index=True)