Compare commits
1 commit
91677f56ed
...
8b59d89820
Author | SHA1 | Date | |
---|---|---|---|
8b59d89820 |
1 changed files with 11 additions and 34 deletions
45
main.py
45
main.py
|
@ -189,9 +189,7 @@ 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 (
|
return f"{DateTimeFormatter.format_date(dt)} {DateTimeFormatter.format_time(dt)}"
|
||||||
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:
|
||||||
|
@ -273,9 +271,7 @@ class PollenData:
|
||||||
PollenPeriod(
|
PollenPeriod(
|
||||||
{
|
{
|
||||||
"index": pollen_percentage,
|
"index": pollen_percentage,
|
||||||
"period": DateTimeFormatter.relative_day_to_date(
|
"period": DateTimeFormatter.relative_day_to_date(period_type),
|
||||||
period_type
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -302,9 +298,7 @@ class WeatherService:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@error_handler
|
@error_handler
|
||||||
async def fetch_weather(
|
async def fetch_weather(latitude: str, longitude: str, api_key: str) -> list[WeatherReport]:
|
||||||
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)
|
||||||
|
|
||||||
|
@ -355,8 +349,7 @@ 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)
|
WeatherService._format_daily_period(period) for period in daily_periods
|
||||||
for period in daily_periods
|
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -373,12 +366,8 @@ 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(
|
"sunrise": DateTimeFormatter.format_time(datetime.fromtimestamp(period["sunrise"])),
|
||||||
datetime.fromtimestamp(period["sunrise"])
|
"sunset": DateTimeFormatter.format_time(datetime.fromtimestamp(period["sunset"])),
|
||||||
),
|
|
||||||
"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),
|
||||||
}
|
}
|
||||||
|
@ -516,13 +505,11 @@ 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(
|
WeatherService.fetch_weather(config.latitude, config.longitude, config.weather_api_key),
|
||||||
config.latitude, config.longitude, config.weather_api_key
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
pollen_periods, weather_periods, current_weather_info = (
|
pollen_periods, weather_periods, current_weather_info = DataAggregator.create_periods_lookup(
|
||||||
DataAggregator.create_periods_lookup(pollen_data, weather_data)
|
pollen_data, weather_data
|
||||||
)
|
)
|
||||||
|
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
|
@ -538,22 +525,12 @@ async def get_weather_pollen_report(token: str) -> FinalReport:
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
today_data = DataAggregator.build_daily_data(
|
today_data = DataAggregator.build_daily_data(today_date, pollen_periods, weather_periods)
|
||||||
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_data = DataAggregator.build_daily_data(tomorrow_date, pollen_periods, weather_periods)
|
||||||
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