Compare commits
1 commit
8b59d89820
...
91677f56ed
Author | SHA1 | Date | |
---|---|---|---|
91677f56ed |
1 changed files with 34 additions and 11 deletions
45
main.py
45
main.py
|
@ -189,7 +189,9 @@ class DateTimeFormatter:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def format_datetime(dt: datetime) -> str:
|
def format_datetime(dt: datetime) -> str:
|
||||||
"""Format datetime as date and time string."""
|
"""Format datetime as date and time string."""
|
||||||
return f"{DateTimeFormatter.format_date(dt)} {DateTimeFormatter.format_time(dt)}"
|
return (
|
||||||
|
f"{DateTimeFormatter.format_date(dt)} {DateTimeFormatter.format_time(dt)}"
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def relative_day_to_date(relative_day: str) -> str:
|
def relative_day_to_date(relative_day: str) -> str:
|
||||||
|
@ -271,7 +273,9 @@ class PollenData:
|
||||||
PollenPeriod(
|
PollenPeriod(
|
||||||
{
|
{
|
||||||
"index": pollen_percentage,
|
"index": pollen_percentage,
|
||||||
"period": DateTimeFormatter.relative_day_to_date(period_type),
|
"period": DateTimeFormatter.relative_day_to_date(
|
||||||
|
period_type
|
||||||
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -298,7 +302,9 @@ class WeatherService:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@error_handler
|
@error_handler
|
||||||
async def fetch_weather(latitude: str, longitude: str, api_key: str) -> list[WeatherReport]:
|
async def fetch_weather(
|
||||||
|
latitude: str, longitude: str, api_key: str
|
||||||
|
) -> list[WeatherReport]:
|
||||||
"""Fetch weather data from OpenWeatherMap API."""
|
"""Fetch weather data from OpenWeatherMap API."""
|
||||||
cache_key = (latitude, longitude)
|
cache_key = (latitude, longitude)
|
||||||
|
|
||||||
|
@ -349,7 +355,8 @@ class WeatherService:
|
||||||
"current_pressure": current["pressure"],
|
"current_pressure": current["pressure"],
|
||||||
"current_desc": current["weather"][0]["description"],
|
"current_desc": current["weather"][0]["description"],
|
||||||
"periods": [
|
"periods": [
|
||||||
WeatherService._format_daily_period(period) for period in daily_periods
|
WeatherService._format_daily_period(period)
|
||||||
|
for period in daily_periods
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -366,8 +373,12 @@ class WeatherService:
|
||||||
"high": int(round(period["temp"]["max"])),
|
"high": int(round(period["temp"]["max"])),
|
||||||
"desc": period["weather"][0]["description"],
|
"desc": period["weather"][0]["description"],
|
||||||
"humidity": period["humidity"],
|
"humidity": period["humidity"],
|
||||||
"sunrise": DateTimeFormatter.format_time(datetime.fromtimestamp(period["sunrise"])),
|
"sunrise": DateTimeFormatter.format_time(
|
||||||
"sunset": DateTimeFormatter.format_time(datetime.fromtimestamp(period["sunset"])),
|
datetime.fromtimestamp(period["sunrise"])
|
||||||
|
),
|
||||||
|
"sunset": DateTimeFormatter.format_time(
|
||||||
|
datetime.fromtimestamp(period["sunset"])
|
||||||
|
),
|
||||||
"pressure": period["pressure"],
|
"pressure": period["pressure"],
|
||||||
"period": DateTimeFormatter.format_date(period_dt),
|
"period": DateTimeFormatter.format_date(period_dt),
|
||||||
}
|
}
|
||||||
|
@ -505,11 +516,13 @@ async def get_weather_pollen_report(token: str) -> FinalReport:
|
||||||
|
|
||||||
pollen_data, weather_data = await asyncio.gather(
|
pollen_data, weather_data = await asyncio.gather(
|
||||||
PollenService.fetch_pollen(config.zip_code),
|
PollenService.fetch_pollen(config.zip_code),
|
||||||
WeatherService.fetch_weather(config.latitude, config.longitude, config.weather_api_key),
|
WeatherService.fetch_weather(
|
||||||
|
config.latitude, config.longitude, config.weather_api_key
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
pollen_periods, weather_periods, current_weather_info = DataAggregator.create_periods_lookup(
|
pollen_periods, weather_periods, current_weather_info = (
|
||||||
pollen_data, weather_data
|
DataAggregator.create_periods_lookup(pollen_data, weather_data)
|
||||||
)
|
)
|
||||||
|
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
|
@ -525,12 +538,22 @@ async def get_weather_pollen_report(token: str) -> FinalReport:
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
today_data = DataAggregator.build_daily_data(today_date, pollen_periods, weather_periods)
|
today_data = DataAggregator.build_daily_data(
|
||||||
|
today_date, pollen_periods, weather_periods
|
||||||
|
)
|
||||||
if today_data:
|
if today_data:
|
||||||
result["today"] = today_data
|
result["today"] = today_data
|
||||||
|
|
||||||
tomorrow_data = DataAggregator.build_daily_data(tomorrow_date, pollen_periods, weather_periods)
|
tomorrow_data = DataAggregator.build_daily_data(
|
||||||
|
tomorrow_date, pollen_periods, weather_periods
|
||||||
|
)
|
||||||
if tomorrow_data:
|
if tomorrow_data:
|
||||||
result["tomorrow"] = tomorrow_data
|
result["tomorrow"] = tomorrow_data
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/health")
|
||||||
|
async def health_check() -> dict[str, str]:
|
||||||
|
"""Health check endpoint."""
|
||||||
|
return {"status": "healthy"}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue