forked from koikonom/pynest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pynest.py
226 lines (179 loc) · 5.98 KB
/
pynest.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
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
import urllib
import requests
import time
import json
from cStringIO import StringIO
import logging
L = logging.getLogger(__name__)
servers = {
'au': 'api.nestoria.com.au',
'br': 'api.nestoria.com.br',
'de': 'api.nestoria.de',
'es': 'api.nestoria.es',
'fr': 'api.nestoria.fr',
'in': 'api.nestoria.in',
'it': 'api.nestoria.it',
'uk': 'api.nestoria.co.uk',
}
country = 'uk'
api_proto = 'http'
api_method = 'GET'
api_server = servers[country]
api_path = 'api'
api_encoding = 'json'
api_url = '%s://%s/%s' % (api_proto, api_server, api_path)
api_throttle = 1.0 # secs to wait between requests
api_last_call = 0.0
####
# helper methods
#
def download_url(url):
response = requests.get(url)
response.raise_for_status()
return StringIO(response.content)
def _make_url(action, args=[]):
global api_last_call
if not action:
return ''
if action not in ['echo', 'keywords'] and args == []:
return ''
if time.time() - api_last_call < api_throttle:
return ''
url_data = urllib.urlencode([('action', action),
('encoding', api_encoding)] + args)
url = "{}?{}".format(api_url, url_data)
L.debug(url)
return url
def _make_call(action, args=[]):
url = _make_url(action, args)
f = download_url(url)
parsed = json.loads(f.read())
if 'error_code' in parsed:
raise RuntimeError("Error {}: {}".format(parsed['error_code'],
parsed['error_string']))
return parsed
def set_country(country_code):
"""
Choose the country we wish to use.
Available country codes can be returned by using get_supported_countries().
"""
if country_code in servers.keys():
country = country_code
def get_supported_countries():
"""
Return list of all supported countries
"""
return servers.keys()
def get_country():
"""
Return current country.
"""
return country
####
# misc API methods
#
def echo(**kwargs):
"""
Make an 'echo' request. Whatever named arguments are passed to this method
are used as parameters to the request.
"""
return _make_call('echo', kwargs.items())
def keywords():
"""
Make a 'keywords' request. Returns all available keywords from the nestoria
API.
"""
return _make_call('keywords')
####
# metadata methods
#
def metadata_by_area(place_name):
"""
place_name: (string) anything you would type into the search
box on Nestoria: a place name, post code,
tube station, etc.
e.g. "Chelsea" or "SW14"
"""
return metadata(place_name=place_name)
def metadata_by_longlat(longlat):
"""
longlat: (two-tuple of strings) a bounding box in the format
sw_latitude, sw_longitude, ne_latitude2, ne_longitude2
e.g ('51.684183,-3.431481', '51.85415,-3.077859')
"""
return metadata(south_west=longlat[0], north_east=longlat[1])
def metadata_by_center(center, radius='2km'):
"""
center: (string) latitude and longitude. A default radius of 2km
will be used.
e.g. '51.684183,-3.431481'
radius: (string) radius from center in kilometers or miles
(km or mi needs to be set)
e.g 10km
"""
s = center + ',' + radius
return metadata(centre_point=s)
def metadata(**kwargs):
return _make_call('metadata', kwargs.items())
####
# property search methods
#
def search_by_area(place_name, sf, snp):
"""
place_name: (string) anything you would type into the search
box on Nestoria: a place name, post code,
tube station, etc.
e.g. "Chelsea" or "SW14"
filter: (dictionary) http://www.nestoria.co.uk/help/api-search-listings
"""
d = dict([('place_name', place_name)] + sf.items() + snp.items())
return search(**d)
def search_by_longlat(longlat, sf, snp):
"""
longlat: (two-tuple of strings) a bounding box in the format
sw_latitude, sw_longitude, ne_latitude2, ne_longitude2
e.g ('51.684183,-3.431481', '51.85415,-3.077859')
sf: (dictionary) search filter as defined in
http://www.nestoria.co.uk/help/api-search-listings
snp: (dictionary) Sort and pagination parameters as defined in
http://www.nestoria.co.uk/help/api-search-listings
"""
d = dict([('south_west', longlat[0]),
('north_east', longlat[1])] + sf.items() + snp.items())
return search(**d)
def search_by_center(center, sf, snp, radius='2km'):
"""
center: (string) latitude and longitude. A default radius of 2km
will be used.
e.g. '51.684183,-3.431481'
radius: (string) radius from center in kilometers or miles
(km or mi needs to be set)
e.g 10km
sf: (dictionary) search filter as defined in
http://www.nestoria.co.uk/help/api-search-listings
snp: (dictionary) Sort and pagination parameters as defined in
http://www.nestoria.co.uk/help/api-search-listings
"""
s = center + ',' + radius
d = dict([('centre_point', s)] + sf.items() + snp.items())
return search(**d)
def search_by_guids(guid_list):
guids = ','.join(guid_list)
return search(guid=guids)
def search(**kwargs):
return _make_call('search_listings', kwargs.items())
# ########################## TEST ############################################ #
# print echo(test='success')
# time.sleep(1.0)
# print keywords()
# print metadata_by_area(place_name='Bristol')
# print metadata_by_longlat(('51.684183,-3.431481', '51.85415,-3.077859'))
# print metadata_by_center('51.85415,-3.077859', '20km')
# sf = {}
# sf['listing_type'] = 'rent'
# sf['price_max'] = '1100'
# sf['bedroom_min'] = '2'
# snp = {"number_of_result":50, "page":20}
# print search_by_area('bristol', sf, snp)
# print search_by_longlat(('51.684183,-3.431481', '51.85415,-3.077859'), sf, snp)
# print search_by_center('51.85415,-3.077859', sf, snp, radius='20km')