2024-03-07 22:02:09 +00:00
|
|
|
# from diskcache import Cache
|
2023-11-06 00:31:58 +00:00
|
|
|
import requests
|
2024-03-07 22:02:09 +00:00
|
|
|
# from rec.db import RightmoveListing
|
2023-11-06 00:31:58 +00:00
|
|
|
|
|
|
|
|
import urllib3
|
2023-11-18 12:30:04 +02:00
|
|
|
|
2023-11-06 00:31:58 +00:00
|
|
|
urllib3.disable_warnings()
|
|
|
|
|
|
2024-03-07 22:02:09 +00:00
|
|
|
# cache = Cache(r"_cache")
|
2023-11-06 00:31:58 +00:00
|
|
|
|
|
|
|
|
headers = {
|
2023-11-18 12:30:04 +02:00
|
|
|
"Host": "api.rightmove.co.uk",
|
2023-11-06 00:31:58 +00:00
|
|
|
# 'Accept-Encoding': 'gzip, deflate, br',
|
2023-11-18 12:30:04 +02:00
|
|
|
"User-Agent": "okhttp/4.10.0",
|
|
|
|
|
"Connection": "close",
|
2023-11-06 00:31:58 +00:00
|
|
|
}
|
|
|
|
|
|
2023-11-18 12:30:04 +02:00
|
|
|
|
2023-11-18 12:56:15 +02:00
|
|
|
def detail_query(detail_id: int):
|
|
|
|
|
params = {
|
2024-03-25 20:48:48 +00:00
|
|
|
"apiApplication": "ANDROID",
|
|
|
|
|
"appVersion": "3.70.0",
|
2023-11-18 12:56:15 +02:00
|
|
|
}
|
2024-03-25 20:48:48 +00:00
|
|
|
url = f"https://api.rightmove.co.uk/api/property/{detail_id}"
|
2023-11-18 12:56:15 +02:00
|
|
|
response = requests.get(url, params=params, headers=headers, verify=False)
|
|
|
|
|
if response.status_code != 200:
|
|
|
|
|
raise Exception("Failed due to: ", response.text)
|
|
|
|
|
|
|
|
|
|
return response.json()
|
|
|
|
|
|
|
|
|
|
|
2024-03-07 22:02:09 +00:00
|
|
|
# @cache.memoize()
|
2024-03-25 20:48:48 +00:00
|
|
|
def listing_query(
|
|
|
|
|
page: int,
|
|
|
|
|
min_bedrooms: int,
|
|
|
|
|
max_bedrooms: int,
|
|
|
|
|
radius: float,
|
|
|
|
|
min_price: int,
|
|
|
|
|
max_price: int,
|
|
|
|
|
mustNewHome: bool = False,
|
|
|
|
|
max_days_since_added: int = None,
|
|
|
|
|
) -> dict:
|
2023-11-06 00:31:58 +00:00
|
|
|
params = {
|
2023-11-18 12:30:04 +02:00
|
|
|
"locationIdentifier": "POSTCODE^4228216",
|
|
|
|
|
"channel": "BUY",
|
|
|
|
|
"page": str(page),
|
|
|
|
|
"numberOfPropertiesPerPage": "25",
|
2023-11-18 12:38:54 +02:00
|
|
|
"radius": str(radius),
|
2023-11-18 12:30:04 +02:00
|
|
|
"sortBy": "distance",
|
|
|
|
|
"includeUnavailableProperties": "false",
|
|
|
|
|
"propertyTypes": "flat",
|
|
|
|
|
"dontShow": "sharedOwnership,retirement",
|
2023-11-18 12:38:54 +02:00
|
|
|
"minPrice": str(min_price),
|
|
|
|
|
"maxPrice": str(max_price),
|
2023-11-18 12:30:04 +02:00
|
|
|
"minBedrooms": str(min_bedrooms),
|
|
|
|
|
"maxBedrooms": str(max_bedrooms),
|
|
|
|
|
"apiApplication": "ANDROID",
|
|
|
|
|
"appVersion": "3.70.0",
|
2023-11-06 00:31:58 +00:00
|
|
|
}
|
2024-03-13 16:21:54 +00:00
|
|
|
if max_days_since_added:
|
2024-03-25 20:48:48 +00:00
|
|
|
if max_days_since_added not in [1, 3, 7, 14]:
|
|
|
|
|
raise Exception("Invalid max days. Can only be", [1, 3, 7, 14])
|
|
|
|
|
params["maxDaysSinceAdded"] = max_days_since_added
|
|
|
|
|
|
2024-03-13 16:21:54 +00:00
|
|
|
if mustNewHome:
|
2024-03-25 20:48:48 +00:00
|
|
|
params["mustHave"] = "newHome"
|
2023-11-06 00:31:58 +00:00
|
|
|
|
2023-11-18 12:30:04 +02:00
|
|
|
response = requests.get(
|
|
|
|
|
"https://api.rightmove.co.uk/api/property-listing",
|
|
|
|
|
params=params,
|
|
|
|
|
headers=headers,
|
|
|
|
|
verify=False,
|
|
|
|
|
)
|
2023-11-06 00:31:58 +00:00
|
|
|
if response.status_code != 200:
|
|
|
|
|
raise Exception("Failed due to: ", response.text)
|
|
|
|
|
|
|
|
|
|
return response.json()
|
|
|
|
|
|
|
|
|
|
|
2023-11-18 12:30:04 +02:00
|
|
|
if __name__ == "__main__":
|
2024-03-25 20:48:48 +00:00
|
|
|
response = listing_query(
|
|
|
|
|
page=1,
|
|
|
|
|
min_bedrooms=2,
|
|
|
|
|
max_bedrooms=2,
|
|
|
|
|
radius=5.0,
|
|
|
|
|
min_price=150000,
|
|
|
|
|
max_price=700000,
|
|
|
|
|
)
|
2023-11-06 00:31:58 +00:00
|
|
|
resp = response
|
2023-11-18 12:30:04 +02:00
|
|
|
for d in resp["properties"]:
|
2023-11-06 00:31:58 +00:00
|
|
|
rl = RightmoveListing(
|
2023-11-18 12:30:04 +02:00
|
|
|
id=d["identifier"],
|
2023-11-06 00:31:58 +00:00
|
|
|
listing_json=d,
|
2023-11-18 12:30:04 +02:00
|
|
|
price=d["price"],
|
|
|
|
|
updated_timestamp=d["updateDate"],
|
2023-11-18 13:09:03 +02:00
|
|
|
lat=d["latitude"],
|
|
|
|
|
lon=d["longitude"],
|
2023-11-06 00:31:58 +00:00
|
|
|
)
|
|
|
|
|
rl.save()
|