Spaces:
Running
Running
Update jihou.py
Browse files
jihou.py
CHANGED
|
@@ -15,6 +15,8 @@ HEADERS = {
|
|
| 15 |
|
| 16 |
JST = ZoneInfo("Asia/Tokyo")
|
| 17 |
|
|
|
|
|
|
|
| 18 |
WEATHER_CODE_MAP = {
|
| 19 |
0: "☀️晴れ",
|
| 20 |
1: "☁️曇り",
|
|
@@ -29,14 +31,32 @@ WEATHER_CODE_MAP = {
|
|
| 29 |
}
|
| 30 |
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
def post_group_message(chat_id, body):
|
| 33 |
url = f"{BASE_URL}/groups/{chat_id}/messages"
|
| 34 |
r = requests.post(url, headers=HEADERS, json=body)
|
| 35 |
r.raise_for_status()
|
| 36 |
return r.json()
|
| 37 |
|
|
|
|
| 38 |
|
| 39 |
-
# ---------- 時報(既存機能) ----------
|
| 40 |
|
| 41 |
def create_time_signal_body():
|
| 42 |
now = datetime.now(JST)
|
|
@@ -56,14 +76,8 @@ def create_time_signal_body():
|
|
| 56 |
"blocks": [{"type": "text", "value": message}],
|
| 57 |
}
|
| 58 |
|
|
|
|
| 59 |
|
| 60 |
-
def wait_until_next_hour():
|
| 61 |
-
now = datetime.now(JST)
|
| 62 |
-
next_hour = now.replace(minute=0, second=0, microsecond=0) + timedelta(hours=1)
|
| 63 |
-
time.sleep((next_hour - now).total_seconds())
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
# ---------- 天気予報(新機能) ----------
|
| 67 |
|
| 68 |
def load_todoufuken():
|
| 69 |
with open("todoufuken.json", encoding="utf-8") as f:
|
|
@@ -90,68 +104,63 @@ def fetch_weather(todoufuken):
|
|
| 90 |
|
| 91 |
def create_weather_body():
|
| 92 |
now = datetime.now(JST)
|
| 93 |
-
|
| 94 |
|
| 95 |
todoufuken = load_todoufuken()
|
| 96 |
data = fetch_weather(todoufuken)
|
| 97 |
|
| 98 |
-
lines = ["<b>☀️ 本日の天気予報(
|
| 99 |
|
| 100 |
for idx, pref in enumerate(todoufuken):
|
| 101 |
hourly = data[idx]["hourly"]
|
| 102 |
-
|
| 103 |
times = hourly["time"]
|
| 104 |
-
|
|
|
|
| 105 |
|
| 106 |
for i, t in enumerate(times):
|
| 107 |
-
t_jst =
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
return {
|
| 129 |
"requestId": f"desk-web-weather-{int(time.time())}",
|
| 130 |
"blocks": [{"type": "text", "value": "\n".join(lines)}],
|
| 131 |
}
|
| 132 |
|
| 133 |
-
|
| 134 |
-
def wait_until_7am():
|
| 135 |
-
now = datetime.now(JST)
|
| 136 |
-
target = now.replace(hour=7, minute=0, second=0, microsecond=0)
|
| 137 |
-
if now >= target:
|
| 138 |
-
target += timedelta(days=1)
|
| 139 |
-
time.sleep((target - now).total_seconds())
|
| 140 |
|
| 141 |
|
| 142 |
-
# ---------- メインループ ----------
|
| 143 |
-
|
| 144 |
def main_loop():
|
| 145 |
while True:
|
| 146 |
try:
|
| 147 |
now = datetime.now(JST)
|
| 148 |
|
| 149 |
-
#
|
| 150 |
if now.minute == 0:
|
| 151 |
body = create_time_signal_body()
|
| 152 |
post_group_message(TARGET_GROUP_CHAT_ID, body)
|
| 153 |
|
| 154 |
-
# 午後2時
|
| 155 |
if now.hour == 14 and now.minute == 9:
|
| 156 |
body = create_weather_body()
|
| 157 |
post_group_message(TARGET_GROUP_CHAT_ID, body)
|
|
@@ -163,6 +172,5 @@ def main_loop():
|
|
| 163 |
time.sleep(10)
|
| 164 |
|
| 165 |
|
| 166 |
-
|
| 167 |
if __name__ == "__main__":
|
| 168 |
main_loop()
|
|
|
|
| 15 |
|
| 16 |
JST = ZoneInfo("Asia/Tokyo")
|
| 17 |
|
| 18 |
+
# ---------------- 天気コード ----------------
|
| 19 |
+
|
| 20 |
WEATHER_CODE_MAP = {
|
| 21 |
0: "☀️晴れ",
|
| 22 |
1: "☁️曇り",
|
|
|
|
| 31 |
}
|
| 32 |
|
| 33 |
|
| 34 |
+
def format_weather_code(code: int) -> str:
|
| 35 |
+
if code < 10:
|
| 36 |
+
return WEATHER_CODE_MAP.get(code, str(code))
|
| 37 |
+
|
| 38 |
+
main = code // 10
|
| 39 |
+
sub = code % 10
|
| 40 |
+
|
| 41 |
+
main_str = WEATHER_CODE_MAP.get(main)
|
| 42 |
+
sub_str = WEATHER_CODE_MAP.get(sub)
|
| 43 |
+
|
| 44 |
+
if main_str and sub_str:
|
| 45 |
+
return f"{main_str}、{sub_str}"
|
| 46 |
+
|
| 47 |
+
return str(code)
|
| 48 |
+
|
| 49 |
+
# ---------------- Channel 投稿 ----------------
|
| 50 |
+
|
| 51 |
+
|
| 52 |
def post_group_message(chat_id, body):
|
| 53 |
url = f"{BASE_URL}/groups/{chat_id}/messages"
|
| 54 |
r = requests.post(url, headers=HEADERS, json=body)
|
| 55 |
r.raise_for_status()
|
| 56 |
return r.json()
|
| 57 |
|
| 58 |
+
# ---------------- 時報 ----------------
|
| 59 |
|
|
|
|
| 60 |
|
| 61 |
def create_time_signal_body():
|
| 62 |
now = datetime.now(JST)
|
|
|
|
| 76 |
"blocks": [{"type": "text", "value": message}],
|
| 77 |
}
|
| 78 |
|
| 79 |
+
# ---------------- 天気予報 ----------------
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
def load_todoufuken():
|
| 83 |
with open("todoufuken.json", encoding="utf-8") as f:
|
|
|
|
| 104 |
|
| 105 |
def create_weather_body():
|
| 106 |
now = datetime.now(JST)
|
| 107 |
+
today_7am = now.replace(hour=7, minute=0, second=0, microsecond=0)
|
| 108 |
|
| 109 |
todoufuken = load_todoufuken()
|
| 110 |
data = fetch_weather(todoufuken)
|
| 111 |
|
| 112 |
+
lines = ["<b>☀️ 本日の天気予報(7時以降・1時間ごと)</b>"]
|
| 113 |
|
| 114 |
for idx, pref in enumerate(todoufuken):
|
| 115 |
hourly = data[idx]["hourly"]
|
|
|
|
| 116 |
times = hourly["time"]
|
| 117 |
+
|
| 118 |
+
lines.append(f"\n<b>{pref['name']}:</b>")
|
| 119 |
|
| 120 |
for i, t in enumerate(times):
|
| 121 |
+
t_jst = (
|
| 122 |
+
datetime.fromisoformat(t)
|
| 123 |
+
.replace(tzinfo=ZoneInfo("UTC"))
|
| 124 |
+
.astimezone(JST)
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
if t_jst < today_7am:
|
| 128 |
+
continue
|
| 129 |
+
|
| 130 |
+
wc = hourly["weather_code"][i]
|
| 131 |
+
at = hourly["apparent_temperature"][i]
|
| 132 |
+
temp = hourly["temperature_2m"][i]
|
| 133 |
+
pop = hourly["precipitation_probability"][i]
|
| 134 |
+
rh = hourly["relative_humidity_2m"][i]
|
| 135 |
+
|
| 136 |
+
wc_str = format_weather_code(wc)
|
| 137 |
+
|
| 138 |
+
line = (
|
| 139 |
+
f" {t_jst.strftime('%H時')}:"
|
| 140 |
+
f"{wc_str} "
|
| 141 |
+
f"{at}℃ {temp}℃ {pop}% {rh}%"
|
| 142 |
+
)
|
| 143 |
+
lines.append(line)
|
| 144 |
|
| 145 |
return {
|
| 146 |
"requestId": f"desk-web-weather-{int(time.time())}",
|
| 147 |
"blocks": [{"type": "text", "value": "\n".join(lines)}],
|
| 148 |
}
|
| 149 |
|
| 150 |
+
# ---------------- メインループ ----------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
|
|
|
|
|
|
|
| 153 |
def main_loop():
|
| 154 |
while True:
|
| 155 |
try:
|
| 156 |
now = datetime.now(JST)
|
| 157 |
|
| 158 |
+
# 毎正時の時報
|
| 159 |
if now.minute == 0:
|
| 160 |
body = create_time_signal_body()
|
| 161 |
post_group_message(TARGET_GROUP_CHAT_ID, body)
|
| 162 |
|
| 163 |
+
# 午後2時9分に天気予報
|
| 164 |
if now.hour == 14 and now.minute == 9:
|
| 165 |
body = create_weather_body()
|
| 166 |
post_group_message(TARGET_GROUP_CHAT_ID, body)
|
|
|
|
| 172 |
time.sleep(10)
|
| 173 |
|
| 174 |
|
|
|
|
| 175 |
if __name__ == "__main__":
|
| 176 |
main_loop()
|