|
@@ -13,8 +13,10 @@ import util
|
|
|
|
|
|
|
|
_SEARCH_PAGE_SIZE = 50
|
|
_SEARCH_PAGE_SIZE = 50
|
|
|
|
|
|
|
|
|
|
+_DATETIME_FORMAT = r'%Y-%m-%d %H:%M:%S'
|
|
|
|
|
+
|
|
|
def _now():
|
|
def _now():
|
|
|
- return datetime.datetime.now().strftime(r'%Y-%m-%d %H:%M:%S')
|
|
|
|
|
|
|
+ return datetime.datetime.now().strftime(_DATETIME_FORMAT)
|
|
|
|
|
|
|
|
def process_properties(conn):
|
|
def process_properties(conn):
|
|
|
count = conn.select('select count(1) from properties where processed=0')[0]
|
|
count = conn.select('select count(1) from properties where processed=0')[0]
|
|
@@ -23,10 +25,49 @@ def process_properties(conn):
|
|
|
|
|
|
|
|
print('Processing %i properties...' % count)
|
|
print('Processing %i properties...' % count)
|
|
|
|
|
|
|
|
- rows = conn.select('select id, raw_json from properties where processed=0 limit 10')
|
|
|
|
|
|
|
+ rows = conn.select('select id, last_update, raw_json from properties where processed=0 limit 50')
|
|
|
while rows:
|
|
while rows:
|
|
|
|
|
+ stmt = 'replace into properties (id, last_update, raw_json, processed, price, lat, lon, size, ber, address, locality_id, property_type, bathrooms, bedrooms) values'
|
|
|
|
|
+ params = []
|
|
|
|
|
+
|
|
|
|
|
+ for property_id, last_update, raw_json in rows:
|
|
|
|
|
+ stmt += ' (?, ?, ?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?),'
|
|
|
|
|
+
|
|
|
|
|
+ params.extend([
|
|
|
|
|
+ property_id,
|
|
|
|
|
+ last_update,
|
|
|
|
|
+ raw_json,
|
|
|
|
|
+ ])
|
|
|
|
|
+
|
|
|
|
|
+ if raw_json is None:
|
|
|
|
|
+ params.extend([None] * 10)
|
|
|
|
|
+
|
|
|
|
|
+ else:
|
|
|
|
|
+ data = json.loads(raw_json)
|
|
|
|
|
+
|
|
|
|
|
+ size = None
|
|
|
|
|
+ if data.get('SizeStringMeters'):
|
|
|
|
|
+ size = data.get('SizeStringMeters')
|
|
|
|
|
+ elif data.get('SizeStringFeet'):
|
|
|
|
|
+ size = round(data.get('SizeStringFeet') * 0.09290304, 2)
|
|
|
|
|
+
|
|
|
|
|
+ params.extend([
|
|
|
|
|
+ data.get('Price', {}).get('MinPrice'),
|
|
|
|
|
+ data.get('BrochureMap', {}).get('latitude'),
|
|
|
|
|
+ data.get('BrochureMap', {}).get('longitude'),
|
|
|
|
|
+ size,
|
|
|
|
|
+ data.get('BerRating'),
|
|
|
|
|
+ data.get('DisplayAddress', data.get('Address')),
|
|
|
|
|
+ data.get('LocalityId'),
|
|
|
|
|
+ data.get('PropertyType'),
|
|
|
|
|
+ data.get('Bathrooms', data.get('BathString', '').replace(' baths', '') or None),
|
|
|
|
|
+ data.get('BedsString', '').replace(' beds', '') or None,
|
|
|
|
|
+ ])
|
|
|
|
|
+
|
|
|
|
|
+ if params:
|
|
|
|
|
+ conn.execute(stmt[:-1], *params)
|
|
|
|
|
|
|
|
- rows = conn.select('select id, raw_json from properties where processed=0 limit 10')
|
|
|
|
|
|
|
+ rows = conn.select('select id, last_update, raw_json from properties where processed=0 limit 10')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -48,23 +89,44 @@ def fetch_properties(conn):
|
|
|
|
|
|
|
|
for property_id in batch:
|
|
for property_id in batch:
|
|
|
resp = requests.get('https://api.myhome.ie/brochure/%i?ApiKey=%s&RequestTypeId=1' % (property_id, config.API_KEY))
|
|
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)
|
|
|
|
|
- ])
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if resp.json() in ["The property you requested isn't available any more", "The property you requested is inactive and can't be found"]:
|
|
|
|
|
+ stmt += ' (?, ?, ?),'
|
|
|
|
|
+ params.extend([
|
|
|
|
|
+ property_id,
|
|
|
|
|
+ _now(),
|
|
|
|
|
+ None,
|
|
|
|
|
+ ])
|
|
|
|
|
+
|
|
|
|
|
+ else:
|
|
|
|
|
+ try:
|
|
|
|
|
+ data = resp.json()['Brochure']['Property']
|
|
|
|
|
+ stmt += ' (?, ?, ?),'
|
|
|
|
|
+ params.extend([
|
|
|
|
|
+ property_id,
|
|
|
|
|
+ _now(),
|
|
|
|
|
+ json.dumps(data)
|
|
|
|
|
+ ])
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ print(str(e))
|
|
|
|
|
+ print(resp.json())
|
|
|
|
|
|
|
|
- conn.execute(stmt[:-1], *params)
|
|
|
|
|
|
|
+ if params:
|
|
|
|
|
+ conn.execute(stmt[:-1], *params)
|
|
|
|
|
|
|
|
complete += len(batch)
|
|
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)
|
|
|
|
|
|
|
|
|
|
+ cutoff = (datetime.datetime.now() - datetime.timedelta(hours=20)).strftime(_DATETIME_FORMAT)
|
|
|
|
|
+
|
|
|
|
|
+ recent_properties = set(conn.select('''select property_id from (select property_id, max(time) m
|
|
|
|
|
+ from search_results group by property_id) where m > ?''', cutoff))
|
|
|
|
|
+
|
|
|
page = 1
|
|
page = 1
|
|
|
num_pages = None
|
|
num_pages = None
|
|
|
|
|
+ inserted = 0
|
|
|
|
|
|
|
|
while True:
|
|
while True:
|
|
|
body = {
|
|
body = {
|
|
@@ -110,6 +172,8 @@ def search(conn, locality_id):
|
|
|
for result in data.get('SearchResults', []):
|
|
for result in data.get('SearchResults', []):
|
|
|
if not result.get('PriceAsString'):
|
|
if not result.get('PriceAsString'):
|
|
|
continue
|
|
continue
|
|
|
|
|
+ if result['id'] in recent_properties:
|
|
|
|
|
+ continue
|
|
|
|
|
|
|
|
stmt += '(?, ?, ?, ?),'
|
|
stmt += '(?, ?, ?, ?),'
|
|
|
|
|
|
|
@@ -120,7 +184,10 @@ def search(conn, locality_id):
|
|
|
json.dumps(result)
|
|
json.dumps(result)
|
|
|
])
|
|
])
|
|
|
|
|
|
|
|
- conn.execute(stmt[:-1], *params)
|
|
|
|
|
|
|
+ inserted += 1
|
|
|
|
|
+
|
|
|
|
|
+ if params:
|
|
|
|
|
+ conn.execute(stmt[:-1], *params)
|
|
|
|
|
|
|
|
num_pages = math.ceil(data['ResultCount'] / float(_SEARCH_PAGE_SIZE))
|
|
num_pages = math.ceil(data['ResultCount'] / float(_SEARCH_PAGE_SIZE))
|
|
|
|
|
|
|
@@ -129,19 +196,22 @@ def search(conn, locality_id):
|
|
|
|
|
|
|
|
page += 1
|
|
page += 1
|
|
|
|
|
|
|
|
|
|
+ print('Search complete. %i records inserted.' % inserted)
|
|
|
|
|
+
|
|
|
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)
|
|
|
|
|
|
|
+ # search(conn, 1265)
|
|
|
pass
|
|
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)
|
|
|
|
|
|
|
|
fetch_properties(conn)
|
|
fetch_properties(conn)
|
|
|
- # process_properties(conn)
|
|
|
|
|
|
|
+ process_properties(conn)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if __name__ == '__main__':
|
|
|
run()
|
|
run()
|