This repository was archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
95 lines (80 loc) · 3.22 KB
/
main.py
File metadata and controls
95 lines (80 loc) · 3.22 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
94
95
# Import API handlers
import server.handle_forecast5_api
# Import the CLI handler
import frontend.interactive_calendar_cli
import frontend.city_name_choose_cli
import frontend.choose_time_cli
import frontend.display_forecast_cli
# Import utilities
import utils.epoch_handlers
# Loading the dotenv configuration
from dotenv import load_dotenv
load_dotenv()
# Import datetime
import datetime
# Setting the API_KEY form the dotenv file
import os
API_KEY = os.getenv("OPENWEATHERMAP_API_KEY")
def main():
try:
# Ask the user for the city name
city_name = frontend.city_name_choose_cli.return_city_name()
if city_name.get("error"):
raise Exception(city_name.get("message"))
except Exception as e:
print("Error in city_name_choose_cli.return_city_name():", str(e))
print("Please try again:)")
return
try:
# API call to get the forecast
forecast_json = server.handle_forecast5_api.get_city_forecast(city_name.get("city_name"), API_KEY)
if forecast_json.get("cod") != '200':
raise Exception("Error with OpenWeatherMap API: " + forecast_json.get("message"))
except Exception as e:
print("Error in server.handle_forecast5_api.get_city_forecast():", str(e))
print("Please try again:)")
return
try:
# Get the epochs from the forecast
epochs = server.handle_forecast5_api.get_epochs_from_forecast(forecast_json)
except Exception as e:
print("Error in server.handle_forecast5_api.get_epochs_from_forecast():", str(e))
print("Please try again:)")
return
try:
# Get unique date epochs for available dates
unique_date_epochs = utils.epoch_handlers.get_unique_date_epoch(epochs)
except Exception as e:
print("Error in utils.epoch_handlers.get_unique_date_epoch():", str(e))
print("Please try again:)")
return
try:
# Send the available dates to the calendar handler and get the final selected date
selected_date = frontend.interactive_calendar_cli.get_user_input_calendar(unique_date_epochs, None)
except Exception as e:
print("Error in frontend.interactive_calendar_cli.get_user_input_calendar():", str(e))
print("Please try again:)")
return
try:
# Get all epochs for the selected date
selected_date_epochs = utils.epoch_handlers.get_epochs_with_date(epochs, selected_date)
except Exception as e:
print("Error in utils.epoch_handlers.get_epochs_with_date():", str(e))
print("Please try again:)")
return
try:
# Ask the user for the time
selected_time = frontend.choose_time_cli.ask_for_preferred_time(selected_date_epochs)
except Exception as e:
print("Error in frontend.choose_time_cli.ask_for_preferred_time():", str(e))
print("Please try again:)")
return
try:
# Display the final weather forecast
frontend.display_forecast_cli.display_weather_forecast_final(selected_time, forecast_json)
except Exception as e:
print("Error in frontend.display_forecast_cli.display_weather_forecast_final():", str(e))
print("Please try again:)")
return
if __name__ == "__main__":
main()