Skip to content

Commit

Permalink
feat(redfin): add sold listing_type
Browse files Browse the repository at this point in the history
  • Loading branch information
cullenwatson committed Sep 19, 2023
1 parent 1fc2d8c commit dba1c03
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
4 changes: 4 additions & 0 deletions homeharvest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def get_ordered_properties(result: Property) -> list[str]:
"apt_max_price",
"apt_min_sqft",
"apt_max_sqft",
"apt_min_beds",
"apt_max_beds",
"apt_min_baths",
"apt_max_baths",
"tax_assessed_value",
"square_feet",
"price_per_sqft",
Expand Down
4 changes: 4 additions & 0 deletions homeharvest/core/scrapers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ class Property:
bldg_min_area: int | None = None

# apt
apt_min_beds: int | None = None
apt_max_beds: int | None = None
apt_min_baths: float | None = None
apt_max_baths: float | None = None
apt_min_price: int | None = None
apt_max_price: int | None = None
apt_min_sqft: int | None = None
Expand Down
20 changes: 10 additions & 10 deletions homeharvest/core/scrapers/redfin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def _handle_rentals(self, region_id, region_type):
url = f"https://www.redfin.com/stingray/api/v1/search/rentals?al=1&isRentals=true&region_id={region_id}&region_type={region_type}"

response = self.session.get(url)
response.raise_for_status() # This will raise an error if the response contains an HTTP error status.

response.raise_for_status()
homes = response.json()

properties_list = []
Expand Down Expand Up @@ -144,9 +143,10 @@ def _handle_rentals(self, region_id, region_type):
site_name=SiteName.REDFIN,
listing_type=ListingType.FOR_RENT,
address=address,
square_feet=sqft_range.get("min", None),
beds=bed_range.get("min", None),
baths=bath_range.get("min", None),
apt_min_beds=bed_range.get("min", None),
apt_min_baths=bath_range.get("min", None),
apt_max_beds=bed_range.get("max", None),
apt_max_baths=bath_range.get("max", None),
description=rental_data.get("description", None),
latitude=centroid.get("latitude", None),
longitude=centroid.get("longitude", None),
Expand Down Expand Up @@ -228,8 +228,11 @@ def search(self):

if self.listing_type == ListingType.FOR_RENT:
return self._handle_rentals(region_id, region_type)
elif self.listing_type == ListingType.FOR_SALE:
url = f"https://www.redfin.com/stingray/api/gis?al=1&region_id={region_id}&region_type={region_type}"
else:
if self.listing_type == ListingType.FOR_SALE:
url = f"https://www.redfin.com/stingray/api/gis?al=1&region_id={region_id}&region_type={region_type}"
else:
url = f"https://www.redfin.com/stingray/api/gis?al=1&region_id={region_id}&region_type={region_type}&sold_within_days=30"
response = self.session.get(url)
response_json = json.loads(response.text.replace("{}&&", ""))
homes = [
Expand All @@ -239,6 +242,3 @@ def search(self):
for building in response_json["payload"]["buildings"].values()
]
return homes
else:
# Handle other cases, maybe raise an error if the listing type is not recognized.
pass
7 changes: 3 additions & 4 deletions tests/test_redfin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ def test_redfin():
scrape_property(
location="Phoenix, AZ, USA", site_name=["redfin"], listing_type="for_rent"
),
# TODO
# scrape_property(
# location="Dallas, TX, USA", site_name="redfin", listing_type="sold"
# ),
scrape_property(
location="Dallas, TX, USA", site_name="redfin", listing_type="sold"
),
scrape_property(location="85281", site_name="redfin"),
]

Expand Down

0 comments on commit dba1c03

Please sign in to comment.