-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredfin_api.py
More file actions
636 lines (550 loc) · 22.3 KB
/
redfin_api.py
File metadata and controls
636 lines (550 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
"""
Redfin Scraper API - Python Client
Powered by RealtyAPI.io
A comprehensive Python wrapper for the Redfin Scraper API.
Supports property search, property details, agent info, AVM estimates,
walk scores, flood info, and more.
"""
import requests
from config import BASE_URL, get_headers
class RedfinAPI:
"""
Redfin Scraper API client for real estate data extraction.
Provides access to Redfin property data, listings, agent info,
market insights, and valuations via the RealtyAPI.io platform.
Get your API key at: https://www.realtyapi.io
"""
def __init__(self, api_key):
self.api_key = api_key
self.headers = get_headers(api_key)
self.base_url = BASE_URL
def _get(self, endpoint, params=None):
"""Make a GET request and return the JSON response."""
url = f"{self.base_url}{endpoint}"
if params:
params = {k: v for k, v in params.items() if v is not None}
try:
response = requests.get(url, headers=self.headers, params=params, timeout=30)
response.raise_for_status()
return response.json()
except requests.exceptions.HTTPError as e:
if response.status_code == 401:
print("Error: Invalid API key. Get your key at https://www.realtyapi.io")
elif response.status_code == 429:
print("Error: Rate limit exceeded. Check your plan at https://www.realtyapi.io")
else:
print(f"HTTP Error: {e}")
return {"error": str(e), "status_code": response.status_code}
except requests.exceptions.RequestException as e:
print(f"Request Error: {e}")
return {"error": str(e)}
# ─────────────────────────────────────────────
# 1. Property Search
# ─────────────────────────────────────────────
def autocomplete(self, query, listing_status=None):
"""
Get autocomplete suggestions for a location query.
Args:
query (str): Location/query text.
Example: "New York", "Austin", "Texas"
listing_status (str): "For_Sale_or_Sold" or "For_Rent".
Sale/Sold shows standard response. Rent shows rental listings.
Returns:
dict: Autocomplete suggestions
"""
return self._get("/autocomplete", {
"query": query,
"listingStatus": listing_status,
})
def search_by_location(self, location_name, search_type=None, page=1,
result_count=None, sort_order=None,
min_price=None, max_price=None,
min_beds=None, max_beds=None, baths=None,
home_type=None, min_square_feet=None,
max_square_feet=None, min_lot_size=None,
max_lot_size=None, min_year_built=None,
max_year_built=None, status=None,
time_on_redfin=None, sold_within=None,
price_reduced=None, hoa_fees=None,
listing_type=None, keyword=None,
walk_score=None, transit_score=None,
bike_score=None, pool_types=None,
garage_spots=None, home_features=None,
great_schools_rating=None, pets=None,
location_sub_name=None, **kwargs):
"""
Search Redfin listings by location name.
Args:
location_name (str): City, state, ZIP, neighborhood, or address.
Example: "New York", "Austin, TX", "10001"
location_sub_name (str): Sub-location filter (optional)
search_type (str): For_Sale, For_Rent, or Sold
page (int): Page number for pagination
result_count (int): Results per page (1-1000, default 100)
sort_order (str): Recommended, Newest, Oldest, Most_Recently_Sold,
Price_High_to_Low, Price_Low_to_High, Square_Feet, Lot_Size,
Price_per_Sqft, Beds, Baths
min_price (str): Minimum price (e.g., "100000")
max_price (str): Maximum price (e.g., "500000")
min_beds (str): Any, Studio, One, Two, Three, Four, FivePlus, SixPlus
max_beds (str): Any, Studio, One, Two, Three, Four, FivePlus, SixPlus
baths (str): Any, OnePlus, OnePointFivePlus, Two, TwoPointFivePlus,
ThreePlus, FourPlus, FivePlus, SixPlus
home_type (str): Comma-separated. House, Condo, Townhouse,
Multi family, Land, Other, Mobile, Co-op, Apartment
min_square_feet (str): Minimum square footage
max_square_feet (str): Maximum square footage
min_lot_size (str): Minimum lot size in sqft
max_lot_size (str): Maximum lot size in sqft
min_year_built (int): Minimum year built (e.g., 1900)
max_year_built (str): Maximum year built (e.g., "2025")
status (str): Active_ComingSoon, Active, ComingSoon,
UnderContractOrPending, etc.
time_on_redfin (str): Any, New_listings, Less_than_3d,
Less_than_7d, Less_than_14d, Less_than_30d, etc.
sold_within (str): Last_7_Days, Last_1_Month, Last_3_Months,
Last_6_Months, Last_1_Year, Last_2_Years, Last_3_Years, Last_5_Years
price_reduced (str): None, AnyTime, Less_than_1d, Less_than_7d, etc.
hoa_fees (str): No_max, 25, 50, 100, 200, 500, 1000, etc.
listing_type (str): Comma-separated. New Constructions, By Agent,
MLS listed Foreclosures, For Sale by owner, Foreclosures
keyword (str): Free-text search keyword
walk_score (str): Any, 10plus, 20plus, ... 90plus
transit_score (str): Any, 10plus, 20plus, ... 90plus
bike_score (str): Any, 10plus, 20plus, ... 90plus
pool_types (str): None, Private, Community,
Private_or_Community, No_private_pool
garage_spots (str): No_Min, 1plus, 2plus, 3plus, 4plus, 5plus
home_features (str): Comma-separated. Air conditioning, Fireplace,
Waterfront, Guest house, Accessible home, Elevator, Green home
great_schools_rating (str): Any, 1plus, ... 9plus, 10
pets (str): For Rent only. Cats allowed, Dogs allowed,
Dogs allowed + Cats allowed
Returns:
dict: Search results with property listings
"""
params = {
"locationName": location_name,
"locationSubName": location_sub_name,
"searchType": search_type,
"page": page,
"resultCount": result_count,
"sortOrder": sort_order,
"minPrice": min_price,
"maxPrice": max_price,
"minBeds": min_beds,
"maxBeds": max_beds,
"baths": baths,
"homeType": home_type,
"minSquareFeet": min_square_feet,
"maxSquareFeet": max_square_feet,
"minLotSize": min_lot_size,
"maxLotSize": max_lot_size,
"minYearBuilt": min_year_built,
"maxYearBuilt": max_year_built,
"status": status,
"timeOnRedfin": time_on_redfin,
"soldWithin": sold_within,
"priceReduced": price_reduced,
"HOAfees": hoa_fees,
"listingType": listing_type,
"keyword": keyword,
"walkScore": walk_score,
"transitScore": transit_score,
"bikeScore": bike_score,
"poolTypes": pool_types,
"garageSpots": garage_spots,
"homeFeatures": home_features,
"greatSchoolsRating": great_schools_rating,
"pets": pets,
**kwargs,
}
return self._get("/search/bylocation", params)
def search_by_coordinates(self, latitude, longitude, radius,
search_type=None, page=1, result_count=None,
sort_order=None, min_price=None, max_price=None,
min_beds=None, max_beds=None, baths=None,
home_type=None, min_square_feet=None,
max_square_feet=None, status=None,
sold_within=None, keyword=None, **kwargs):
"""
Search Redfin listings by coordinates (circle search).
Args:
latitude (str): Latitude. Example: "40.748817"
longitude (str): Longitude. Example: "-73.985428"
radius (str): Radius in miles. Example: "1.5"
search_type (str): For_Sale, For_Rent, or Sold
page (int): Page number
result_count (int): Results per page (1-1000)
sort_order (str): Sort order option
min_price (str): Minimum price
max_price (str): Maximum price
min_beds (str): Min bedrooms
max_beds (str): Max bedrooms
baths (str): Bathroom filter
home_type (str): Comma-separated home types
min_square_feet (str): Min sqft
max_square_feet (str): Max sqft
status (str): Listing status filter
sold_within (str): Sold timeframe filter
keyword (str): Free-text keyword
**kwargs: Additional search filters
Returns:
dict: Search results with property listings
"""
params = {
"latitude": str(latitude),
"longitude": str(longitude),
"radius": str(radius),
"searchType": search_type,
"page": page,
"resultCount": result_count,
"sortOrder": sort_order,
"minPrice": min_price,
"maxPrice": max_price,
"minBeds": min_beds,
"maxBeds": max_beds,
"baths": baths,
"homeType": home_type,
"minSquareFeet": min_square_feet,
"maxSquareFeet": max_square_feet,
"status": status,
"soldWithin": sold_within,
"keyword": keyword,
**kwargs,
}
return self._get("/search/bycoordinates", params)
def search_by_polygon(self, polygon, search_type=None, page=1,
result_count=None, sort_order=None,
min_price=None, max_price=None,
min_beds=None, max_beds=None, baths=None,
home_type=None, keyword=None, **kwargs):
"""
Search Redfin listings by polygon coordinates.
Args:
polygon (str): Comma-separated lng/lat pairs defining the polygon.
Example: "-74.006 40.733,-73.981 40.748,-73.958 40.733"
search_type (str): For_Sale, For_Rent, or Sold
page (int): Page number
result_count (int): Results per page (1-1000)
sort_order (str): Sort order
min_price (str): Minimum price
max_price (str): Maximum price
min_beds (str): Min bedrooms
max_beds (str): Max bedrooms
baths (str): Bathroom filter
home_type (str): Comma-separated home types
keyword (str): Free-text keyword
**kwargs: Additional search filters
Returns:
dict: Search results
"""
params = {
"polygon": polygon,
"searchType": search_type,
"page": page,
"resultCount": result_count,
"sortOrder": sort_order,
"minPrice": min_price,
"maxPrice": max_price,
"minBeds": min_beds,
"maxBeds": max_beds,
"baths": baths,
"homeType": home_type,
"keyword": keyword,
**kwargs,
}
return self._get("/search/bypolygon", params)
def search_by_region_id(self, region_id, search_type=None, page=1,
result_count=None, sort_order=None,
min_price=None, max_price=None,
min_beds=None, max_beds=None, baths=None,
home_type=None, keyword=None, **kwargs):
"""
Search Redfin listings by region ID.
Args:
region_id (str): Redfin region identifier. Example: "4_3244"
search_type (str): For_Sale, For_Rent, or Sold
page (int): Page number
result_count (int): Results per page (1-1000)
sort_order (str): Sort order
**kwargs: Additional search filters
Returns:
dict: Search results
"""
params = {
"regionId": region_id,
"searchType": search_type,
"page": page,
"resultCount": result_count,
"sortOrder": sort_order,
"minPrice": min_price,
"maxPrice": max_price,
"minBeds": min_beds,
"maxBeds": max_beds,
"baths": baths,
"homeType": home_type,
"keyword": keyword,
**kwargs,
}
return self._get("/search/byregionid", params)
def search_by_url(self, search_url, result_count=None):
"""
Search Redfin by passing a Redfin search URL with filters.
Args:
search_url (str): Complete Redfin search URL.
Example: "https://www.redfin.com/zipcode/10002/filter/property-type=house,min-price=150k"
result_count (int): Number of results (1-350)
Returns:
dict: Search results
"""
return self._get("/search/byurl", {
"searchUrl": search_url,
"resultCount": result_count,
})
# ─────────────────────────────────────────────
# 2. Property Details
# ─────────────────────────────────────────────
def get_details_by_address(self, property_address):
"""
Get full property details by address.
Args:
property_address (str): Full property address.
Example: "166 W 22nd St Unit 1D, New York, NY 10011"
Returns:
dict: Comprehensive property details
"""
return self._get("/detailsbyaddress", {
"property_address": property_address,
})
def get_details_by_id(self, property_id, listing_id):
"""
Get full property details by property ID and listing ID.
Args:
property_id (str): Redfin property ID. Example: "20739181"
listing_id (str): Redfin listing ID. Example: "192575318"
Returns:
dict: Comprehensive property details
"""
return self._get("/detailsbyid", {
"property_id": property_id,
"listing_id": listing_id,
})
def get_details_by_url(self, property_url):
"""
Get full property details by Redfin URL.
Args:
property_url (str): Full Redfin property URL.
Example: "https://www.redfin.com/NY/Saint-Albans/10970-203rd-St-11412/home/20739181"
Returns:
dict: Comprehensive property details
"""
return self._get("/detailsbyurl", {
"property_url": property_url,
})
# ─────────────────────────────────────────────
# 3. Individual Details
# ─────────────────────────────────────────────
def get_agent_info(self, property_id, listing_id):
"""
Get listing agent details.
Args:
property_id (str): Redfin property ID
listing_id (str): Redfin listing ID
Returns:
dict: Agent information
"""
return self._get("/agentInfo", {
"property_id": property_id,
"listing_id": listing_id,
})
def get_amenities(self, property_id, listing_id):
"""
Get property amenities details.
Args:
property_id (str): Redfin property ID
listing_id (str): Redfin listing ID
Returns:
dict: Property amenities
"""
return self._get("/amenities", {
"property_id": property_id,
"listing_id": listing_id,
})
def get_avm_estimate(self, property_id, listing_id):
"""
Get Automated Valuation Model (AVM) estimate.
Args:
property_id (str): Redfin property ID
listing_id (str): Redfin listing ID
Returns:
dict: AVM estimate data
"""
return self._get("/avm", {
"property_id": property_id,
"listing_id": listing_id,
})
def get_basic_details(self, property_url):
"""
Get basic property details from a Redfin URL.
Useful for getting property_id and listing_id from a URL.
Args:
property_url (str): Full Redfin property URL.
Returns:
dict: Basic details including property_id and listing_id
"""
return self._get("/basicDetails", {
"property_url": property_url,
})
def get_customer_conversion_info(self, property_id, listing_id):
"""
Get customer conversion information data.
Args:
property_id (str): Redfin property ID
listing_id (str): Redfin listing ID
Returns:
dict: Customer conversion data
"""
return self._get("/customerConversionInfo", {
"property_id": property_id,
"listing_id": listing_id,
})
def get_flood_info(self, fips_code, apn, lat, lng):
"""
Get flood risk information for a property.
Args:
fips_code (str): FIPS county code. Example: "36081"
apn (str): Assessor's Parcel Number. Example: "4109420115"
lat (str): Latitude. Example: "40.7059662"
lng (str): Longitude. Example: "-73.754585"
Returns:
dict: Flood risk data
"""
return self._get("/floodInfo", {
"fipsCode": fips_code,
"apn": apn,
"lat": lat,
"lng": lng,
})
def get_date_picker_data(self, listing_id):
"""
Get date picker data for scheduling property tours.
Args:
listing_id (str): Redfin listing ID
Returns:
dict: Available tour dates
"""
return self._get("/getDatePickerData", {
"listing_id": listing_id,
})
def get_hot_market_info(self, property_id):
"""
Get hot market information for a property's area.
Args:
property_id (str): Redfin property ID
Returns:
dict: Market heat data
"""
return self._get("/hotMarketInfo", {
"property_id": property_id,
})
def get_insights(self, property_id, listing_id):
"""
Get property insights (below the fold data).
Args:
property_id (str): Redfin property ID
listing_id (str): Redfin listing ID
Returns:
dict: Property insights
"""
return self._get("/insights", {
"property_id": property_id,
"listing_id": listing_id,
})
def get_main_house_info(self, property_id, listing_id):
"""
Get main house information panel details.
Args:
property_id (str): Redfin property ID
listing_id (str): Redfin listing ID
Returns:
dict: Main house info panel data
"""
return self._get("/mainHouseInfoPanelInfo", {
"property_id": property_id,
"listing_id": listing_id,
})
def get_mortgage_calculator(self, property_id, listing_id):
"""
Get mortgage calculator information.
Args:
property_id (str): Redfin property ID
listing_id (str): Redfin listing ID
Returns:
dict: Mortgage calculation data
"""
return self._get("/mortgageCalculatorInfo", {
"property_id": property_id,
"listing_id": listing_id,
})
def get_overview(self, property_id, listing_id):
"""
Get property overview (above the fold data).
Args:
property_id (str): Redfin property ID
listing_id (str): Redfin listing ID
Returns:
dict: Property overview
"""
return self._get("/overview", {
"property_id": property_id,
"listing_id": listing_id,
})
def get_popularity_info(self, listing_id):
"""
Get property popularity information.
Args:
listing_id (str): Redfin listing ID
Returns:
dict: Popularity metrics
"""
return self._get("/popularityInfo", {
"listing_id": listing_id,
})
def get_price_drop_info(self, listing_id):
"""
Get price drop information for a listing.
Args:
listing_id (str): Redfin listing ID
Returns:
dict: Price drop history
"""
return self._get("/priceDropInfo", {
"listing_id": listing_id,
})
def get_tour_insights(self, property_id, listing_id):
"""
Get tour insights data.
Args:
property_id (str): Redfin property ID
listing_id (str): Redfin listing ID
Returns:
dict: Tour insight data
"""
return self._get("/tourInsights", {
"property_id": property_id,
"listing_id": listing_id,
})
def get_walk_score(self, property_id, listing_id):
"""
Get walk, transit, and bike scores.
Args:
property_id (str): Redfin property ID
listing_id (str): Redfin listing ID
Returns:
dict: Walk score, transit score, bike score
"""
return self._get("/walkScore", {
"property_id": property_id,
"listing_id": listing_id,
})