|
|
@@ -1,34 +1,94 @@
|
|
|
+import json
|
|
|
+import logging
|
|
|
+import datetime
|
|
|
+
|
|
|
import requests
|
|
|
+
|
|
|
import db
|
|
|
import config
|
|
|
|
|
|
import init_localities
|
|
|
|
|
|
-def search(locality_id):
|
|
|
- request = {
|
|
|
- 'AddedSince': "",
|
|
|
- 'ApiKey': config.API_KEY,
|
|
|
- 'Page': 1,
|
|
|
- 'PageSize': 20,
|
|
|
- 'RequestTypeId': 2,
|
|
|
- 'SearchRequest': config.SEARCH_PARAMS,
|
|
|
- 'SortColumn': 2,
|
|
|
- 'SortDirection': 2,
|
|
|
- 'endpoint': {
|
|
|
- 'name': "Search",
|
|
|
- 'paramsName': "SearchRequest",
|
|
|
- },
|
|
|
- 'params': {
|
|
|
- 'RegionId': locality_id
|
|
|
- },
|
|
|
- }
|
|
|
+def _now():
|
|
|
+ return datetime.datetime.now().strftime(r'%Y-%m-%d %M:%H:%S')
|
|
|
+
|
|
|
+def get_property(conn, property_id):
|
|
|
+ resp = requests.get('https://api.myhome.ie/brochure/%i?ApiKey=%s&RequestTypeId=1' % (property_id, config.API_KEY))
|
|
|
+
|
|
|
+ print(json.dumps(resp.json()))
|
|
|
+
|
|
|
+ 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 search(conn, locality_id):
|
|
|
+ logging.info('Searching %i...', locality_id)
|
|
|
|
|
|
- resp = requests.post('https://api.myhome.ie/search?ApiKey=%s' % config.API_KEY, body=request)
|
|
|
+ page = 1
|
|
|
+ found_properties = set()
|
|
|
+
|
|
|
+ while True:
|
|
|
+ body = {
|
|
|
+ 'AddedSince': "",
|
|
|
+ 'ApiKey': config.API_KEY,
|
|
|
+ 'Page': page,
|
|
|
+ 'PageSize': 50,
|
|
|
+ 'RequestTypeId': 2,
|
|
|
+ 'SearchRequest': config.SEARCH_PARAMS,
|
|
|
+ 'SortColumn': 2,
|
|
|
+ 'SortDirection': 2,
|
|
|
+ 'endpoint': {
|
|
|
+ 'name': "Search",
|
|
|
+ 'paramsName': "SearchRequest",
|
|
|
+ },
|
|
|
+ 'params': {
|
|
|
+ 'RegionId': locality_id
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ headers = {
|
|
|
+ 'content-type': 'application/json',
|
|
|
+ 'accept': 'application/json',
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ print('Fetching page %i...' % page)
|
|
|
+
|
|
|
+ resp = requests.post('https://api.myhome.ie/search?ApiKey=%s' % config.API_KEY, json=body, headers=headers)
|
|
|
+
|
|
|
+ resp.raise_for_status()
|
|
|
+
|
|
|
+ data = resp.json()
|
|
|
+
|
|
|
+ if not isinstance(data, dict):
|
|
|
+ print('Error: %s' % data)
|
|
|
+ return
|
|
|
+
|
|
|
+ stmt = 'insert into search_results (time, property_id, raw_json) VALUES '
|
|
|
+ params = []
|
|
|
+
|
|
|
+ for result in data.get('SearchResults', []):
|
|
|
+ found_properties.add(result['id'])
|
|
|
+ stmt += '(?, ?, ?),'
|
|
|
+ params.extend([_now(), result['id'], json.dumps(result)])
|
|
|
+
|
|
|
+ conn.execute(stmt[:-1], *params)
|
|
|
+
|
|
|
+ if page*500 > data['ResultCount']:
|
|
|
+ break
|
|
|
+
|
|
|
+ page += 1
|
|
|
+
|
|
|
+ known_properties = set(conn.select('select id from properties'))
|
|
|
+
|
|
|
+ to_get = found_properties.difference(known_properties)
|
|
|
|
|
|
- # resp.raise_for_status()
|
|
|
+ get_property(conn, list(to_get)[0])
|
|
|
|
|
|
- print(resp.content)
|
|
|
+ print('Getting %i properties...' % len(to_get))
|
|
|
|
|
|
def run():
|
|
|
with db.connect() as conn:
|
|
|
@@ -39,7 +99,7 @@ def run():
|
|
|
print('You must set MONITORED_LOCATIONS. Possible values: ')
|
|
|
init_localities.init_localities(conn)
|
|
|
|
|
|
- search(1265)
|
|
|
+ search(conn, 1265)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
run()
|