Spaces:
Running
Running
File size: 4,933 Bytes
fb481fa e3e1fb6 fb481fa 065e6e6 fb481fa e3e1fb6 fb481fa e3e1fb6 47fcc27 e3e1fb6 47fcc27 e4f6d97 fb481fa e3e1fb6 228ce2f e3e1fb6 228ce2f e3e1fb6 fb481fa e3e1fb6 e4f6d97 e3e1fb6 fb481fa e3e1fb6 fb481fa e3e1fb6 fb481fa e4f6d97 e3e1fb6 04a53c8 e4f6d97 e3e1fb6 e4f6d97 e3e1fb6 8c6fc68 2bcf4f9 fb481fa e3e1fb6 e4f6d97 e3e1fb6 e4f6d97 e3e1fb6 228ce2f e3e1fb6 e4f6d97 2f1b119 e3e1fb6 2f1b119 e3e1fb6 228ce2f e3e1fb6 228ce2f e3e1fb6 1511ea4 e3e1fb6 47fcc27 e3e1fb6 e4f6d97 e3e1fb6 fb481fa e3e1fb6 fd408ce e3e1fb6 fb481fa e3e1fb6 fb481fa e3e1fb6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | import time
import requests
import os
import json
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
BASE_URL = "https://desk-api.channel.io/desk/channels/200605"
TARGET_GROUP_CHAT_ID = "536194"
HEADERS = {
"accept-language": "ja",
"x-account": os.getenv("dmsendertoken")
}
JST = ZoneInfo("Asia/Tokyo")
# ---------------- 天気コード ----------------
WEATHER_CODE_MAP = {
0: "☀️",
1: "☁️",
2: "吹雪",
3: "砂嵐",
4: "🌫️",
5: "🌫️",
6: "🌧️",
7: "🌨️",
8: "🌦️",
9: "⛈️",
}
def format_weather_code(code: int) -> str:
if code < 10:
return WEATHER_CODE_MAP.get(code, str(code))
main = code // 10
sub = code % 10
main_str = WEATHER_CODE_MAP.get(main)
sub_str = WEATHER_CODE_MAP.get(sub)
if main_str and sub_str:
return f"{main_str}、{sub_str}"
return str(code)
# ---------------- Channel 投稿 ----------------
def post_group_message(chat_id, body):
url = f"{BASE_URL}/groups/{chat_id}/messages"
r = requests.post(url, headers=HEADERS, json=body)
r.raise_for_status()
return r.json()
# ---------------- 時報 ----------------
def create_time_signal_body():
now = datetime.now(JST)
hour = now.hour
if hour < 12:
period = "午前"
display_hour = hour if hour != 0 else 12
else:
period = "午後"
display_hour = hour - 12 if hour > 12 else 12
message = f"<b>時報</b>\n{period}{display_hour}時をお知らせします。"
return {
"requestId": f"desk-web-time-signal-{int(time.time())}",
"blocks": [{"type": "text", "value": message}],
}
# ---------------- 天気予報 ----------------
def load_todoufuken():
with open("todoufuken.json", encoding="utf-8") as f:
lines = []
for line in f:
stripped = line.lstrip()
if stripped.startswith("//"):
continue
lines.append(line)
json_str = "".join(lines)
return json.loads(json_str)
def fetch_weather(todoufuken):
lats = ",".join(str(p["lat"]) for p in todoufuken)
lons = ",".join(str(p["lon"]) for p in todoufuken)
url = (
"https://api.open-meteo.com/v1/forecast"
f"?latitude={lats}"
f"&longitude={lons}"
"&hourly=relative_humidity_2m,apparent_temperature,temperature_2m,"
"precipitation_probability,weather_code"
"&forecast_days=1"
"&timezone=Asia%2FTokyo"
)
r = requests.get(url)
r.raise_for_status()
return r.json()
def create_weather_body():
now = datetime.now(JST)
today_7am = now.replace(hour=7, minute=0, second=0, microsecond=0)
todoufuken = load_todoufuken()
data = fetch_weather(todoufuken)
lines = ["<b>☀️ 本日の天気予報(7時以降・1時間ごと)</b>"]
for idx, pref in enumerate(todoufuken):
hourly = data[idx]["hourly"]
times = hourly["time"]
lines.append(f"\n<b>{pref['name']}:</b>")
TARGET_HOURS = {7, 10, 13, 16, 21}
for i, t in enumerate(times):
t_jst = (
datetime.fromisoformat(t)
.replace(tzinfo=ZoneInfo("UTC"))
.astimezone(JST)
)
if t_jst.hour not in TARGET_HOURS:
continue
wc = hourly["weather_code"][i]
at = hourly["apparent_temperature"][i]
temp = hourly["temperature_2m"][i]
pop = hourly["precipitation_probability"][i]
rh = hourly["relative_humidity_2m"][i]
wc_str = format_weather_code(wc)
line = (
f" {t_jst.strftime('%H時')}:"
f"{wc_str} "
f"{at}℃ {temp}℃ {pop}% {rh}%"
)
lines.append(line)
message2 = "☀️・・・晴れ、☁️・・・曇り、🌫️・・・霧・霧雨、🌧️・・・雨、🌨️・・・雪、🌦️・・・にわか雨、⛈️・・・雷雨"
return {
"requestId": f"desk-web-weather-{int(time.time())}",
"blocks": [{"type": "text", "value": "\n".join(lines)},{"type": "text", "value": message2}],
}
# ---------------- メインループ ----------------
def main_loop():
while True:
try:
now = datetime.now(JST)
# 毎正時の時報
if now.minute == 0:
body = create_time_signal_body()
post_group_message(TARGET_GROUP_CHAT_ID, body)
# 午後7時20分に天気予報
if now.hour == 7 and now.minute == 20:
body = create_weather_body()
post_group_message(TARGET_GROUP_CHAT_ID, body)
time.sleep(60)
except Exception as e:
print("Error:", e)
time.sleep(10)
if __name__ == "__main__":
main_loop() |