diff --git a/projects/project-3/openapi/db_init.py b/projects/project-3/openapi/db_init.py index f42df31..6d4a358 100644 --- a/projects/project-3/openapi/db_init.py +++ b/projects/project-3/openapi/db_init.py @@ -77,6 +77,14 @@ def init_database(): lon REAL, id_num INTEGER AS (CAST(SUBSTR(id, 12) as INTEGER)) STORED )""") + conn.execute("""CREATE TABLE IF NOT EXISTS accidents( + id INTEGER PRIMARY KEY, + lat REAL, + lon REAL, + location TEXT, + date TEXT, + severity TEXT + )""") conn.commit() conn.close() LOG.info("Tables created") @@ -97,12 +105,36 @@ def import_bikepoints(): conn = sqlite3.connect("bike-data.db", timeout=300) points = json.loads(requests.get("https://api.tfl.gov.uk/BikePoint").text) points = list(map(lambda p: (p['id'], p['commonName'], p['lat'], p['lon']), points)) + + LOG.info(f"Writing {len(points)} bikepoints to DB") conn.executemany("INSERT OR IGNORE INTO bike_points VALUES (?, ?, ?, ?)", points) conn.commit() conn.close() LOG.info("Bikepoints imported") +def import_accidents(year): + LOG.info("Importing accidents") + conn = sqlite3.connect("bike-data.db", timeout=300) + + def filter_pedal_cycles(accident): + for vehicle in accident['vehicles']: + if vehicle['type'] == "PedalCycle": + return True + return False + + accidents = requests.get(f"https://api.tfl.gov.uk/AccidentStats/{year}").text + accidents = json.loads(accidents) + accidents = list(filter(filter_pedal_cycles, accidents)) + accidents = list(map(lambda a: (a['id'], a['lat'], a['lon'], a['location'], a['date'], a['severity']), accidents)) + + LOG.info(f"Writing {len(accidents)} bike accidents to DB") + conn.executemany("INSERT OR IGNORE INTO accidents VALUES (?, ?, ?, ?, ?, ?)", accidents) + conn.commit() + conn.close() + LOG.info("Accidents importet") + + def import_usage_stats_file(export_file: ApiExportFile): conn = sqlite3.connect("bike-data.db", timeout=300) @@ -162,10 +194,12 @@ def main(): for file in all_files: import_usage_stats_file(file) - # Import Bikepoints - import_bikepoints() # Create search-index for faster querying create_indexes() + # Import Bikepoints + import_bikepoints() + # Import bike accidents + import_accidents(2019) if __name__ == "__main__":