-
Notifications
You must be signed in to change notification settings - Fork 20
/
queries.py
66 lines (57 loc) · 2.48 KB
/
queries.py
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
""" Contains all queries to the Realtor.ca API and OpenStreetMap."""
import requests
def get_coordinates(city):
"""Gets the coordinate bounds of a city from OpenStreetMap."""
url = "https://nominatim.openstreetmap.org/search?q=" + city + "&format=json"
response = requests.get(url=url, timeout=10)
response.raise_for_status()
data = response.json()
for response in data:
if (response["class"] == "boundary" and
response["type"] == "administrative"):
return response["boundingbox"] # [latMin, latMax, lonMin, lonMax]
return data
#pylint: disable=too-many-arguments
def get_property_list(
lat_min, lat_max, long_min, long_max,
price_min=0, price_max=10000000,
records_per_page=200, culture_id=1,
current_page=1, application_id=1):
"""Queries the Realtor.ca API to get a list of properties."""
url = "https://api2.realtor.ca/Listing.svc/PropertySearch_Post"
headers = {"Referer": "https://www.realtor.ca/",
"Origin": "https://www.realtor.ca/",
"Host": "api2.realtor.ca"}
form = {
"LatitudeMin": lat_min,
"LatitudeMax": lat_max,
"LongitudeMin": long_min,
"LongitudeMax": long_max,
"PriceMin": price_min,
"PriceMax": price_max,
"RecordsPerPage": records_per_page,
"CultureId": culture_id,
"CurrentPage": current_page,
"ApplicationId": application_id
}
response = requests.post(url=url, headers=headers, data=form, timeout=10)
if response.status_code == 403:
print("Error 403: Rate limited")
elif response.status_code != 200:
print("Error " + str(response.status_code))
response.raise_for_status()
return response.json()
def get_property_details(property_id, mls_reference_number):
"""Queries the Realtor.ca API to get details of a property."""
baseurl = "https://api2.realtor.ca/Listing.svc/PropertyDetails?ApplicationId=1&CultureId=1"
url = baseurl + "&PropertyID=" + property_id + "&ReferenceNumber=" + mls_reference_number
headers = {"Referer": "https://www.realtor.ca/",
"Origin": "https://www.realtor.ca/",
"Host": "api2.realtor.ca"}
response = requests.get(url=url, headers=headers, timeout=10)
if response.status_code == 403:
print("Error 403: Rate limited")
elif response.status_code != 200:
print("Error " + str(response.status_code))
response.raise_for_status()
return response.json()