-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
93 lines (75 loc) · 2.83 KB
/
main.py
File metadata and controls
93 lines (75 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""Upload tool for off-street parking into NIPKaart system."""
import asyncio
import os
import time
from datetime import datetime
from pathlib import Path
import pytz
from dotenv import load_dotenv
from app.cities.germany import hamburg
from app.cities.netherlands import amsterdam
from app.database import test_connection
from app.helpers import test_data
CITY = os.getenv("CITY")
WAIT_TIME = int(os.getenv("WAIT_TIME"))
TESTING = False
class CityProvider:
"""Class to provide the correct city."""
def provide_city(self, city_name: str) -> object:
"""Provide the correct city.
Args:
----
city_name (str): The city to provide.
Returns:
-------
city_class (class): The class of the city.
"""
match city_name:
case "amsterdam":
city_class = amsterdam.Municipality()
case "hamburg":
city_class = hamburg.Municipality()
case _:
msg = f"{city_name} is not a valid city."
raise ValueError(msg)
return city_class
if __name__ == "__main__":
# Load environment variables
load_dotenv()
env_path = Path() / ".env"
load_dotenv(dotenv_path=env_path)
cp = CityProvider()
print("--- Start program ---")
print()
# Get the city from the environment variables
selected_city: str = os.getenv("CITY").lower()
provided_city = cp.provide_city(selected_city)
if TESTING:
data_set = asyncio.run(provided_city.async_get_locations())
test_data(data_set)
test_connection()
else:
while True:
LOCAL_ZONE = pytz.timezone("Europe/Amsterdam")
CURRENT_TIME = datetime.now(tz=LOCAL_ZONE).strftime("%H:%M:%S")
print(f"-------- START {selected_city} ---------")
# Check if the city is in the list of cities
if selected_city in ["hamburg"]:
local_time: datetime = datetime.now(tz=LOCAL_ZONE)
if local_time.hour >= 1:
# Get the data from the selected city
data_set = asyncio.run(provided_city.async_get_locations())
# Upload the data to the database
provided_city.upload_data(data_set, CURRENT_TIME)
else:
print(
"Hamburg: Not updating database, time between 00:00 and 01:00.",
)
else:
# Get the data from the selected city
data_set = asyncio.run(provided_city.async_get_locations())
# Upload the data to the database
provided_city.upload_data(data_set, CURRENT_TIME)
# Wait for the next update
print(f"--------- DONE {selected_city} ---------")
time.sleep(60 * WAIT_TIME)