Skip to content

Commit

Permalink
add weather icon in calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
motty-mio2 committed Nov 18, 2023
1 parent 7c2e15e commit 66d96d4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/kb_2315/backend/api/endpoints/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
)
Expand Down
45 changes: 45 additions & 0 deletions src/kb_2315/backend/weather/code2icon.py
Original file line number Diff line number Diff line change
@@ -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 "☁"

0 comments on commit 66d96d4

Please sign in to comment.