2023-09-19 19:13:20 -07:00
|
|
|
"""
|
|
|
|
homeharvest.realtor.__init__
|
|
|
|
~~~~~~~~~~~~
|
|
|
|
|
2023-10-04 06:58:55 -07:00
|
|
|
This module implements the scraper for realtor.com
|
2023-09-19 19:13:20 -07:00
|
|
|
"""
|
2023-10-04 06:58:55 -07:00
|
|
|
from typing import Dict, Union, Optional
|
|
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
|
|
|
2023-09-15 20:58:54 -07:00
|
|
|
from .. import Scraper
|
2023-09-18 08:16:59 -07:00
|
|
|
from ....exceptions import NoResultsFound
|
2023-10-04 06:58:55 -07:00
|
|
|
from ..models import Property, Address, ListingType, Description
|
2023-09-15 20:58:54 -07:00
|
|
|
|
|
|
|
|
|
|
|
class RealtorScraper(Scraper):
|
2023-10-04 10:07:32 -07:00
|
|
|
SEARCH_GQL_URL = "https://www.realtor.com/api/v1/rdc_search_srp?client_id=rdc-search-new-communities&schema=vesta"
|
2023-10-04 06:58:55 -07:00
|
|
|
PROPERTY_URL = "https://www.realtor.com/realestateandhomes-detail/"
|
|
|
|
ADDRESS_AUTOCOMPLETE_URL = "https://parser-external.geo.moveaws.com/suggest"
|
|
|
|
|
2023-09-15 20:58:54 -07:00
|
|
|
def __init__(self, scraper_input):
|
2023-09-19 19:13:20 -07:00
|
|
|
self.counter = 1
|
2023-09-15 20:58:54 -07:00
|
|
|
super().__init__(scraper_input)
|
|
|
|
|
|
|
|
def handle_location(self):
|
|
|
|
params = {
|
2023-09-17 13:06:31 -07:00
|
|
|
"input": self.location,
|
2023-09-18 13:43:44 -07:00
|
|
|
"client_id": self.listing_type.value.lower().replace("_", "-"),
|
2023-09-17 13:06:31 -07:00
|
|
|
"limit": "1",
|
|
|
|
"area_types": "city,state,county,postal_code,address,street,neighborhood,school,school_district,university,park",
|
2023-09-15 20:58:54 -07:00
|
|
|
}
|
|
|
|
|
2023-09-17 13:06:31 -07:00
|
|
|
response = self.session.get(
|
2023-10-04 06:58:55 -07:00
|
|
|
self.ADDRESS_AUTOCOMPLETE_URL,
|
2023-09-17 13:06:31 -07:00
|
|
|
params=params,
|
|
|
|
)
|
2023-09-15 20:58:54 -07:00
|
|
|
response_json = response.json()
|
|
|
|
|
2023-09-18 08:16:59 -07:00
|
|
|
result = response_json["autocomplete"]
|
|
|
|
|
2023-09-18 20:28:03 -07:00
|
|
|
if not result:
|
2023-09-18 08:16:59 -07:00
|
|
|
raise NoResultsFound("No results found for location: " + self.location)
|
|
|
|
|
|
|
|
return result[0]
|
|
|
|
|
2023-10-04 10:07:32 -07:00
|
|
|
def handle_listing(self, listing_id: str) -> list[Property]:
|
|
|
|
query = """query Listing($listing_id: ID!) {
|
|
|
|
listing(id: $listing_id) {
|
|
|
|
source {
|
|
|
|
id
|
|
|
|
listing_id
|
|
|
|
}
|
|
|
|
address {
|
|
|
|
street_number
|
|
|
|
street_name
|
|
|
|
street_suffix
|
|
|
|
unit
|
|
|
|
city
|
|
|
|
state_code
|
|
|
|
postal_code
|
|
|
|
location {
|
|
|
|
coordinate {
|
|
|
|
lat
|
|
|
|
lon
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
basic {
|
|
|
|
sqft
|
|
|
|
beds
|
|
|
|
baths_full
|
|
|
|
baths_half
|
|
|
|
lot_sqft
|
|
|
|
sold_price
|
|
|
|
sold_price
|
|
|
|
type
|
|
|
|
price
|
|
|
|
status
|
|
|
|
sold_date
|
|
|
|
list_date
|
|
|
|
}
|
|
|
|
details {
|
|
|
|
year_built
|
|
|
|
stories
|
|
|
|
garage
|
|
|
|
permalink
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
|
|
|
|
variables = {"listing_id": listing_id}
|
|
|
|
payload = {
|
|
|
|
"query": query,
|
|
|
|
"variables": variables,
|
|
|
|
}
|
|
|
|
|
|
|
|
response = self.session.post(self.SEARCH_GQL_URL, json=payload)
|
|
|
|
response_json = response.json()
|
|
|
|
|
|
|
|
property_info = response_json["data"]["listing"]
|
|
|
|
|
|
|
|
mls = (
|
|
|
|
property_info["source"].get("id")
|
|
|
|
if "source" in property_info and isinstance(property_info["source"], dict)
|
|
|
|
else None
|
|
|
|
)
|
|
|
|
|
|
|
|
able_to_get_lat_long = (
|
|
|
|
property_info
|
|
|
|
and property_info.get("address")
|
|
|
|
and property_info["address"].get("location")
|
|
|
|
and property_info["address"]["location"].get("coordinate")
|
|
|
|
)
|
|
|
|
|
|
|
|
listing = Property(
|
|
|
|
mls=mls,
|
|
|
|
mls_id=property_info["source"].get("listing_id")
|
|
|
|
if "source" in property_info and isinstance(property_info["source"], dict)
|
|
|
|
else None,
|
|
|
|
property_url=f"{self.PROPERTY_URL}{property_info['details']['permalink']}",
|
|
|
|
status=property_info["basic"]["status"].upper(),
|
|
|
|
list_price=property_info["basic"]["price"],
|
|
|
|
list_date=property_info["basic"]["list_date"].split("T")[0]
|
|
|
|
if property_info["basic"].get("list_date")
|
|
|
|
else None,
|
|
|
|
prc_sqft=property_info["basic"].get("price") / property_info["basic"].get("sqft")
|
|
|
|
if property_info["basic"].get("price") and property_info["basic"].get("sqft")
|
|
|
|
else None,
|
|
|
|
last_sold_date=property_info["basic"]["sold_date"].split("T")[0]
|
|
|
|
if property_info["basic"].get("sold_date")
|
|
|
|
else None,
|
|
|
|
latitude=property_info["address"]["location"]["coordinate"].get("lat")
|
|
|
|
if able_to_get_lat_long
|
|
|
|
else None,
|
|
|
|
longitude=property_info["address"]["location"]["coordinate"].get("lon")
|
|
|
|
if able_to_get_lat_long
|
|
|
|
else None,
|
|
|
|
address=self._parse_address(property_info, search_type="handle_listing"),
|
|
|
|
description=Description(
|
|
|
|
style=property_info["basic"].get("type", "").upper(),
|
|
|
|
beds=property_info["basic"].get("beds"),
|
|
|
|
baths_full=property_info["basic"].get("baths_full"),
|
|
|
|
baths_half=property_info["basic"].get("baths_half"),
|
|
|
|
sqft=property_info["basic"].get("sqft"),
|
|
|
|
lot_sqft=property_info["basic"].get("lot_sqft"),
|
|
|
|
sold_price=property_info["basic"].get("sold_price"),
|
|
|
|
year_built=property_info["details"].get("year_built"),
|
|
|
|
garage=property_info["details"].get("garage"),
|
|
|
|
stories=property_info["details"].get("stories"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return [listing]
|
|
|
|
|
|
|
|
def get_latest_listing_id(self, property_id: str) -> str | None:
|
|
|
|
query = """query Property($property_id: ID!) {
|
|
|
|
property(id: $property_id) {
|
|
|
|
listings {
|
|
|
|
listing_id
|
|
|
|
primary
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
variables = {"property_id": property_id}
|
|
|
|
payload = {
|
|
|
|
"query": query,
|
|
|
|
"variables": variables,
|
|
|
|
}
|
|
|
|
|
|
|
|
response = self.session.post(self.SEARCH_GQL_URL, json=payload)
|
|
|
|
response_json = response.json()
|
|
|
|
|
|
|
|
property_info = response_json["data"]["property"]
|
|
|
|
if property_info["listings"] is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
primary_listing = next((listing for listing in property_info["listings"] if listing["primary"]), None)
|
|
|
|
if primary_listing:
|
|
|
|
return primary_listing["listing_id"]
|
|
|
|
else:
|
|
|
|
return property_info["listings"][0]["listing_id"]
|
|
|
|
|
2023-09-18 08:16:59 -07:00
|
|
|
def handle_address(self, property_id: str) -> list[Property]:
|
2023-09-19 19:13:20 -07:00
|
|
|
"""
|
|
|
|
Handles a specific address & returns one property
|
|
|
|
"""
|
2023-09-18 08:16:59 -07:00
|
|
|
query = """query Property($property_id: ID!) {
|
|
|
|
property(id: $property_id) {
|
|
|
|
property_id
|
|
|
|
details {
|
|
|
|
date_updated
|
|
|
|
garage
|
|
|
|
permalink
|
|
|
|
year_built
|
|
|
|
stories
|
|
|
|
}
|
|
|
|
address {
|
|
|
|
street_number
|
2023-10-04 06:58:55 -07:00
|
|
|
street_name
|
2023-09-18 08:16:59 -07:00
|
|
|
street_suffix
|
|
|
|
unit
|
2023-10-04 06:58:55 -07:00
|
|
|
city
|
|
|
|
state_code
|
|
|
|
postal_code
|
|
|
|
location {
|
|
|
|
coordinate {
|
|
|
|
lat
|
|
|
|
lon
|
|
|
|
}
|
|
|
|
}
|
2023-09-18 08:16:59 -07:00
|
|
|
}
|
|
|
|
basic {
|
|
|
|
baths
|
|
|
|
beds
|
|
|
|
price
|
|
|
|
sqft
|
|
|
|
lot_sqft
|
|
|
|
type
|
|
|
|
sold_price
|
|
|
|
}
|
|
|
|
public_record {
|
|
|
|
lot_size
|
|
|
|
sqft
|
|
|
|
stories
|
|
|
|
units
|
|
|
|
year_built
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
|
2023-09-18 13:43:44 -07:00
|
|
|
variables = {"property_id": property_id}
|
2023-09-18 08:16:59 -07:00
|
|
|
|
|
|
|
payload = {
|
2023-09-18 13:43:44 -07:00
|
|
|
"query": query,
|
|
|
|
"variables": variables,
|
2023-09-18 08:16:59 -07:00
|
|
|
}
|
|
|
|
|
2023-10-04 10:07:32 -07:00
|
|
|
response = self.session.post(self.SEARCH_GQL_URL, json=payload)
|
2023-09-18 08:16:59 -07:00
|
|
|
response_json = response.json()
|
|
|
|
|
2023-09-18 13:43:44 -07:00
|
|
|
property_info = response_json["data"]["property"]
|
2023-09-18 08:16:59 -07:00
|
|
|
|
2023-09-18 13:43:44 -07:00
|
|
|
return [
|
|
|
|
Property(
|
|
|
|
mls_id=property_id,
|
2023-10-04 06:58:55 -07:00
|
|
|
property_url=f"{self.PROPERTY_URL}{property_info['details']['permalink']}",
|
2023-10-04 08:11:53 -07:00
|
|
|
address=self._parse_address(
|
|
|
|
property_info, search_type="handle_address"
|
|
|
|
),
|
|
|
|
description=self._parse_description(property_info),
|
2023-09-18 13:43:44 -07:00
|
|
|
)
|
|
|
|
]
|
2023-09-18 08:16:59 -07:00
|
|
|
|
2023-10-04 08:11:53 -07:00
|
|
|
def general_search(
|
|
|
|
self, variables: dict, search_type: str
|
|
|
|
) -> Dict[str, Union[int, list[Property]]]:
|
2023-09-19 19:13:20 -07:00
|
|
|
"""
|
|
|
|
Handles a location area & returns a list of properties
|
|
|
|
"""
|
2023-10-02 13:58:47 -07:00
|
|
|
results_query = """{
|
2023-10-03 22:21:16 -07:00
|
|
|
count
|
|
|
|
total
|
|
|
|
results {
|
|
|
|
property_id
|
|
|
|
list_date
|
|
|
|
status
|
|
|
|
last_sold_price
|
|
|
|
last_sold_date
|
2023-10-04 06:58:55 -07:00
|
|
|
list_price
|
|
|
|
price_per_sqft
|
2023-10-03 22:21:16 -07:00
|
|
|
description {
|
2023-10-04 06:58:55 -07:00
|
|
|
sqft
|
|
|
|
beds
|
2023-10-03 22:21:16 -07:00
|
|
|
baths_full
|
|
|
|
baths_half
|
|
|
|
lot_sqft
|
|
|
|
sold_price
|
|
|
|
year_built
|
|
|
|
garage
|
|
|
|
sold_price
|
|
|
|
type
|
|
|
|
name
|
|
|
|
stories
|
|
|
|
}
|
|
|
|
source {
|
|
|
|
id
|
|
|
|
listing_id
|
2023-10-04 06:58:55 -07:00
|
|
|
}
|
|
|
|
hoa {
|
|
|
|
fee
|
2023-10-03 22:21:16 -07:00
|
|
|
}
|
|
|
|
location {
|
|
|
|
address {
|
2023-10-04 06:58:55 -07:00
|
|
|
street_number
|
|
|
|
street_name
|
|
|
|
street_suffix
|
|
|
|
unit
|
2023-10-03 22:21:16 -07:00
|
|
|
city
|
|
|
|
state_code
|
2023-10-04 06:58:55 -07:00
|
|
|
postal_code
|
2023-10-03 22:21:16 -07:00
|
|
|
coordinate {
|
|
|
|
lon
|
|
|
|
lat
|
|
|
|
}
|
|
|
|
}
|
|
|
|
neighborhoods {
|
2023-10-04 06:58:55 -07:00
|
|
|
name
|
2023-10-03 22:21:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-02 13:58:47 -07:00
|
|
|
}
|
2023-10-03 22:21:16 -07:00
|
|
|
}"""
|
2023-10-02 13:58:47 -07:00
|
|
|
|
2023-10-04 08:11:53 -07:00
|
|
|
date_param = (
|
|
|
|
'sold_date: { min: "$today-%sD" }' % self.last_x_days
|
|
|
|
if self.listing_type == ListingType.SOLD and self.last_x_days
|
|
|
|
else (
|
|
|
|
'list_date: { min: "$today-%sD" }' % self.last_x_days
|
|
|
|
if self.last_x_days
|
|
|
|
else ""
|
|
|
|
)
|
|
|
|
)
|
2023-10-04 10:07:32 -07:00
|
|
|
|
2023-10-04 08:11:53 -07:00
|
|
|
sort_param = (
|
|
|
|
"sort: [{ field: sold_date, direction: desc }]"
|
|
|
|
if self.listing_type == ListingType.SOLD
|
|
|
|
else "sort: [{ field: list_date, direction: desc }]"
|
|
|
|
)
|
2023-10-03 15:05:17 -07:00
|
|
|
|
2023-10-04 18:25:01 -07:00
|
|
|
pending_or_contingent_param = "or_filters: { contingent: true, pending: true }" if self.pending_or_contingent else ""
|
|
|
|
|
|
|
|
if search_type == "comps": #: comps search, came from an address
|
2023-10-04 08:11:53 -07:00
|
|
|
query = """query Property_search(
|
2023-10-04 06:58:55 -07:00
|
|
|
$coordinates: [Float]!
|
|
|
|
$radius: String!
|
|
|
|
$offset: Int!,
|
|
|
|
) {
|
|
|
|
property_search(
|
|
|
|
query: {
|
|
|
|
nearby: {
|
|
|
|
coordinates: $coordinates
|
|
|
|
radius: $radius
|
|
|
|
}
|
|
|
|
status: %s
|
|
|
|
%s
|
|
|
|
}
|
|
|
|
%s
|
|
|
|
limit: 200
|
|
|
|
offset: $offset
|
|
|
|
) %s""" % (
|
2023-10-04 08:11:53 -07:00
|
|
|
self.listing_type.value.lower(),
|
|
|
|
date_param,
|
|
|
|
sort_param,
|
|
|
|
results_query,
|
2023-10-04 06:58:55 -07:00
|
|
|
)
|
2023-10-04 18:25:01 -07:00
|
|
|
elif search_type == "area": #: general search, came from a general location
|
2023-10-04 08:11:53 -07:00
|
|
|
query = """query Home_search(
|
2023-10-02 13:58:47 -07:00
|
|
|
$city: String,
|
|
|
|
$county: [String],
|
|
|
|
$state_code: String,
|
|
|
|
$postal_code: String
|
|
|
|
$offset: Int,
|
2023-09-18 08:16:59 -07:00
|
|
|
) {
|
2023-10-02 13:58:47 -07:00
|
|
|
home_search(
|
|
|
|
query: {
|
|
|
|
city: $city
|
|
|
|
county: $county
|
|
|
|
postal_code: $postal_code
|
|
|
|
state_code: $state_code
|
|
|
|
status: %s
|
2023-10-03 15:05:17 -07:00
|
|
|
%s
|
2023-10-04 18:25:01 -07:00
|
|
|
%s
|
2023-09-18 08:16:59 -07:00
|
|
|
}
|
2023-10-04 06:58:55 -07:00
|
|
|
%s
|
2023-10-02 13:58:47 -07:00
|
|
|
limit: 200
|
|
|
|
offset: $offset
|
2023-10-04 08:11:53 -07:00
|
|
|
) %s""" % (
|
|
|
|
self.listing_type.value.lower(),
|
|
|
|
date_param,
|
2023-10-04 18:25:01 -07:00
|
|
|
pending_or_contingent_param,
|
2023-10-04 08:11:53 -07:00
|
|
|
sort_param,
|
|
|
|
results_query,
|
2023-10-03 15:05:17 -07:00
|
|
|
)
|
2023-10-04 18:25:01 -07:00
|
|
|
else: #: general search, came from an address
|
2023-10-04 10:07:32 -07:00
|
|
|
query = (
|
|
|
|
"""query Property_search(
|
|
|
|
$property_id: [ID]!
|
|
|
|
$offset: Int!,
|
|
|
|
) {
|
|
|
|
property_search(
|
|
|
|
query: {
|
|
|
|
property_id: $property_id
|
|
|
|
}
|
|
|
|
limit: 1
|
|
|
|
offset: $offset
|
|
|
|
) %s""" % results_query)
|
2023-09-18 08:16:59 -07:00
|
|
|
|
|
|
|
payload = {
|
2023-09-18 13:43:44 -07:00
|
|
|
"query": query,
|
|
|
|
"variables": variables,
|
2023-09-18 08:16:59 -07:00
|
|
|
}
|
|
|
|
|
2023-10-04 10:07:32 -07:00
|
|
|
response = self.session.post(self.SEARCH_GQL_URL, json=payload)
|
2023-09-18 13:43:44 -07:00
|
|
|
response.raise_for_status()
|
2023-09-18 08:16:59 -07:00
|
|
|
response_json = response.json()
|
2023-10-04 10:07:32 -07:00
|
|
|
search_key = "home_search" if search_type == "area" else "property_search"
|
2023-09-18 08:16:59 -07:00
|
|
|
|
|
|
|
properties: list[Property] = []
|
|
|
|
|
2023-09-18 13:43:44 -07:00
|
|
|
if (
|
2023-10-04 08:11:53 -07:00
|
|
|
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]
|
2023-09-18 13:43:44 -07:00
|
|
|
):
|
2023-10-04 06:58:55 -07:00
|
|
|
return {"total": 0, "properties": []}
|
2023-09-18 13:43:44 -07:00
|
|
|
|
2023-10-02 13:58:47 -07:00
|
|
|
for result in response_json["data"][search_key]["results"]:
|
2023-09-19 19:13:20 -07:00
|
|
|
self.counter += 1
|
2023-10-03 22:21:16 -07:00
|
|
|
mls = (
|
|
|
|
result["source"].get("id")
|
|
|
|
if "source" in result and isinstance(result["source"], dict)
|
|
|
|
else None
|
|
|
|
)
|
|
|
|
|
2023-10-04 08:11:53 -07:00
|
|
|
if not mls and self.mls_only:
|
2023-10-03 22:21:16 -07:00
|
|
|
continue
|
|
|
|
|
2023-10-04 08:11:53 -07:00
|
|
|
able_to_get_lat_long = (
|
|
|
|
result
|
|
|
|
and result.get("location")
|
|
|
|
and result["location"].get("address")
|
|
|
|
and result["location"]["address"].get("coordinate")
|
|
|
|
)
|
2023-10-03 22:21:16 -07:00
|
|
|
|
2023-09-18 08:16:59 -07:00
|
|
|
realty_property = Property(
|
2023-10-03 22:21:16 -07:00
|
|
|
mls=mls,
|
2023-10-04 08:11:53 -07:00
|
|
|
mls_id=result["source"].get("listing_id")
|
|
|
|
if "source" in result and isinstance(result["source"], dict)
|
|
|
|
else None,
|
2023-10-04 06:58:55 -07:00
|
|
|
property_url=f"{self.PROPERTY_URL}{result['property_id']}",
|
2023-10-03 22:21:16 -07:00
|
|
|
status=result["status"].upper(),
|
|
|
|
list_price=result["list_price"],
|
2023-10-04 08:11:53 -07:00
|
|
|
list_date=result["list_date"].split("T")[0]
|
|
|
|
if result.get("list_date")
|
|
|
|
else None,
|
2023-10-04 06:58:55 -07:00
|
|
|
prc_sqft=result.get("price_per_sqft"),
|
|
|
|
last_sold_date=result.get("last_sold_date"),
|
2023-10-04 08:11:53 -07:00
|
|
|
hoa_fee=result["hoa"]["fee"]
|
|
|
|
if result.get("hoa") and isinstance(result["hoa"], dict)
|
|
|
|
else None,
|
|
|
|
latitude=result["location"]["address"]["coordinate"].get("lat")
|
|
|
|
if able_to_get_lat_long
|
|
|
|
else None,
|
|
|
|
longitude=result["location"]["address"]["coordinate"].get("lon")
|
|
|
|
if able_to_get_lat_long
|
|
|
|
else None,
|
2023-10-04 06:58:55 -07:00
|
|
|
address=self._parse_address(result, search_type="general_search"),
|
2023-10-04 18:44:47 -07:00
|
|
|
#: neighborhoods=self._parse_neighborhoods(result),
|
2023-10-04 08:11:53 -07:00
|
|
|
description=self._parse_description(result),
|
2023-09-18 08:16:59 -07:00
|
|
|
)
|
|
|
|
properties.append(realty_property)
|
|
|
|
|
2023-10-04 06:58:55 -07:00
|
|
|
return {
|
|
|
|
"total": response_json["data"][search_key]["total"],
|
|
|
|
"properties": properties,
|
|
|
|
}
|
2023-09-16 13:39:03 -07:00
|
|
|
|
2023-09-15 20:58:54 -07:00
|
|
|
def search(self):
|
|
|
|
location_info = self.handle_location()
|
2023-09-17 13:06:31 -07:00
|
|
|
location_type = location_info["area_type"]
|
2023-09-16 13:39:03 -07:00
|
|
|
|
2023-10-03 23:33:53 -07:00
|
|
|
search_variables = {
|
2023-10-04 06:58:55 -07:00
|
|
|
"offset": 0,
|
2023-10-03 23:33:53 -07:00
|
|
|
}
|
|
|
|
|
2023-10-04 10:07:32 -07:00
|
|
|
search_type = "comps" if self.radius and location_type == "address" else "address" if location_type == "address" and not self.radius else "area"
|
2023-10-04 06:58:55 -07:00
|
|
|
if location_type == "address":
|
2023-10-04 08:11:53 -07:00
|
|
|
if not self.radius: #: single address search, non comps
|
2023-10-04 06:58:55 -07:00
|
|
|
property_id = location_info["mpr_id"]
|
|
|
|
search_variables |= {"property_id": property_id}
|
2023-10-04 10:07:32 -07:00
|
|
|
|
|
|
|
gql_results = self.general_search(search_variables, search_type=search_type)
|
|
|
|
if gql_results["total"] == 0:
|
|
|
|
listing_id = self.get_latest_listing_id(property_id)
|
|
|
|
if listing_id is None:
|
|
|
|
return self.handle_address(property_id)
|
|
|
|
else:
|
|
|
|
return self.handle_listing(listing_id)
|
|
|
|
else:
|
|
|
|
return gql_results["properties"]
|
2023-10-04 06:58:55 -07:00
|
|
|
|
2023-10-04 08:11:53 -07:00
|
|
|
else: #: general search, comps (radius)
|
2023-10-04 06:58:55 -07:00
|
|
|
coordinates = list(location_info["centroid"].values())
|
|
|
|
search_variables |= {
|
|
|
|
"coordinates": coordinates,
|
|
|
|
"radius": "{}mi".format(self.radius),
|
|
|
|
}
|
|
|
|
|
|
|
|
else: #: general search, location
|
|
|
|
search_variables |= {
|
2023-10-02 13:58:47 -07:00
|
|
|
"city": location_info.get("city"),
|
|
|
|
"county": location_info.get("county"),
|
|
|
|
"state_code": location_info.get("state_code"),
|
|
|
|
"postal_code": location_info.get("postal_code"),
|
|
|
|
}
|
|
|
|
|
2023-10-04 06:58:55 -07:00
|
|
|
result = self.general_search(search_variables, search_type=search_type)
|
|
|
|
total = result["total"]
|
|
|
|
homes = result["properties"]
|
2023-09-18 08:16:59 -07:00
|
|
|
|
|
|
|
with ThreadPoolExecutor(max_workers=10) as executor:
|
|
|
|
futures = [
|
|
|
|
executor.submit(
|
2023-10-03 23:33:53 -07:00
|
|
|
self.general_search,
|
2023-09-18 13:43:44 -07:00
|
|
|
variables=search_variables | {"offset": i},
|
2023-10-03 23:33:53 -07:00
|
|
|
search_type=search_type,
|
2023-09-18 13:43:44 -07:00
|
|
|
)
|
2023-10-04 06:58:55 -07:00
|
|
|
for i in range(200, min(total, 10000), 200)
|
2023-09-18 08:16:59 -07:00
|
|
|
]
|
|
|
|
|
|
|
|
for future in as_completed(futures):
|
2023-10-04 06:58:55 -07:00
|
|
|
homes.extend(future.result()["properties"])
|
2023-09-18 08:16:59 -07:00
|
|
|
|
|
|
|
return homes
|
2023-10-04 06:58:55 -07:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _parse_neighborhoods(result: dict) -> Optional[str]:
|
|
|
|
neighborhoods_list = []
|
|
|
|
neighborhoods = result["location"].get("neighborhoods", [])
|
|
|
|
|
|
|
|
if neighborhoods:
|
|
|
|
for neighborhood in neighborhoods:
|
|
|
|
name = neighborhood.get("name")
|
|
|
|
if name:
|
|
|
|
neighborhoods_list.append(name)
|
|
|
|
|
|
|
|
return ", ".join(neighborhoods_list) if neighborhoods_list else None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _parse_address(result: dict, search_type):
|
|
|
|
if search_type == "general_search":
|
|
|
|
return Address(
|
|
|
|
street=f"{result['location']['address']['street_number']} {result['location']['address']['street_name']} {result['location']['address']['street_suffix']}",
|
|
|
|
unit=result["location"]["address"]["unit"],
|
|
|
|
city=result["location"]["address"]["city"],
|
|
|
|
state=result["location"]["address"]["state_code"],
|
|
|
|
zip=result["location"]["address"]["postal_code"],
|
|
|
|
)
|
|
|
|
return Address(
|
|
|
|
street=f"{result['address']['street_number']} {result['address']['street_name']} {result['address']['street_suffix']}",
|
2023-10-04 08:11:53 -07:00
|
|
|
unit=result["address"]["unit"],
|
|
|
|
city=result["address"]["city"],
|
|
|
|
state=result["address"]["state_code"],
|
|
|
|
zip=result["address"]["postal_code"],
|
2023-10-04 06:58:55 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _parse_description(result: dict) -> Description:
|
|
|
|
description_data = result.get("description", {})
|
|
|
|
return Description(
|
|
|
|
style=description_data.get("type", "").upper(),
|
|
|
|
beds=description_data.get("beds"),
|
|
|
|
baths_full=description_data.get("baths_full"),
|
|
|
|
baths_half=description_data.get("baths_half"),
|
|
|
|
sqft=description_data.get("sqft"),
|
|
|
|
lot_sqft=description_data.get("lot_sqft"),
|
|
|
|
sold_price=description_data.get("sold_price"),
|
|
|
|
year_built=description_data.get("year_built"),
|
|
|
|
garage=description_data.get("garage"),
|
|
|
|
stories=description_data.get("stories"),
|
2023-10-04 08:11:53 -07:00
|
|
|
)
|