Spaces:
Runtime error
Runtime error
File size: 13,297 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 |
import requests
import json
from datetime import date, datetime, timedelta
import os
from ..tool import Tool
from typing import Optional, Dict, List
def build_tool(config) -> Tool:
tool = Tool(
"Short-term rental and housing information",
"Look up rental and housing information",
name_for_model="Airbnb",
description_for_model="Plugin for look up rental and housing information",
logo_url="https://your-app-url.com/.well-known/logo.png",
contact_email="hello@contact.com",
legal_info_url="hello@legal.com",
)
BASE_URL = "https://airbnb19.p.rapidapi.com/api/v1"
KEY = config["subscription_key"]
HEADERS = {"X-RapidAPI-Key": KEY, "X-RapidAPI-Host": "airbnb19.p.rapidapi.com"}
@tool.get("/ssearch_property")
def search_property(
_id: str,
display_name: Optional[str] = None,
total_records: Optional[str] = "10",
currency: Optional[str] = "USD",
offset: Optional[str] = None,
category: Optional[str] = None,
adults: Optional[int] = 1,
children: Optional[int] = None,
infants: Optional[int] = None,
pets: Optional[int] = None,
checkin: Optional[str] = None,
checkout: Optional[str] = None,
priceMin: Optional[int] = None,
priceMax: Optional[int] = None,
minBedrooms: Optional[int] = None,
minBeds: Optional[int] = None,
minBathrooms: Optional[int] = None,
property_type: Optional[List[str]] = None,
host_languages: Optional[List[str]] = None,
amenities: Optional[List[str]] = None,
type_of_place: Optional[List[str]] = None,
top_tier_stays: Optional[List[str]] = None,
self_check_in: Optional[bool] = None,
instant_book: Optional[bool] = None,
super_host: Optional[bool] = None,
languageId: Optional[str] = None,
) -> dict:
"""
This function takes various parameters to search properties on Airbnb.
Parameters:
api_key (str): The RapidAPI Key for Airbnb API.
id (str): The ID of the destination.
display_name (Optional[str]): The name of the destination.
total_records (Optional[str]): The number of records to be retrieved per API call.
currency (Optional[str]): The currency for the transaction.
offset (Optional[str]): The offset for the search result.
category (Optional[str]): The category of the properties.
adults (Optional[int]): The number of adults.
children (Optional[int]): The number of children.
infants (Optional[int]): The number of infants.
pets (Optional[int]): The number of pets.
checkin (Optional[str]): The check-in date.
checkout (Optional[str]): The check-out date.
priceMin (Optional[int]): The minimum price.
priceMax (Optional[int]): The maximum price.
minBedrooms (Optional[int]): The minimum number of bedrooms.
minBeds (Optional[int]): The minimum number of beds.
minBathrooms (Optional[int]): The minimum number of bathrooms.
property_type (Optional[List[str]]): The type of the property.
host_languages (Optional[List[str]]): The languages that the host can speak.
amenities (Optional[List[str]]): The amenities provided by the property.
type_of_place (Optional[List[str]]): The type of the place.
top_tier_stays (Optional[List[str]]): The list of top-tier stays.
self_check_in (Optional[bool]): If the property has self check-in feature.
instant_book (Optional[bool]): If the property can be booked instantly.
super_host (Optional[bool]): If the host is a super host.
languageId (Optional[str]): The ID of the language for the response.
Returns:
dict: A dictionary that contains the search results.
"""
params = {
"id": _id,
"display_name": display_name,
"totalRecords": total_records,
"currency": currency,
"offset": offset,
"category": category,
"adults": adults,
"children": children,
"infants": infants,
"pets": pets,
"checkin": checkin,
"checkout": checkout,
"priceMin": priceMin,
"priceMax": priceMax,
"minBedrooms": minBedrooms,
"minBeds": minBeds,
"minBathrooms": minBathrooms,
"property_type": property_type,
"host_languages": host_languages,
"amenities": amenities,
"type_of_place": type_of_place,
"top_tier_stays": top_tier_stays,
"self_check_in": self_check_in,
"instant_book": instant_book,
"super_host": super_host,
"languageId": languageId,
}
response = requests.get(
f"{BASE_URL}/searchPropertyByPlace", headers=HEADERS, params=params
)
return response.json()["data"][0]
@tool.get("/search_property_by_coordinates")
def search_property_by_coordinates(
neLat: float,
neLng: float,
swLat: float,
swLng: float,
currency: Optional[str] = "USD",
nextPageCursor: Optional[str] = None,
totalRecords: Optional[str] = None,
infants: Optional[int] = None,
adults: Optional[int] = 1,
children: Optional[int] = None,
pets: Optional[int] = None,
checkin: Optional[str] = None,
checkout: Optional[str] = None,
priceMin: Optional[int] = None,
priceMax: Optional[int] = None,
minBedrooms: Optional[int] = None,
minBeds: Optional[int] = None,
minBathrooms: Optional[int] = None,
property_type: Optional[List[str]] = None,
host_languages: Optional[List[str]] = None,
amenities: Optional[List[str]] = None,
type_of_place: Optional[List[str]] = None,
top_tier_stays: Optional[List[str]] = None,
super_host: Optional[bool] = None,
) -> dict:
"""
This function takes GEO coordinates and various other parameters to search properties on Airbnb.
Parameters:
neLat (float): Latitude of the northeastern corner of the search area.
neLng (float): Longitude of the northeastern corner of the search area.
swLat (float): Latitude of the southwestern corner of the search area.
swLng (float): Longitude of the southwestern corner of the search area.
Other parameters are the same as search_property function.
Returns:
dict: A dictionary that contains the search results.
"""
params = {
"neLat": neLat,
"neLng": neLng,
"swLat": swLat,
"swLng": swLng,
"currency": currency,
"nextPageCursor": nextPageCursor,
"totalRecords": totalRecords,
"infants": infants,
"adults": adults,
"children": children,
"pets": pets,
"checkin": checkin,
"checkout": checkout,
"priceMin": priceMin,
"priceMax": priceMax,
"minBedrooms": minBedrooms,
"minBeds": minBeds,
"minBathrooms": minBathrooms,
"property_type": property_type,
"host_languages": host_languages,
"amenities": amenities,
"type_of_place": type_of_place,
"top_tier_stays": top_tier_stays,
"super_host": super_host,
}
response = requests.get(
f"https://airbnb19.p.rapidapi.com/api/v2/searchPropertyByGEO",
headers=HEADERS,
params=params,
)
return response.json()["data"]["list"][0]
@tool.get("/search_destination")
def search_destination(self, query: str, country: Optional[str] = None) -> dict:
"""
This function performs a destination search given a query and optionally a country. And return positions 'ID' information.
Parameters:
query (str): The search query.
country (Optional[str]): The country for the search.
Returns:
dict: A dictionary that contains the search results. including ID information for a destination
"""
params = {"query": query, "country": country}
response = requests.get(
f"{BASE_URL}/searchDestination", 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("/get_property_details")
def get_property_details(
propertyId: int,
currency: Optional[str] = "USD",
checkIn: Optional[str] = None,
checkOut: Optional[str] = None,
adults: Optional[int] = 1,
children: Optional[int] = None,
infants: Optional[int] = None,
pets: Optional[int] = None,
languageId: Optional[str] = None,
) -> dict:
"""
This function retrieves the details of a property given its ID.
Parameters:
propertyId (int): The ID of the property.
currency (Optional[str]): The currency for the transaction.
checkIn (Optional[str]): The check-in date.
checkOut (Optional[str]): The check-out date.
adults (Optional[int]): The number of adults.
children (Optional[int]): The number of children.
infants (Optional[int]): The number of infants.
pets (Optional[int]): The number of pets.
languageId (Optional[str]): The ID of the language for the response.
Returns:
dict: A dictionary that contains the details of the property.
"""
params = {
"propertyId": propertyId,
"currency": currency,
"checkIn": checkIn,
"checkOut": checkOut,
"adults": adults,
"children": children,
"infants": infants,
"pets": pets,
"languageId": languageId,
}
response = requests.get(
f"https://airbnb19.p.rapidapi.com/api/v2/getPropertyDetails",
headers=HEADERS,
params=params,
)
return response.json()
@tool.get("/check_availability")
def check_availability(propertyId: int) -> dict:
"""
This function checks the availability of a property given its ID.
Parameters:
propertyId (int): The ID of the property.
Returns:
dict: A dictionary that contains the availability of the property.
"""
params = {
"propertyId": propertyId,
}
response = requests.get(
f"{BASE_URL}/checkAvailability", headers=HEADERS, params=params
)
return response.json()
@tool.get("/get_property_reviews")
def get_property_reviews(propertyId: int) -> dict:
"""
This function retrieves the reviews of a property given its ID.
Parameters:
propertyId (int): The ID of the property.
Returns:
dict: A dictionary that contains the reviews of the property.
"""
params = {
"propertyId": propertyId,
}
response = requests.get(
f"{BASE_URL}/getPropertyReviews", headers=HEADERS, params=params
)
return response.json()
@tool.get("/get_property_checkout_price")
def get_property_checkout_price(propertyId: int, checkIn: str) -> dict:
"""
This function retrieves the checkout cost of a property given its ID and check-in date.
Parameters:
propertyId (int): The ID of the property.
checkIn (str): The check-in date.
Returns:
dict: A dictionary that contains the checkout price of the property.
"""
params = {"propertyId": propertyId, "checkIn": checkIn}
response = requests.get(
f"{BASE_URL}/getPropertyCheckoutPrice", headers=HEADERS, params=params
)
return response.json()
return tool
|