|
@@ -1,3 +1,4 @@
|
|
|
|
|
+import math
|
|
|
import json
|
|
import json
|
|
|
import logging
|
|
import logging
|
|
|
import datetime
|
|
import datetime
|
|
@@ -8,34 +9,69 @@ import db
|
|
|
import config
|
|
import config
|
|
|
|
|
|
|
|
import init_localities
|
|
import init_localities
|
|
|
|
|
+import util
|
|
|
|
|
+
|
|
|
|
|
+_SEARCH_PAGE_SIZE = 50
|
|
|
|
|
|
|
|
def _now():
|
|
def _now():
|
|
|
- return datetime.datetime.now().strftime(r'%Y-%m-%d %M:%H:%S')
|
|
|
|
|
|
|
+ return datetime.datetime.now().strftime(r'%Y-%m-%d %H:%M:%S')
|
|
|
|
|
+
|
|
|
|
|
+def process_properties(conn):
|
|
|
|
|
+ count = conn.select('select count(1) from properties where processed=0')[0]
|
|
|
|
|
+ if not count:
|
|
|
|
|
+ return
|
|
|
|
|
+
|
|
|
|
|
+ print('Processing %i properties...' % count)
|
|
|
|
|
|
|
|
-def get_property(conn, property_id):
|
|
|
|
|
- resp = requests.get('https://api.myhome.ie/brochure/%i?ApiKey=%s&RequestTypeId=1' % (property_id, config.API_KEY))
|
|
|
|
|
|
|
+ rows = conn.select('select id, raw_json from properties where processed=0 limit 10')
|
|
|
|
|
+ while rows:
|
|
|
|
|
|
|
|
- print(json.dumps(resp.json()))
|
|
|
|
|
|
|
+ rows = conn.select('select id, raw_json from properties where processed=0 limit 10')
|
|
|
|
|
|
|
|
- data = resp.json()['Brochure']['Property']
|
|
|
|
|
|
|
|
|
|
- stmt = 'insert into properties (id, last_update, raw_json) values (?,?,?)'
|
|
|
|
|
- params = [data['PropertyId'], _now(), json.dumps(data)]
|
|
|
|
|
|
|
|
|
|
- conn.execute(stmt, *params)
|
|
|
|
|
|
|
+def fetch_properties(conn):
|
|
|
|
|
+
|
|
|
|
|
+ unknown_properties = conn.select('''
|
|
|
|
|
+ select distinct sr.property_id
|
|
|
|
|
+ from search_results sr left join properties p on sr.property_id = p.id
|
|
|
|
|
+ where p.id is null
|
|
|
|
|
+ ''')
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ complete = 0
|
|
|
|
|
+ for batch in util.batches(unknown_properties, 10):
|
|
|
|
|
+ print('Fetching property details (complete: %i/%i)...' % (complete, len(unknown_properties)))
|
|
|
|
|
+
|
|
|
|
|
+ stmt = 'insert into properties (id, last_update, raw_json) values'
|
|
|
|
|
+ params = []
|
|
|
|
|
+
|
|
|
|
|
+ for property_id in batch:
|
|
|
|
|
+ resp = requests.get('https://api.myhome.ie/brochure/%i?ApiKey=%s&RequestTypeId=1' % (property_id, config.API_KEY))
|
|
|
|
|
+ data = resp.json()['Brochure']['Property']
|
|
|
|
|
+ stmt += ' (?, ?, ?),'
|
|
|
|
|
+ params.extend([
|
|
|
|
|
+ property_id,
|
|
|
|
|
+ _now(),
|
|
|
|
|
+ json.dumps(data)
|
|
|
|
|
+ ])
|
|
|
|
|
+
|
|
|
|
|
+ conn.execute(stmt[:-1], *params)
|
|
|
|
|
+
|
|
|
|
|
+ complete += len(batch)
|
|
|
|
|
|
|
|
def search(conn, locality_id):
|
|
def search(conn, locality_id):
|
|
|
logging.info('Searching %i...', locality_id)
|
|
logging.info('Searching %i...', locality_id)
|
|
|
|
|
|
|
|
page = 1
|
|
page = 1
|
|
|
- found_properties = set()
|
|
|
|
|
|
|
+ num_pages = None
|
|
|
|
|
|
|
|
while True:
|
|
while True:
|
|
|
body = {
|
|
body = {
|
|
|
'AddedSince': "",
|
|
'AddedSince': "",
|
|
|
'ApiKey': config.API_KEY,
|
|
'ApiKey': config.API_KEY,
|
|
|
'Page': page,
|
|
'Page': page,
|
|
|
- 'PageSize': 50,
|
|
|
|
|
|
|
+ 'PageSize': _SEARCH_PAGE_SIZE,
|
|
|
'RequestTypeId': 2,
|
|
'RequestTypeId': 2,
|
|
|
'SearchRequest': config.SEARCH_PARAMS,
|
|
'SearchRequest': config.SEARCH_PARAMS,
|
|
|
'SortColumn': 2,
|
|
'SortColumn': 2,
|
|
@@ -55,51 +91,57 @@ def search(conn, locality_id):
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- print('Fetching page %i...' % page)
|
|
|
|
|
|
|
+ if num_pages:
|
|
|
|
|
+ print('Fetching page %i/%i...' % (page, num_pages))
|
|
|
|
|
+ else:
|
|
|
|
|
+ print('Fetching page %i...' % page)
|
|
|
|
|
|
|
|
resp = requests.post('https://api.myhome.ie/search?ApiKey=%s' % config.API_KEY, json=body, headers=headers)
|
|
resp = requests.post('https://api.myhome.ie/search?ApiKey=%s' % config.API_KEY, json=body, headers=headers)
|
|
|
-
|
|
|
|
|
resp.raise_for_status()
|
|
resp.raise_for_status()
|
|
|
-
|
|
|
|
|
data = resp.json()
|
|
data = resp.json()
|
|
|
|
|
|
|
|
if not isinstance(data, dict):
|
|
if not isinstance(data, dict):
|
|
|
print('Error: %s' % data)
|
|
print('Error: %s' % data)
|
|
|
return
|
|
return
|
|
|
|
|
|
|
|
- stmt = 'insert into search_results (time, property_id, raw_json) VALUES '
|
|
|
|
|
|
|
+ stmt = 'insert into search_results (time, property_id, price, raw_json) VALUES '
|
|
|
params = []
|
|
params = []
|
|
|
|
|
|
|
|
for result in data.get('SearchResults', []):
|
|
for result in data.get('SearchResults', []):
|
|
|
- found_properties.add(result['id'])
|
|
|
|
|
- stmt += '(?, ?, ?),'
|
|
|
|
|
- params.extend([_now(), result['id'], json.dumps(result)])
|
|
|
|
|
|
|
+ if not result.get('PriceAsString'):
|
|
|
|
|
+ continue
|
|
|
|
|
+
|
|
|
|
|
+ stmt += '(?, ?, ?, ?),'
|
|
|
|
|
+
|
|
|
|
|
+ params.extend([
|
|
|
|
|
+ _now(),
|
|
|
|
|
+ result['id'],
|
|
|
|
|
+ result['PriceAsString'].replace(',', '').replace('\u20ac', ''),
|
|
|
|
|
+ json.dumps(result)
|
|
|
|
|
+ ])
|
|
|
|
|
|
|
|
conn.execute(stmt[:-1], *params)
|
|
conn.execute(stmt[:-1], *params)
|
|
|
|
|
|
|
|
- if page*500 > data['ResultCount']:
|
|
|
|
|
|
|
+ num_pages = math.ceil(data['ResultCount'] / float(_SEARCH_PAGE_SIZE))
|
|
|
|
|
+
|
|
|
|
|
+ if page >= num_pages:
|
|
|
break
|
|
break
|
|
|
|
|
|
|
|
page += 1
|
|
page += 1
|
|
|
-
|
|
|
|
|
- known_properties = set(conn.select('select id from properties'))
|
|
|
|
|
-
|
|
|
|
|
- to_get = found_properties.difference(known_properties)
|
|
|
|
|
-
|
|
|
|
|
- get_property(conn, list(to_get)[0])
|
|
|
|
|
-
|
|
|
|
|
- print('Getting %i properties...' % len(to_get))
|
|
|
|
|
|
|
|
|
|
def run():
|
|
def run():
|
|
|
with db.connect() as conn:
|
|
with db.connect() as conn:
|
|
|
if config.MONITORED_LOCALITIES:
|
|
if config.MONITORED_LOCALITIES:
|
|
|
for locality_id in config.MONITORED_LOCALITIES:
|
|
for locality_id in config.MONITORED_LOCALITIES:
|
|
|
- init_localities.init_localities(conn, locality_id=locality_id)
|
|
|
|
|
|
|
+ # init_localities.init_localities(conn, locality_id=locality_id)
|
|
|
|
|
+ search(conn, 1265)
|
|
|
|
|
+ pass
|
|
|
else:
|
|
else:
|
|
|
print('You must set MONITORED_LOCATIONS. Possible values: ')
|
|
print('You must set MONITORED_LOCATIONS. Possible values: ')
|
|
|
init_localities.init_localities(conn)
|
|
init_localities.init_localities(conn)
|
|
|
|
|
|
|
|
- search(conn, 1265)
|
|
|
|
|
|
|
+ fetch_properties(conn)
|
|
|
|
|
+ # process_properties(conn)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if __name__ == '__main__':
|
|
|
run()
|
|
run()
|