From 66d96d404a82206eeb25d90a205e292e0fd9e4ce Mon Sep 17 00:00:00 2001 From: motty Date: Sat, 18 Nov 2023 15:06:40 +0000 Subject: [PATCH] add weather icon in calendar --- src/kb_2315/backend/api/endpoints/calendar.py | 28 +++++++----- src/kb_2315/backend/weather/code2icon.py | 45 +++++++++++++++++++ 2 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 src/kb_2315/backend/weather/code2icon.py diff --git a/src/kb_2315/backend/api/endpoints/calendar.py b/src/kb_2315/backend/api/endpoints/calendar.py index a5c8b89..804369b 100644 --- a/src/kb_2315/backend/api/endpoints/calendar.py +++ b/src/kb_2315/backend/api/endpoints/calendar.py @@ -4,8 +4,9 @@ from fastapi.responses import PlainTextResponse from icalendar import Calendar, Event -from kb_2315.backend.crud import crud_sensor, crud_session, crud_shoe +from kb_2315.backend.crud import crud_session, crud_shoe from kb_2315.backend.models import Session +from kb_2315.backend.weather.code2icon import weather_code2icon router = APIRouter() @@ -14,27 +15,32 @@ @router.get("/") def get_calendar(shoe_id: int | None = None) -> PlainTextResponse: JST = timezone(timedelta(hours=+9), "JST") + sessions: list[Session] = crud_session.search_session_by(shoe_id=shoe_id) - if shoe_id is None: - shoe_name: str = "靴" - sessions: list[Session] = crud_session.search_session_by() - else: - shoe_name = crud_shoe.search_shoe_by(shoe_id=shoe_id)[0].name - sessions = crud_session.search_session_by(shoe_id=shoe_id) + shoe_names: dict[int, str] = {} + + def _search_shoes(shoe_id: int | None) -> str: + if shoe_id is None: + return "靴" + elif shoe_id in shoe_names.keys(): + return shoe_names[shoe_id] + else: + shoe_names[shoe_id] = crud_shoe.search_shoe_by(shoe_id=shoe_id)[0].name + return shoe_names[shoe_id] cal: Calendar = Calendar() - cal["summary"] = f"{shoe_name} の乾燥記録" + cal["summary"] = f"{_search_shoes(shoe_id=shoe_id)} の乾燥記録" cal["scale"] = "GREGORIAN" cal["method"] = "PUBLISH" - cal["X-WR-CALNAME"] = f"{shoe_name} の乾燥記録" + cal["X-WR-CALNAME"] = f"{_search_shoes(shoe_id=shoe_id)} の乾燥記録" cal["X-WR-TIMEZONE"] = "Asia/Tokyo" for s in sessions: try: - last_time: datetime = crud_sensor.search_sensor_by(session_id=s.session_id)[0].time + last_time: datetime = s.created_at e: Event = Event( - SUMMARY=f"{crud_shoe.search_shoe_by(shoe_id= s.shoe_id)[0].name} を履いた", + SUMMARY=f"天気: { weather_code2icon(s.weather_code)}, {_search_shoes(shoe_id=s.shoe_id)} を履いた", DTSTART=datetime( last_time.year, last_time.month, last_time.day, time(7, 0).hour, time(7, 0).minute, tzinfo=JST ) diff --git a/src/kb_2315/backend/weather/code2icon.py b/src/kb_2315/backend/weather/code2icon.py new file mode 100644 index 0000000..42b43ac --- /dev/null +++ b/src/kb_2315/backend/weather/code2icon.py @@ -0,0 +1,45 @@ +def weather_code2icon(weather_code: int) -> str: + """ + WMO 天気コードを4分類にする + https://open-meteo.com/en/docs + https://www.jodc.go.jp/data_format/weather-code_j.html#:~:text=%E7%8F%BE%E5%9C%A8%E5%A4%A9%E5%80%99(Present%20Weather)%20(WMO%20Code%204677) + """ + if weather_code in ["0", "1"]: + # 晴れ + return "☀" + + elif weather_code in ["2", "3"]: + # 曇り + return "☁" + + elif weather_code in [ + # 雨 + "45", + "48", + "51", + "53", + "55", + "56", + "57", + "61", + "63", + "65", + "66", + "67", + "80", + "81", + "82", + "95", + "96", + "99", + ]: + return "🌧" + + elif weather_code in ["71", "73", "75", "77", "85", "86"]: + # 雪 + return "🌨" + + # その他 + else: + # その他,曇りとする + return "☁"