Skip to content

Commit

Permalink
Add new cities (#5)
Browse files Browse the repository at this point in the history
* Add Arnhem and Eindhoven to project

* 📚 Update the docs
  • Loading branch information
klaasnicolaas authored Oct 9, 2021
1 parent 291a36c commit 2f48765
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
16 changes: 14 additions & 2 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -11,11 +13,21 @@
""" Zoetermeer """
# zoetermeer.download()
# zoetermeer.upload()
# zoetermeer.truncate()

""" Arnhem """
# arnhem.download()
# arnhem.upload()

""" Amsterdam """
# amsterdam.upload()

""" Den Haag """
# den_haag.upload()
# 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")
42 changes: 42 additions & 0 deletions cities/arnhem.py
Original file line number Diff line number Diff line change
@@ -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')
30 changes: 30 additions & 0 deletions cities/eindhoven.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2f48765

Please sign in to comment.