Skip to content

Commit

Permalink
- realtor support
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacharyHampton committed Oct 4, 2023
1 parent f06a016 commit f8c0dd7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
58 changes: 41 additions & 17 deletions homeharvest/core/scrapers/realtor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ def handle_address(self, property_id: str) -> list[Property]:
)
]

def handle_area(self, variables: dict, is_for_comps: bool = False, return_total: bool = False) -> list[
Property] | int:
def general_search(self, variables: dict, search_type: str, return_total: bool = False) -> list[Property] | int:
"""
Handles a location area & returns a list of properties
"""
Expand Down Expand Up @@ -221,7 +220,7 @@ def handle_area(self, variables: dict, is_for_comps: bool = False, return_total:
if self.listing_type == ListingType.SOLD and self.sold_last_x_days is not None
else "")

if not is_for_comps:
if search_type == "area":
query = (
"""query Home_search(
$city: String,
Expand All @@ -248,7 +247,7 @@ def handle_area(self, variables: dict, is_for_comps: bool = False, return_total:
results_query
)
)
else:
elif search_type == "comp_address":
query = (
"""query Property_search(
$coordinates: [Float]!
Expand All @@ -266,6 +265,20 @@ def handle_area(self, variables: dict, is_for_comps: bool = False, return_total:
limit: 200
offset: $offset
) %s""" % (sold_date_param, results_query))
else:
query = (
"""query Property_search(
$property_id: [ID]!
$offset: Int!,
) {
property_search(
query: {
property_id: $property_id
%s
}
limit: 200
offset: $offset
) %s""" % (sold_date_param, results_query))

payload = {
"query": query,
Expand All @@ -275,7 +288,7 @@ def handle_area(self, variables: dict, is_for_comps: bool = False, return_total:
response = self.session.post(self.search_url, json=payload)
response.raise_for_status()
response_json = response.json()
search_key = "home_search" if not is_for_comps else "property_search"
search_key = "home_search" if search_type == "area" else "property_search"

if return_total:
return response_json["data"][search_key]["total"]
Expand Down Expand Up @@ -367,38 +380,49 @@ def search(self):
location_type = location_info["area_type"]
is_for_comps = self.radius is not None and location_type == "address"

if location_type == "address" and not is_for_comps:
offset = 0
search_variables = {
"offset": offset,
}

search_type = "comp_address" if is_for_comps \
else "address" if location_type == "address" and not is_for_comps \
else "area"

if location_type == "address" and not is_for_comps: #: single address search, non comps
property_id = location_info["mpr_id"]
return self.handle_address(property_id)
search_variables = search_variables | {"property_id": property_id}

offset = 0
general_search = self.general_search(search_variables, search_type)
if general_search:
return general_search
else:
return self.handle_address(property_id) #: TODO: support single address search for query by property address (can go from property -> listing to get better data)

if not is_for_comps:
search_variables = {
elif not is_for_comps: #: area search
search_variables = search_variables | {
"city": location_info.get("city"),
"county": location_info.get("county"),
"state_code": location_info.get("state_code"),
"postal_code": location_info.get("postal_code"),
"offset": offset,
}
else:
else: #: comps search
coordinates = list(location_info["centroid"].values())
search_variables = {
search_variables = search_variables | {
"coordinates": coordinates,
"radius": "{}mi".format(self.radius),
"offset": offset,
}

total = self.handle_area(search_variables, return_total=True, is_for_comps=is_for_comps)
total = self.general_search(search_variables, return_total=True, search_type=search_type)

homes = []
with ThreadPoolExecutor(max_workers=10) as executor:
futures = [
executor.submit(
self.handle_area,
self.general_search,
variables=search_variables | {"offset": i},
return_total=False,
is_for_comps=is_for_comps,
search_type=search_type,
)
for i in range(0, total, 200)
]
Expand Down
17 changes: 17 additions & 0 deletions tests/test_realtor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def test_realtor_comps():
result = scrape_property(
location="2530 Al Lipscomb Way",
radius=0.5,
sold_last_x_days=180,
listing_type="sold",
)

assert result is not None and len(result) > 0
Expand All @@ -28,6 +30,21 @@ def test_realtor_last_x_days_sold():
assert all([result is not None for result in [days_result_30, days_result_10]]) and len(days_result_30) != len(days_result_10)


def test_realtor_single_property():
results = [
scrape_property(
location="15509 N 172nd Dr, Surprise, AZ 85388",
listing_type="for_sale",
),
scrape_property(
location="2530 Al Lipscomb Way",
listing_type="for_sale",
)
]

assert all([result is not None for result in results])


def test_realtor():
results = [
scrape_property(
Expand Down

0 comments on commit f8c0dd7

Please sign in to comment.