Skip to content

Commit

Permalink
Update code (#2)
Browse files Browse the repository at this point in the history
* Update packages

* Update code per city

* Fix typo in filename
  • Loading branch information
klaasnicolaas authored Oct 9, 2021
1 parent 936f642 commit 291a36c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 38 deletions.
File renamed without changes.
8 changes: 7 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
import cities.arnhem as arnhem
import cities.den_haag as den_haag
import cities.amsterdam as amsterdam
import database as database
import asyncio

if __name__ == '__main__':
print("Start program")
print("--- Start program ---")
""" Zoetermeer """
# zoetermeer.download()
# zoetermeer.upload()
# zoetermeer.truncate()

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

""" Den Haag """
# den_haag.upload()
15 changes: 4 additions & 11 deletions cities/amsterdam.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
city = "Amsterdam"

def upload():
"""Upload the data from the JSON file to the database."""

amsterdam_file = "data/parking-amsterdam.json"
amsterdam_data = open(amsterdam_file).read()
amsterdam_obj = json.loads(amsterdam_data)
Expand All @@ -19,26 +21,17 @@ def upload():
latitude = latitude.replace(']', '')

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["properties"]["straatnaam"]), str(item["properties"]["type"]), int(item["properties"]["aantal"]), float(longitude), float(latitude), bool(True), (datetime.datetime.now()), (datetime.datetime.now()))
val = (id, str(city), str(item["straatnaam"]), str(item["type"]), int(item["aantal"]), float(longitude), float(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 truncate():
try:
sql = "DELETE FROM `parking_cities` WHERE `city`=%s"
cursor.execute(sql, city)
connection.commit()
except Exception as e:
print(f'MySQL error: {e}')
finally:
print(f'{city} - verwijderen van de data')

def update():
return
17 changes: 4 additions & 13 deletions cities/den_haag.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import json, datetime, uuid

from pymysql import NULL

from database import connection, cursor

city = "Den Haag"

def upload():
"""Upload the data from the JSON file to the database."""

den_haag_file = "data/parking-denhaag.json"
den_haag_data = open(den_haag_file).read()
den_haag_obj = json.loads(den_haag_data)
Expand All @@ -21,27 +21,18 @@ def upload():
latitude = latitude.replace(']', '')

id = uuid.uuid4().hex[:8]
item = item["properties"]

sql = """INSERT INTO `parking_cities` (`id`, `city`, `orientation`, `number`, `longitude`, `latitude`, `visibility`, `created_at`, `updated_at`)
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)"""
val = (id, str(city), str(item["properties"]["ORIENTATIE"]), number(item["properties"]["CAPACITEIT"]), float(longitude), float(latitude), bool(True), (datetime.datetime.now()), (datetime.datetime.now()))
val = (id, str(city), str(item["ORIENTATIE"]), number(item["CAPACITEIT"]), float(longitude), float(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 truncate():
try:
sql = "DELETE FROM `parking_cities` WHERE `city`=%s"
cursor.execute(sql, city)
connection.commit()
except Exception as e:
print(f'MySQL error: {e}')
finally:
print(f'{city} - verwijderen van de data')

def number(value):
if value is None:
return 1
Expand Down
18 changes: 6 additions & 12 deletions cities/zoetermeer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
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")}/77cd514505dd40589f52f99d0e5edd6a_0.geojson'
url = f'{os.getenv("SOURCE")}/308bb3581ba646afad6f776a8f7e4e67_0.geojson'
# Copy a network object to a local file
urllib.request.urlretrieve(url, r'data/parking-zoetermeer.json')
urllib.request.urlretrieve(url, 'data/parking-zoetermeer.json')
print(f'{city} - KLAAR met downloaden')

def upload():
"""Upload the data from the JSON file to the database."""

zoetermeer_file = "data/parking-zoetermeer.json"
zoetermeer_data = open(zoetermeer_file).read()
zoetermeer_obj = json.loads(zoetermeer_data)
Expand All @@ -36,15 +40,5 @@ def upload():
finally:
print(f'{city} - KLAAR met updaten van database')

def truncate():
try:
sql = "DELETE FROM `parking_cities` WHERE `city`=%s"
cursor.execute(sql, city)
connection.commit()
except Exception as e:
print(f'MySQL error: {e}')
finally:
print(f'{city} - verwijderen van de data')

def update():
return
17 changes: 16 additions & 1 deletion database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,19 @@

# Connect to MySQL
connection = pymysql.connect(host=DB_SERVER, port=DB_PORT, user=DB_USERNAME, password=DB_PASSWORD, database=DATABASE)
cursor = connection.cursor()
cursor = connection.cursor()

def truncate(city):
"""Remove all data from a city.
Args:
city: The name of the city, it's case sensitive!
"""
try:
sql = "DELETE FROM `parking_cities` WHERE `city`=%s"
cursor.execute(sql, city)
connection.commit()
except Exception as e:
print(f'MySQL error: {e}')
finally:
print(f'{city} - verwijderen van de data')
12 changes: 12 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
aiohttp==3.7.4.post0
async-timeout==3.0.1
attrs==21.2.0
certifi==2021.10.8
chardet==4.0.0
charset-normalizer==2.0.6
idna==3.2
multidict==5.2.0
parking-eindhoven==1.0.0
PyMySQL==1.0.2
python-dotenv==0.19.0
typing-extensions==3.10.0.2
urllib3==1.26.7
yarl==1.7.0

0 comments on commit 291a36c

Please sign in to comment.