-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbayut_api.py
More file actions
288 lines (246 loc) · 11 KB
/
bayut_api.py
File metadata and controls
288 lines (246 loc) · 11 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
"""
Bayut Scraper API - Python Client
Powered by RealtyAPI.io
A comprehensive Python wrapper for the Bayut Scraper API.
Supports property search, property details, agent/agency lookup,
TruBroker search, and autocomplete for UAE real estate.
"""
import requests
from config import BASE_URL, get_headers
class BayutAPI:
"""
Bayut Scraper API client for UAE real estate data extraction.
Provides access to Bayut property data for Dubai, Abu Dhabi,
Sharjah, and all UAE emirates 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. Search & Autocomplete
# ─────────────────────────────────────────────
def autocomplete(self, query, hits_per_page=None, page=None):
"""
Get autocomplete suggestions for a location query.
Returns location_external_id used in property search.
Args:
query (str): Search term. Example: "dubai", "abu dhabi"
hits_per_page (str): Results per page. Default: 25
page (str): Page number (0-based). Default: 0
Returns:
dict: Autocomplete results with location_external_id values
"""
return self._get("/autocomplete", {
"query": query,
"hitsPerPage": hits_per_page,
"page": page,
})
def search_property(self, location_external_id, purpose="for-sale",
hits_per_page=None, page=None, category=None,
rent_frequency=None, rooms=None, baths=None,
min_price=None, max_price=None, min_area=None,
max_area=None, furnishing_status=None,
agency_external_ids=None, category_external_id=None,
product=None, has_floorplan=None, has_panorama=None,
has_video=None, completion_status=None):
"""
Search properties on Bayut.
Args:
location_external_id (str): Location ID from /autocomplete.
Example: "6901" (Dubai Marina)
purpose (str): "for-sale" or "for-rent"
hits_per_page (str): Results per page. Default: 25
page (str): Page number (0-based). Default: 0
category (str): "residential" or "commercial"
rent_frequency (str): "monthly", "yearly", "weekly", or "daily"
(only for for-rent)
rooms (str): Number of bedrooms. Example: "1", "2", "3"
baths (str): Number of bathrooms. Example: "1", "2"
min_price (str): Minimum price
max_price (str): Maximum price
min_area (str): Minimum area in sqft
max_area (str): Maximum area in sqft
furnishing_status (str): "furnished" or "unfurnished"
agency_external_ids (str): Filter by agency. Comma-separated IDs.
Example: "7737,5002"
category_external_id (str): Property type ID.
Apartment=4, Townhouses=16, Villas=3, Penthouses=18,
Hotel Apartments=21, Villa Compound=19, Residential Plot=14,
Residential Floor=12, Residential Building=17,
Office=5, Shop=6, Warehouse=7, Labour camp=9,
Commercial Villa=25, Bulk Units=20, Commercial Plot=15,
Commercial Floor=13, Commercial Building=10, Factory=8,
Industrial Land=22, Mixed Use Land=23, Showroom=24,
Other Commercial=11
product (str): "superhot" or "hot" (featured listings)
has_floorplan (bool): Filter listings with floor plans
has_panorama (bool): Filter listings with 360 panorama
has_video (bool): Filter listings with video tours
completion_status (str): "completed" or "under-construction"
(only for for-sale)
Returns:
dict: Search results with property listings
"""
return self._get("/search/property", {
"location_external_id": location_external_id,
"purpose": purpose,
"hitsPerPage": hits_per_page,
"page": page,
"category": category,
"rent_frequency": rent_frequency,
"rooms": rooms,
"baths": baths,
"min_price": min_price,
"max_price": max_price,
"min_area": min_area,
"max_area": max_area,
"furnishingStatus": furnishing_status,
"agency_external_ids": agency_external_ids,
"category_external_id": category_external_id,
"product": product,
"hasFloorplan": has_floorplan,
"hasPanorama": has_panorama,
"hasVideo": has_video,
"completionStatus": completion_status,
})
# ─────────────────────────────────────────────
# 2. Property Details
# ─────────────────────────────────────────────
def get_property_details(self, property_external_id):
"""
Get full property details by external ID.
Args:
property_external_id (str): The externalID from search results.
Example: "12372178"
Returns:
dict: Full property details
"""
return self._get("/details/property", {
"property_external_id": property_external_id,
})
# ─────────────────────────────────────────────
# 3. Agency & Agent
# ─────────────────────────────────────────────
def get_agency_details(self, agency_external_id):
"""
Get agency details and its agents list.
Args:
agency_external_id (str): Agency ID from search results.
Example: "10499"
Returns:
dict: Agency details with agent list
"""
return self._get("/details/agency", {
"agency_external_id": agency_external_id,
})
def get_agent_details(self, agent_slug):
"""
Get agent profile details.
Args:
agent_slug (str): Agent slug from agency details
(hits -> slug field).
Example: "salma-mohamed-mahmoud-hashem-2521536"
Returns:
dict: Agent profile details
"""
return self._get("/details/agent", {
"agent_slug": agent_slug,
})
def get_agents_by_agency(self, agency_external_id):
"""
Get all agents belonging to an agency.
Args:
agency_external_id (str): Agency ID. Example: "10499"
Returns:
dict: List of agents in the agency
"""
return self._get("/agent/agency", {
"agency_external_id": agency_external_id,
})
def get_agent_claimed_deals_count(self, agent_external_id):
"""
Get the number of claimed deals for an agent.
Args:
agent_external_id (str): Agent ID from agency details.
Example: "1919589"
Returns:
dict: Claimed deals count
"""
return self._get("/agent/claimedDeals/count", {
"agent_external_id": agent_external_id,
})
def get_agent_claimed_deals_total_price(self, agent_external_id):
"""
Get total price of claimed deals for an agent.
Args:
agent_external_id (str): Agent ID from agency details.
Example: "1919589"
Returns:
dict: Total price of claimed deals
"""
return self._get("/agent/claimedDeals/totalPrice", {
"agent_external_id": agent_external_id,
})
def get_agent_stories(self, agent_external_id, page_size=None):
"""
Get agent stories/posts.
Args:
agent_external_id (str): Agent ID. Example: "1919589"
page_size (str): Number of stories per page. Default: 50
Returns:
dict: Agent stories
"""
return self._get("/stories/agent", {
"agent_external_id": agent_external_id,
"page_size": page_size,
})
# ─────────────────────────────────────────────
# 4. TruBroker
# ─────────────────────────────────────────────
def search_trubroker_agents(self, location_external_id,
hits_per_page=None, page=None,
category=None, purpose=None,
completion_status=None):
"""
Search TruBroker verified agents by location.
Args:
location_external_id (str): Location ID from /autocomplete.
Example: "5152"
hits_per_page (str): Results per page. Default: 25
page (str): Page number. Default: 1
category (str): "residential" or "commercial"
purpose (str): "for-sale" or "for-rent"
completion_status (str): "completed" or "under-construction"
Returns:
dict: TruBroker agent results
"""
return self._get("/truBroker/agent", {
"location_external_id": location_external_id,
"hitsPerPage": hits_per_page,
"page": page,
"category": category,
"purpose": purpose,
"completionStatus": completion_status,
})