HomeHarvest/homeharvest/utils.py

73 lines
2.0 KiB
Python
Raw Normal View History

from .core.scrapers.models import Property, ListingType
2023-10-03 22:21:16 -07:00
import pandas as pd
2023-10-04 18:44:47 -07:00
from .exceptions import InvalidListingType
2023-10-03 22:21:16 -07:00
ordered_properties = [
2023-10-04 18:44:47 -07:00
"property_url",
"mls",
"mls_id",
"status",
"style",
"street",
"unit",
"city",
"state",
"zip_code",
"beds",
"full_baths",
"half_baths",
"sqft",
"year_built",
2023-10-09 09:00:36 -07:00
"days_on_mls",
2023-10-04 18:44:47 -07:00
"list_price",
"list_date",
"sold_price",
"last_sold_date",
"lot_sqft",
"price_per_sqft",
"latitude",
"longitude",
"stories",
"hoa_fee",
"parking_garage",
2023-10-03 22:21:16 -07:00
]
def process_result(result: Property) -> pd.DataFrame:
prop_data = {prop: None for prop in ordered_properties}
prop_data.update(result.__dict__)
if "address" in prop_data:
address_data = prop_data["address"]
2023-10-04 18:44:47 -07:00
prop_data["street"] = address_data.street
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
2023-10-03 22:21:16 -07:00
2023-10-04 18:44:47 -07:00
prop_data["price_per_sqft"] = prop_data["prc_sqft"]
2023-10-03 22:21:16 -07:00
2023-10-04 06:58:55 -07:00
description = result.description
2023-10-04 18:44:47 -07:00
prop_data["style"] = description.style
prop_data["beds"] = description.beds
prop_data["full_baths"] = description.baths_full
prop_data["half_baths"] = description.baths_half
prop_data["sqft"] = description.sqft
prop_data["lot_sqft"] = description.lot_sqft
prop_data["sold_price"] = description.sold_price
prop_data["year_built"] = description.year_built
prop_data["parking_garage"] = description.garage
prop_data["stories"] = description.stories
2023-10-03 22:21:16 -07:00
properties_df = pd.DataFrame([prop_data])
properties_df = properties_df.reindex(columns=ordered_properties)
return properties_df[ordered_properties]
def validate_input(listing_type: str) -> None:
if listing_type.upper() not in ListingType.__members__:
raise InvalidListingType(
f"Provided listing type, '{listing_type}', does not exist."
)