|
|
@@ -0,0 +1,56 @@
|
|
|
+import requests
|
|
|
+import config
|
|
|
+
|
|
|
+def _extract_localities(api_response):
|
|
|
+ res = []
|
|
|
+
|
|
|
+ for v in api_response.values():
|
|
|
+ if isinstance(v, list) and v and v[0].get('LocalityId') and v[0].get('Name'):
|
|
|
+ res.extend(v)
|
|
|
+
|
|
|
+ return res
|
|
|
+
|
|
|
+def _init_relationships(conn, locality_id):
|
|
|
+ resp = requests.get('https://api.myhome.ie/localities/%s?ApiKey=%s&classId=1' % (locality_id, config.API_KEY))
|
|
|
+ resp.raise_for_status()
|
|
|
+ data = resp.json()
|
|
|
+
|
|
|
+ print('checking %i' % locality_id)
|
|
|
+
|
|
|
+
|
|
|
+ print(data)
|
|
|
+ for obj in _extract_localities(data):
|
|
|
+ pk = '%i_%i' % (obj['LocalityId'], locality_id)
|
|
|
+ conn.execute('replace into locality_relationships (id, parent, child) values (?,?, ?)', pk, locality_id, obj['LocalityId'])
|
|
|
+
|
|
|
+
|
|
|
+def init_localities(conn, locality_id=None):
|
|
|
+ known = set(conn.select('select id from localities'))
|
|
|
+
|
|
|
+ to_insert = []
|
|
|
+
|
|
|
+ if locality_id is None:
|
|
|
+ resp = requests.get('https://www.myhome.ie/app/assets/data/regionsIreland.json')
|
|
|
+ resp.raise_for_status()
|
|
|
+ data = resp.json()
|
|
|
+
|
|
|
+ for x in data:
|
|
|
+ print('%s: %s' % (x['id'], x['name']))
|
|
|
+
|
|
|
+ else:
|
|
|
+ resp = requests.get('https://api.myhome.ie/localities/%s?ApiKey=%s&classId=1' % (locality_id, config.API_KEY))
|
|
|
+ resp.raise_for_status()
|
|
|
+ data = resp.json()
|
|
|
+
|
|
|
+ to_insert.extend(_extract_localities(data))
|
|
|
+
|
|
|
+ to_insert = [x for x in to_insert if x['LocalityId'] not in known]
|
|
|
+
|
|
|
+ for i, x in enumerate(to_insert):
|
|
|
+ if locality_id:
|
|
|
+ pk = '%i_%i' % (x['LocalityId'], locality_id)
|
|
|
+ conn.execute('replace into locality_relationships (id, parent, child) values (?,?, ?)', pk, locality_id, x['LocalityId'])
|
|
|
+ _init_relationships(conn, x['LocalityId'])
|
|
|
+
|
|
|
+ conn.execute('replace into localities (id, name) values (?,?)', x['LocalityId'], x['Name'])
|
|
|
+ print('Inserted %i/%i' % (i, len(to_insert)))
|