diff --git a/README.md b/README.md index bd47991..433aaff 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ These are the cities currently supported: | Germany | Hamburg | 812 (says 813) | | | Netherlands | Amersfoort | 149 | every monday at 03:00 | `0 3 * * 1` | | Netherlands | Amsterdam | 1328 | every second day of the month at 03:00 | `0 3 2 * *` | -| Netherlands | Arnhem | 121 | | +| Netherlands | Arnhem | 88 | | | Netherlands | Den Haag | 234 (says 241) | every second day of the month at 02:30 | `30 2 2 * *` | | Netherlands | Eindhoven | 180 | every second day of the month at 03:00 | `0 3 2 * *` | | Netherlands | Groningen | 173 | | diff --git a/app/cities/netherlands/arnhem.py b/app/cities/netherlands/arnhem.py index 240b7eb..2f6c7b7 100644 --- a/app/cities/netherlands/arnhem.py +++ b/app/cities/netherlands/arnhem.py @@ -1,20 +1,13 @@ """Manage the location data of Arnhem.""" import datetime -import json -import os -from pathlib import Path import pymysql import pytz -import requests -from dotenv import load_dotenv +from arnhem import ODPArnhem from app.cities import City from app.database import connection, cursor - -load_dotenv() -env_path = Path(".") / ".env" -load_dotenv(dotenv_path=env_path) +from app.helper import centroid class Municipality(City): @@ -29,43 +22,47 @@ def __init__(self) -> None: province_id=6, geo_code="NL-GE", ) + self.limit = 200 self.cbs_code = "0202" - self.local_file = "app/data/parking-arnhem.json" - def download(self) -> None: - """Download the data as JSON file.""" - # Create a variable and pass the url of file to be downloaded - remote_url = ( - f'{os.getenv("ARCGIS_SOURCE")}/6f301547133a4acda9074ec3ca9b075b_0.geojson' - ) - # Make http request for remote file data - data = requests.get(remote_url, timeout=10) - # Save file data to local copy - with Path(self.local_file).open("wb") as file: - file.write(data.content) - print(f"{self.name} - KLAAR met downloaden") + async def async_get_locations(self) -> list: + """Get parking data from API. + + Returns + ------- + List of objects from all parking lots. + """ + async with ODPArnhem() as client: + locations = await client.locations( + limit=self.limit, + set_filter="RVV_SOORT='E6a'", + ) + print(f"{self.name} - data has been retrieved") + return locations - def upload_json(self) -> None: - """Upload the data from the JSON file to the database.""" - with Path(self.local_file).open(encoding="UTF-8") as arnhem_data: - arnhem_obj = json.load(arnhem_data) + def upload_data(self, data_set: list) -> None: + """Upload the data_set to the database. + Args: + ---- + data_set: The data set to upload. + """ count: int = 0 try: - for item in arnhem_obj["features"]: + for item in data_set: count += 1 - location = item["properties"] + # Get the coordinates of the parking lot with centroid + latitude, longitude = centroid(item.coordinates) # Generate unique id - location_id = f"{self.geo_code}-{self.cbs_code}-{location['OBJECTID']}" + location_id = f"{self.geo_code}-{self.cbs_code}-{item.spot_id}" - sql = """INSERT INTO `parking_cities` (id, country_id, province_id, municipality, street, orientation, number, longitude, latitude, visibility, created_at, updated_at) - VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ON DUPLICATE KEY + sql = """INSERT INTO `parking_cities` (id, country_id, province_id, municipality, street, number, longitude, latitude, visibility, created_at, updated_at) + VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ON DUPLICATE KEY UPDATE id=values(id), country_id=values(country_id), province_id=values(province_id), municipality=values(municipality), street=values(street), - orientation=values(orientation), number=values(number), longitude=values(longitude), latitude=values(latitude), @@ -75,11 +72,10 @@ def upload_json(self) -> None: int(self.country_id), int(self.province_id), str(self.name), - str(location["LOCATIE"]), - str(location["TYPE_VAK"]), - int(location["AANTAL"]), - float(location["LON"]), - float(location["LAT"]), + str(item.street), + int(1), + float(longitude), + float(latitude), bool(True), (datetime.datetime.now(tz=pytz.timezone("Europe/Amsterdam"))), (datetime.datetime.now(tz=pytz.timezone("Europe/Amsterdam"))), diff --git a/poetry.lock b/poetry.lock index 9d89a88..7df4684 100644 --- a/poetry.lock +++ b/poetry.lock @@ -122,6 +122,21 @@ files = [ [package.dependencies] frozenlist = ">=1.1.0" +[[package]] +name = "arnhem" +version = "0.1.0" +description = "Asynchronous Python client providing Open Data information of Arnhem" +optional = false +python-versions = ">=3.9,<4.0" +files = [ + {file = "arnhem-0.1.0-py3-none-any.whl", hash = "sha256:9dd8d59c3ee883874efe0a8d185655b385c4f59d09abca0e06abe3a72f4d7813"}, + {file = "arnhem-0.1.0.tar.gz", hash = "sha256:02f4aabffb2ec55c13863730ef57db962ccab90300bbeb786056ed0af1ba52dd"}, +] + +[package.dependencies] +aiohttp = ">=3.0.0" +yarl = ">=1.6.0" + [[package]] name = "astroid" version = "2.15.5" @@ -1393,4 +1408,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "ab4cd3197ee72a7bf3c204565b5f406cbd7e8f36eaa98916b55d211897cf7b7e" +content-hash = "22ec6ca62e98947fb68ce690faa516f23db2c9a70d8e6492c4e546f302c9c79d" diff --git a/pyproject.toml b/pyproject.toml index c8f98b5..e319774 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ pytz = "^2023.3" requests = "^2.31.0" [tool.poetry.group.cities.dependencies] +arnhem = "^0.1.0" brussel = "^0.2.1" dresden = "^0.2.1" dusseldorf = "^1.0.0" diff --git a/run.py b/run.py index b450e10..9ab6ca2 100644 --- a/run.py +++ b/run.py @@ -80,7 +80,7 @@ def provide_city(self, city_name: str) -> object: provided_city = cp.provide_city(selected_city) # Get the data from the API - if selected_city in ["groningen", "arnhem", "zoetermeer"]: + if selected_city in ["groningen", "zoetermeer"]: data_set = None # pylint: disable=C0103 provided_city.download() else: