Compare commits

..

3 Commits

Author SHA1 Message Date
Zachary Hampton
5c2498c62b - pending date, property type fields (#45)
- alt photos bug fix (#57)
2024-03-13 19:17:17 -07:00
Zachary Hampton
d775540afd - location bug fix 2024-03-06 16:31:06 -07:00
Cullen Watson
5ea9a6f6b6 docs: readme 2024-03-03 11:49:27 -06:00
5 changed files with 74 additions and 41 deletions

View File

@@ -11,8 +11,6 @@
- **Source**: Fetches properties directly from **Realtor.com**.
- **Data Format**: Structures data to resemble MLS listings.
- **Export Flexibility**: Options to save as either CSV or Excel.
- **Usage Modes**:
- **Python**: For those who'd like to integrate scraping into their Python scripts.
[Video Guide for HomeHarvest](https://youtu.be/J1qgNPgmSLI) - _updated for release v0.3.4_
@@ -21,7 +19,7 @@
## Installation
```bash
pip install homeharvest
pip install -U homeharvest
```
_Python version >= [3.10](https://www.python.org/downloads/release/python-3100/) required_
@@ -126,6 +124,7 @@ Property
│ ├── days_on_mls
│ ├── list_price
│ ├── list_date
│ ├── pending_date
│ ├── sold_price
│ ├── last_sold_date
│ ├── price_per_sqft
@@ -144,4 +143,4 @@ The following exceptions may be raised when using HomeHarvest:
- `InvalidListingType` - valid options: `for_sale`, `for_rent`, `sold`
- `InvalidDate` - date_from or date_to is not in the format YYYY-MM-DD

View File

@@ -23,6 +23,27 @@ class ListingType(Enum):
SOLD = "SOLD"
class PropertyType(Enum):
APARTMENT = "APARTMENT"
BUILDING = "BUILDING"
COMMERCIAL = "COMMERCIAL"
CONDO_TOWNHOME = "CONDO_TOWNHOME"
CONDO_TOWNHOME_ROWHOME_COOP = "CONDO_TOWNHOME_ROWHOME_COOP"
CONDO = "CONDO"
CONDOS = "CONDOS"
COOP = "COOP"
DUPLEX_TRIPLEX = "DUPLEX_TRIPLEX"
FARM = "FARM"
INVESTMENT = "INVESTMENT"
LAND = "LAND"
MOBILE = "MOBILE"
MULTI_FAMILY = "MULTI_FAMILY"
RENTAL = "RENTAL"
SINGLE_FAMILY = "SINGLE_FAMILY"
TOWNHOMES = "TOWNHOMES"
OTHER = "OTHER"
@dataclass
class Address:
street: str | None = None
@@ -36,7 +57,7 @@ class Address:
class Description:
primary_photo: str | None = None
alt_photos: list[str] | None = None
style: str | None = None
style: PropertyType | None = None
beds: int | None = None
baths_full: int | None = None
baths_half: int | None = None
@@ -58,6 +79,7 @@ class Property:
list_price: int | None = None
list_date: str | None = None
pending_date: str | None = None
last_sold_date: str | None = None
prc_sqft: int | None = None
hoa_fee: int | None = None

View File

@@ -9,7 +9,7 @@ from typing import Dict, Union, Optional
from concurrent.futures import ThreadPoolExecutor, as_completed
from .. import Scraper
from ..models import Property, Address, ListingType, Description
from ..models import Property, Address, ListingType, Description, PropertyType
class RealtorScraper(Scraper):
@@ -84,11 +84,10 @@ class RealtorScraper(Scraper):
garage
permalink
}
primary_photo {
href
}
photos {
href
media {
photos {
href
}
}
}
}"""
@@ -120,9 +119,11 @@ class RealtorScraper(Scraper):
"list_date") else None
last_sold_date_str = property_info["basic"]["sold_date"].split("T")[0] if property_info["basic"].get(
"sold_date") else None
pending_date_str = property_info["pending_date"].split("T")[0] if property_info.get("pending_date") else None
list_date = datetime.strptime(list_date_str, "%Y-%m-%d") if list_date_str else None
last_sold_date = datetime.strptime(last_sold_date_str, "%Y-%m-%d") if last_sold_date_str else None
pending_date = datetime.strptime(pending_date_str, "%Y-%m-%d") if pending_date_str else None
today = datetime.now()
days_on_mls = None
@@ -150,6 +151,7 @@ class RealtorScraper(Scraper):
and property_info["basic"].get("sqft")
else None,
last_sold_date=last_sold_date,
pending_date=pending_date,
latitude=property_info["address"]["location"]["coordinate"].get("lat")
if able_to_get_lat_long
else None,
@@ -158,8 +160,7 @@ class RealtorScraper(Scraper):
else None,
address=self._parse_address(property_info, search_type="handle_listing"),
description=Description(
primary_photo=property_info["primary_photo"].get("href", "").replace("s.jpg", "od-w480_h360_x2.webp?w=1080&q=75"),
alt_photos=self.process_alt_photos(property_info.get("photos", [])),
alt_photos=self.process_alt_photos(property_info.get("media", {}).get("photos", [])),
style=property_info["basic"].get("type", "").upper(),
beds=property_info["basic"].get("beds"),
baths_full=property_info["basic"].get("baths_full"),
@@ -288,7 +289,7 @@ class RealtorScraper(Scraper):
]
def general_search(
self, variables: dict, search_type: str
self, variables: dict, search_type: str
) -> Dict[str, Union[int, list[Property]]]:
"""
Handles a location area & returns a list of properties
@@ -297,6 +298,7 @@ class RealtorScraper(Scraper):
count
total
results {
pending_date
property_id
list_date
status
@@ -309,6 +311,7 @@ class RealtorScraper(Scraper):
is_pending
}
description {
type
sqft
beds
baths_full
@@ -381,16 +384,15 @@ class RealtorScraper(Scraper):
if self.listing_type == ListingType.PENDING
else ""
)
listing_type = ListingType.FOR_SALE if self.listing_type == ListingType.PENDING else self.listing_type
is_foreclosure = ""
if 'foreclosure' in variables and variables['foreclosure'] == True:
is_foreclosure = "foreclosure: true"
if 'foreclosure' in variables and variables['foreclosure'] == False:
is_foreclosure = "foreclosure: false"
if variables.get('foreclosure') is True:
is_foreclosure = "foreclosure: true"
elif variables.get('foreclosure') is False:
is_foreclosure = "foreclosure: false"
if search_type == "comps": #: comps search, came from an address
query = """query Property_search(
$coordinates: [Float]!
@@ -412,7 +414,7 @@ class RealtorScraper(Scraper):
limit: 200
offset: $offset
) %s""" % (
is_foreclosure,
is_foreclosure,
listing_type.value.lower(),
date_param,
pending_or_contingent_param,
@@ -451,7 +453,7 @@ class RealtorScraper(Scraper):
)
else: #: general search, came from an address
query = (
"""query Property_search(
"""query Property_search(
$property_id: [ID]!
$offset: Int!,
) {
@@ -462,7 +464,7 @@ class RealtorScraper(Scraper):
limit: 1
offset: $offset
) %s"""
% results_query
% results_query
)
payload = {
@@ -478,12 +480,12 @@ class RealtorScraper(Scraper):
properties: list[Property] = []
if (
response_json is None
or "data" not in response_json
or response_json["data"] is None
or search_key not in response_json["data"]
or response_json["data"][search_key] is None
or "results" not in response_json["data"][search_key]
response_json is None
or "data" not in response_json
or response_json["data"] is None
or search_key not in response_json["data"]
or response_json["data"][search_key] is None
or "results" not in response_json["data"][search_key]
):
return {"total": 0, "properties": []}
@@ -498,10 +500,10 @@ class RealtorScraper(Scraper):
continue
able_to_get_lat_long = (
result
and result.get("location")
and result["location"].get("address")
and result["location"]["address"].get("coordinate")
result
and result.get("location")
and result["location"].get("address")
and result["location"]["address"].get("coordinate")
)
is_pending = result["flags"].get("is_pending") or result["flags"].get("is_contingent")
@@ -552,7 +554,7 @@ class RealtorScraper(Scraper):
search_variables = {
"offset": 0,
}
search_type = (
"comps"
if self.radius and location_type == "address"
@@ -578,6 +580,9 @@ class RealtorScraper(Scraper):
return gql_results["properties"]
else: #: general search, comps (radius)
if not location_info.get("centroid"):
return []
coordinates = list(location_info["centroid"].values())
search_variables |= {
"coordinates": coordinates,
@@ -597,7 +602,7 @@ class RealtorScraper(Scraper):
"postal_code": location_info.get("postal_code"),
}
if self.foreclosure:
if self.foreclosure:
search_variables['foreclosure'] = self.foreclosure
result = self.general_search(search_variables, search_type=search_type)
@@ -660,7 +665,6 @@ class RealtorScraper(Scraper):
@staticmethod
def _parse_description(result: dict) -> Description:
description_data = result.get("description", {})
if description_data is None or not isinstance(description_data, dict):
@@ -680,7 +684,7 @@ class RealtorScraper(Scraper):
return Description(
primary_photo=primary_photo,
alt_photos=RealtorScraper.process_alt_photos(result.get("photos")),
style=style,
style=PropertyType(style) if style else None,
beds=description_data.get("beds"),
baths_full=description_data.get("baths_full"),
baths_half=description_data.get("baths_half"),
@@ -692,7 +696,6 @@ class RealtorScraper(Scraper):
stories=description_data.get("stories"),
)
@staticmethod
def calculate_days_on_mls(result: dict) -> Optional[int]:
list_date_str = result.get("list_date")

View File

@@ -1,6 +1,6 @@
[tool.poetry]
name = "homeharvest"
version = "0.3.12"
version = "0.3.14"
description = "Real estate scraping library"
authors = ["Zachary Hampton <zachary@bunsly.com>", "Cullen Watson <cullen@bunsly.com>"]
homepage = "https://github.com/Bunsly/HomeHarvest"

View File

@@ -131,6 +131,15 @@ def test_realtor():
assert all([result is not None for result in results])
def test_realtor_city():
results = scrape_property(
location="Atlanta, GA",
listing_type="for_sale",
)
assert results is not None and len(results) > 0
def test_realtor_bad_address():
bad_results = scrape_property(
location="abceefg ju098ot498hh9",