From 2f4876549817f96a529536e363350ccfef383c16 Mon Sep 17 00:00:00 2001 From: Klaas Schoute Date: Sat, 9 Oct 2021 05:22:34 +0200 Subject: [PATCH] Add new cities (#5) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add Arnhem and Eindhoven to project * 📚 Update the docs --- README.md | 10 ++++++++++ __init__.py | 16 ++++++++++++++-- cities/arnhem.py | 42 ++++++++++++++++++++++++++++++++++++++++++ cities/eindhoven.py | 30 ++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 cities/arnhem.py create mode 100644 cities/eindhoven.py diff --git a/README.md b/README.md index 539c4b2..f2ce455 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,16 @@ This project makes it possible to download and upload parking data from municipalities to NIPkaart. +## Supported cities + +These are the cities currently supported: + +- Amsterdam +- Arnhem +- Den Haag +- Eindhoven +- Zoetermeer + ## Contributing Would you like to contribute to the development of this project? Then read the prepared [contribution guidelines](CONTRIBUTING.md) and go ahead! diff --git a/__init__.py b/__init__.py index 15d2d2e..444cb1a 100644 --- a/__init__.py +++ b/__init__.py @@ -3,6 +3,8 @@ import cities.arnhem as arnhem import cities.den_haag as den_haag import cities.amsterdam as amsterdam +import cities.eindhoven as eindhoven + import database as database import asyncio @@ -11,11 +13,21 @@ """ Zoetermeer """ # zoetermeer.download() # zoetermeer.upload() - # zoetermeer.truncate() + """ Arnhem """ # arnhem.download() + # arnhem.upload() + """ Amsterdam """ # amsterdam.upload() """ Den Haag """ - # den_haag.upload() \ No newline at end of file + # den_haag.upload() + + """ Eindhoven """ + # data_set = asyncio.run(eindhoven.async_get_locations(200, "Parkeerplaats Gehandicapten")) + # print(data_set) + # eindhoven.upload(data_set) + + # Database + # database.truncate("Eindhoven") diff --git a/cities/arnhem.py b/cities/arnhem.py new file mode 100644 index 0000000..64199ec --- /dev/null +++ b/cities/arnhem.py @@ -0,0 +1,42 @@ +import json, datetime, uuid, os +import urllib.request + +from database import connection, cursor +from dotenv import load_dotenv +from pathlib import Path + +city = "Arnhem" + +load_dotenv() +env_path = Path('.')/'.env' +load_dotenv(dotenv_path=env_path) + +def download(): + """Download the data as JSON file.""" + + # Create a variable and pass the url of file to be downloaded + url = f'{os.getenv("SOURCE")}/6f301547133a4acda9074ec3ca9b075b_0.geojson' + # Copy a network object to a local file + urllib.request.urlretrieve(url, 'data/parking-arnhem.json') + print(f'{city} - KLAAR met downloaden') + +def upload(): + """Upload the data from the JSON file to the database.""" + + arnhem_file = "data/parking-arnhem.json" + arnhem_data = open(arnhem_file).read() + arnhem_obj = json.loads(arnhem_data) + try: + for item in arnhem_obj["features"]: + id = uuid.uuid4().hex[:8] + item = item["properties"] + + sql= """INSERT INTO `parking_cities` (`id`, `city`, `street`, `orientation`, `number`, `longitude`, `latitude`, `visibility`, `created_at`, `updated_at`) + VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""" + val = (id, str(city), str(item["LOCATIE"]), str(item["TYPE_VAK"]), int(item["AANTAL"]), float(item["LON"]), float(item["LAT"]), bool(True), (datetime.datetime.now()), (datetime.datetime.now())) + cursor.execute(sql, val) + connection.commit() + except Exception as e: + print(f'MySQL error: {e}') + finally: + print(f'{city} - KLAAR met updaten van database') \ No newline at end of file diff --git a/cities/eindhoven.py b/cities/eindhoven.py new file mode 100644 index 0000000..8b73e97 --- /dev/null +++ b/cities/eindhoven.py @@ -0,0 +1,30 @@ +import uuid, parking_eindhoven, datetime, aiohttp + +from database import connection, cursor + +city = "Eindhoven" + +async def async_get_locations(number, type): + """Get parking data from API.""" + + async with aiohttp.ClientSession() as client: + return await parking_eindhoven.get_locations(number, type, client) + +def upload(data_set): + """Upload the data_set to the database.""" + + try: + for item in data_set: + id = uuid.uuid4().hex[:8] + sql = """INSERT INTO `parking_cities` (`id`, `city`, `street`, `orientation`, `number`, `longitude`, `latitude`, `visibility`, `created_at`, `updated_at`) + VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""" + val = (id, str(city), str(item.street), None, item.number, float(item.longitude), float(item.latitude), bool(True), (datetime.datetime.now()), (datetime.datetime.now())) + cursor.execute(sql, val) + connection.commit() + except Exception as e: + print(f'MySQL error: {e}') + finally: + print(f'{city} - KLAAR met updaten van database') + +def update(): + return \ No newline at end of file