File size: 15,681 Bytes
e67043b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import json
from datetime import date, datetime, timedelta
import os
from ..tool import Tool

from typing import Optional, Dict


def build_tool(config) -> Tool:
    tool = Tool(
        "Real Estate Information",
        "Look up rental and housing information",
        name_for_model="Zillow",
        description_for_model="Plugin for look up real estate information",
        logo_url="https://your-app-url.com/.well-known/logo.png",
        contact_email="[email protected]",
        legal_info_url="[email protected]",
    )

    BASE_URL = "https://zillow-com1.p.rapidapi.com"
    KEY = config["subscription_key"]
    HEADERS = {"X-RapidAPI-Key": KEY, "X-RapidAPI-Host": "zillow-com1.p.rapidapi.com"}

    @tool.get("/search_properties")
    def search_properties(
        location: str,
        page: Optional[int] = None,
        status_type: Optional[str] = None,
        home_type: Optional[str] = None,
        sort: Optional[str] = None,
        polygon: Optional[str] = None,
        minPrice: Optional[float] = None,
        maxPrice: Optional[float] = None,
        rentMinPrice: Optional[float] = None,
        rentMaxPrice: Optional[float] = None,
        bathsMin: Optional[int] = None,
        bathsMax: Optional[int] = None,
        bedsMin: Optional[int] = None,
        bedsMax: Optional[int] = None,
        sqftMin: Optional[int] = None,
        sqftMax: Optional[int] = None,
        buildYearMin: Optional[int] = None,
        buildYearMax: Optional[int] = None,
        daysOn: Optional[str] = None,
        soldInLast: Optional[str] = None,
        isBasementFinished: Optional[int] = None,
        isBasementUnfinished: Optional[int] = None,
        isPendingUnderContract: Optional[int] = None,
        isAcceptingBackupOffers: Optional[int] = None,
        isComingSoon: Optional[bool] = None,
        otherListings: Optional[int] = None,
        isNewConstruction: Optional[bool] = None,
        keywords: Optional[str] = None,
        lotSizeMin: Optional[str] = None,
        lotSizeMax: Optional[str] = None,
        saleByAgent: Optional[str] = None,
        saleByOwner: Optional[str] = None,
        isForSaleForeclosure: Optional[bool] = None,
        isWaterfront: Optional[bool] = None,
        hasPool: Optional[bool] = None,
        hasAirConditioning: Optional[bool] = None,
        isCityView: Optional[bool] = None,
        isMountainView: Optional[bool] = None,
        isWaterView: Optional[bool] = None,
        isParkView: Optional[bool] = None,
        isOpenHousesOnly: Optional[bool] = None,
        is3dHome: Optional[bool] = None,
        coordinates: Optional[str] = None,
        hoa: Optional[float] = None,
        includeHomesWithNoHoaData: Optional[bool] = None,
        isAuction: Optional[bool] = None,
    ):
        """
        Function to search properties based on a set of parameters.

        Parameters:
        location (str): Location details, address, county, neighborhood or Zip code.
        page (int): Page number if at the previous response totalPages > 1.
        status_type (str): Status type of the property.
        home_type (str): Type of the home.
        sort (str): Sorting option for the results.
        polygon (str): Polygon coordinates for the search.
        minPrice (float): Minimum price of the property.
        maxPrice (float): Maximum price of the property.
        rentMinPrice (float): Minimum rent price of the property.
        rentMaxPrice (float): Maximum rent price of the property.
        bathsMin (int): Minimum number of bathrooms.
        bathsMax (int): Maximum number of bathrooms.
        bedsMin (int): Minimum number of bedrooms.
        bedsMax (int): Maximum number of bedrooms.
        sqftMin (int): Minimum square feet area of the property.
        sqftMax (int): Maximum square feet area of the property.
         buildYearMin (int): Minimum year of construction of the property.
        buildYearMax (int): Maximum year of construction of the property.
        daysOn (str): Days on Zillow.
        soldInLast (str): Property sold in the last days.
        isBasementFinished (int): Whether the basement is finished or not.
        isBasementUnfinished (int): Whether the basement is unfinished or not.
        isPendingUnderContract (int): Whether the property is under contract or not.
        isAcceptingBackupOffers (int): Whether the property is accepting backup offers or not.
        isComingSoon (bool): Whether the property is coming soon or not.
        otherListings (int): Whether to include other listings or not.
        isNewConstruction (bool): Whether the property is new construction or not.
        keywords (str): Keywords to filter the search.
        lotSizeMin (str): Minimum lot size of the property.
        lotSizeMax (str): Maximum lot size of the property.
        saleByAgent (str): Whether the property is for sale by agent or not.
        saleByOwner (str): Whether the property is for sale by owner or not.
        isForSaleForeclosure (bool): Whether the property is for sale by foreclosure or not.
        isWaterfront (bool): Whether the property is a waterfront property or not.
        hasPool (bool): Whether the property has a pool or not.
        hasAirConditioning (bool): Whether the property has air conditioning or not.
        isCityView (bool): Whether the property has a city view or not.
        isMountainView (bool): Whether the property has a mountain view or not.
        isWaterView (bool): Whether the property has a water view or not.
        isParkView (bool): Whether the property has a park view or not.
        isOpenHousesOnly (bool): Whether to only include properties with open houses.
        is3dHome (bool): Whether the property has a 3D home tour.
        coordinates (str): Coordinates of the location for the search.
        hoa (float): Maximum HOA.
        includeHomesWithNoHoaData (bool): Whether to include homes with no HOA data.
        isAuction (bool): Whether the property is for auction.

        Returns:
        A response object from the Zillow API.
        """
        params = {
            "location": location,
            "page": page,
            "status_type": status_type,
            "home_type": home_type,
            "sort": sort,
            "polygon": polygon,
            "minPrice": minPrice,
            "maxPrice": maxPrice,
            "rentMinPrice": rentMinPrice,
            "rentMaxPrice": rentMaxPrice,
            "bathsMin": bathsMin,
            "bathsMax": bathsMax,
            "bedsMin": bedsMin,
            "bedsMax": bedsMax,
            "sqftMin": sqftMin,
            "sqftMax": sqftMax,
            "buildYearMin": buildYearMin,
            "buildYearMax": buildYearMax,
            "daysOn": daysOn,
            "soldInLast": soldInLast,
            "isBasementFinished": isBasementFinished,
            "isBasementUnfinished": isBasementUnfinished,
            "isPendingUnderContract": isPendingUnderContract,
            "isAcceptingBackupOffers": isAcceptingBackupOffers,
            "isComingSoon": isComingSoon,
            "otherListings": otherListings,
            "isNewConstruction": isNewConstruction,
            "keywords": keywords,
            "lotSizeMin": lotSizeMin,
            "lotSizeMax": lotSizeMax,
            "saleByAgent": saleByAgent,
            "saleByOwner": saleByOwner,
            "isForSaleForeclosure": isForSaleForeclosure,
            "isWaterfront": isWaterfront,
            "hasPool": hasPool,
            "hasAirConditioning": hasAirConditioning,
            "isCityView": isCityView,
            "isMountainView": isMountainView,
            "isWaterView": isWaterView,
            "isParkView": isParkView,
            "isOpenHousesOnly": isOpenHousesOnly,
            "is3dHome": is3dHome,
            "coordinates": coordinates,
            "hoa": hoa,
            "includeHomesWithNoHoaData": includeHomesWithNoHoaData,
            "isAuction": isAuction,
        }

        # Remove parameters that are None
        params = {k: v for k, v in params.items() if v is not None}
        url = BASE_URL + "/propertyExtendedSearch"
        # Send GET request to Zillow API endpoint
        response = requests.get(url, headers=HEADERS, params=params)

        return response.json()

    @tool.get("/rent_estimate")
    def rent_estimate(
        propertyType: str,
        address: Optional[str] = None,
        long: Optional[float] = None,
        lat: Optional[float] = None,
        d: Optional[float] = None,
        beds: Optional[int] = None,
        baths: Optional[int] = None,
        sqftMin: Optional[int] = None,
        sqftMax: Optional[int] = None,
    ):
        """
        Estimate rent for a property.

        Args:
        propertyType (str): Type of the property. This is a required parameter. Options are 'SingleFamily', 'Condo', 'MultiFamily', 'Townhouse', 'Apartment'
        address (str, optional): Address of the property.
        long (float, optional): Longitude of the property.
        lat (float, optional): Latitude of the property.
        d (float, optional): Diameter in miles. The max and low values are 0.5 and 0.05 respectively. The default value is 0.5.
        beds (int, optional): Number of bedrooms in the property.
        baths (int, optional): Number of bathrooms in the property.
        sqftMin (int, optional): Minimum square footage of the property.
        sqftMax (int, optional): Maximum square footage of the property.

        Returns:
        A response object from the Zillow API with rent estimate and comparable rentals information.
        """
        params = {
            "propertyType": propertyType,
            "address": address,
            "long": long,
            "lat": lat,
            "d": d,
            "beds": beds,
            "baths": baths,
            "sqftMin": sqftMin,
            "sqftMax": sqftMax,
        }

        # Remove parameters that are None
        params = {k: v for k, v in params.items() if v is not None}
        url = BASE_URL + "/rentEstimate"
        # Send GET request to Zillow API endpoint
        response = requests.get(url, headers=HEADERS, params=params)

        return response.json()

    @tool.get("/zillow_property")
    def zillow_property(zpid: Optional[int] = None, property_url: Optional[str] = None):
        """
        Fetch property details and Zestimate value.

        Args:
        zpid (int, optional): Unique ID that Zillow gives to each property.
        property_url (str, optional): Full page URL of the property on Zillow.

        Returns:
        A response object from the Zillow API with property details and Zestimate value.
        """
        params = {
            "zpid": zpid,
            "property_url": property_url,
        }

        # Remove parameters that are None
        params = {k: v for k, v in params.items() if v is not None}
        url = BASE_URL + "/property"
        # Send GET request to Zillow API endpoint
        response = requests.get(url, headers=HEADERS, params=params)

        return response.json()

    @tool.get("/property_by_coordinates")
    def property_by_coordinates(
        long: float,
        lat: float,
        d: Optional[float] = None,
        includeSold: Optional[bool] = None,
    ):
        """
        Search property by coordinates.

        Args:
        long (float): Longitude of the property. This is a required parameter.
        lat (float): Latitude of the property. This is a required parameter.
        d (float, optional): Diameter in miles. The max and low values are 0.5 and 0.05 respectively. The default value is 0.1.
        includeSold (bool, optional): Include sold properties in the results. True or 1 to include (default), False or 0 to exclude.

        Returns:
        A response object from the Zillow API with an array of zpid.
        """
        params = {
            "long": long,
            "lat": lat,
            "d": d,
            "includeSold": includeSold,
        }

        # Remove parameters that are None
        params = {k: v for k, v in params.items() if v is not None}
        url = BASE_URL + "/propertyByCoordinates"
        # Send GET request to Zillow API endpoint
        response = requests.get(url, headers=HEADERS, params=params)

        return response.json()

    @tool.get("/property_by_mls")
    def property_by_mls(mls: str):
        """
        Search property by MLS #.

        Args:
        mls (str): MLS # of the property. This is a required parameter.

        Returns:
        A response object from the Zillow API with an array of zpid.
        """
        params = {
            "mls": mls,
        }
        url = BASE_URL + "/propertyByMls"
        # Send GET request to Zillow API endpoint
        response = requests.get(url, headers=HEADERS, params=params)

        return response.json()

    @tool.get("/location_suggestions")
    def location_suggestions(q: str):
        """
        Search location by name.

        Args:
        q (str): Name of the state, county, neighborhood, city, or street. This is a required parameter.

        Returns:
        A response object from the Zillow API with suggested locations.
        """
        params = {
            "q": q,
        }
        url = BASE_URL + "/locationSuggestions"
        # Send GET request to Zillow API endpoint
        response = requests.get(url, headers=HEADERS, params=params)

        return response.json()

    @tool.get("/similar_property")
    def similar_property(
        zpid: Optional[int] = None, property_url: Optional[str] = None
    ):
        """
        Get similar properties for sale. Either zpid or property_url is a required parameter.

        Args:
        zpid (int, optional): Zillow's unique identifier for a property. This can be obtained from /propertyExtendedSearch
            or /propertyByCoordinates endpoints, or extracted from a full URL.
        property_url (str, optional): Full page URL of the property.

        Returns:
        A response object from the Zillow API with similar properties for sale.
        """
        if not zpid and not property_url:
            raise ValueError("Either zpid or property_url must be provided.")

        params = {}
        if zpid:
            params["zpid"] = zpid
        if property_url:
            params["property_url"] = property_url
        url = BASE_URL + "/similarProperty"
        # Send GET request to Zillow API endpoint
        response = requests.get(url, headers=HEADERS, params=params)

        return response.json()

    @tool.get("/similar_for_rent")
    def similar_for_rent(
        zpid: Optional[int] = None, property_url: Optional[str] = None
    ):
        """
        Get similar properties for rent. Either zpid or property_url is a required parameter.

        Args:
        zpid (int, optional): Zillow's unique identifier for a property. This can be obtained from /propertyExtendedSearch
            or /propertyByCoordinates endpoints, or extracted from a full URL.
        property_url (str, optional): Full page URL of the property.

        Returns:
        A response object from the Zillow API with similar properties for rent.
        """
        if not zpid and not property_url:
            raise ValueError("Either zpid or property_url must be provided.")

        params = {}
        if zpid:
            params["zpid"] = zpid
        if property_url:
            params["property_url"] = property_url
        url = BASE_URL + "/similarForRent"
        # Send GET request to Zillow API endpoint
        response = requests.get(url, headers=HEADERS, params=params)

        return response.json()

    return tool