Spaces:
Running
Running
Delete weather.py
Browse files- weather.py +0 -117
weather.py
DELETED
|
@@ -1,117 +0,0 @@
|
|
| 1 |
-
import time
|
| 2 |
-
import requests
|
| 3 |
-
import os
|
| 4 |
-
import json
|
| 5 |
-
from datetime import datetime, timedelta
|
| 6 |
-
from zoneinfo import ZoneInfo
|
| 7 |
-
|
| 8 |
-
BASE_URL = "https://desk-api.channel.io/desk/channels/200605"
|
| 9 |
-
TARGET_GROUP_CHAT_ID = "463667"
|
| 10 |
-
|
| 11 |
-
HEADERS = {
|
| 12 |
-
"accept-language": "ja",
|
| 13 |
-
"x-account": os.getenv("dmsendertoken")
|
| 14 |
-
}
|
| 15 |
-
|
| 16 |
-
JST = ZoneInfo("Asia/Tokyo")
|
| 17 |
-
|
| 18 |
-
WEATHER_CODE_MAP = {
|
| 19 |
-
0: "☀️晴れ",
|
| 20 |
-
1: "☁️曇り",
|
| 21 |
-
2: "吹雪",
|
| 22 |
-
3: "砂嵐",
|
| 23 |
-
4: "🌫️霧",
|
| 24 |
-
5: "🌫️霧雨",
|
| 25 |
-
6: "🌧️雨",
|
| 26 |
-
7: "🌨️雪",
|
| 27 |
-
8: "🌦️シャワー",
|
| 28 |
-
9: "⛈️雷雨",
|
| 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 |
-
def wait_until_7am():
|
| 40 |
-
now = datetime.now(JST)
|
| 41 |
-
target = now.replace(hour=7, minute=0, second=0, microsecond=0)
|
| 42 |
-
if now >= target:
|
| 43 |
-
target += timedelta(days=1)
|
| 44 |
-
time.sleep((target - now).total_seconds())
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def fetch_weather(lat, lon):
|
| 48 |
-
url = (
|
| 49 |
-
"https://api.open-meteo.com/v1/forecast"
|
| 50 |
-
f"?latitude={lat}&longitude={lon}"
|
| 51 |
-
"&hourly=relative_humidity_2m,apparent_temperature,"
|
| 52 |
-
"temperature_2m,precipitation_probability,weather_code"
|
| 53 |
-
"&forecast_days=1"
|
| 54 |
-
"&timezone=Asia%2FTokyo"
|
| 55 |
-
)
|
| 56 |
-
r = requests.get(url)
|
| 57 |
-
r.raise_for_status()
|
| 58 |
-
return r.json()
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
def extract_after_6am(hourly):
|
| 62 |
-
for i, t in enumerate(hourly["time"]):
|
| 63 |
-
hour = int(t[11:13])
|
| 64 |
-
if hour >= 6:
|
| 65 |
-
return {
|
| 66 |
-
"weather": WEATHER_CODE_MAP.get(hourly["weather_code"][i], "不明"),
|
| 67 |
-
"apparent": hourly["apparent_temperature"][i],
|
| 68 |
-
"temp": hourly["temperature_2m"][i],
|
| 69 |
-
"rain": hourly["precipitation_probability"][i],
|
| 70 |
-
"humidity": hourly["relative_humidity_2m"][i],
|
| 71 |
-
}
|
| 72 |
-
return None
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
def create_weather_message():
|
| 76 |
-
with open("todoufuken.json", encoding="utf-8") as f:
|
| 77 |
-
prefectures = json.load(f)
|
| 78 |
-
|
| 79 |
-
lines = ["<b>☀️ 本日の天気予報(6時以降)</b>"]
|
| 80 |
-
|
| 81 |
-
for p in prefectures:
|
| 82 |
-
data = fetch_weather(p["lat"], p["lon"])
|
| 83 |
-
info = extract_after_6am(data["hourly"])
|
| 84 |
-
if not info:
|
| 85 |
-
continue
|
| 86 |
-
|
| 87 |
-
line = (
|
| 88 |
-
f'{p["name"]} {info["weather"]} '
|
| 89 |
-
f'{info["apparent"]}℃ {info["temp"]}℃ '
|
| 90 |
-
f'{info["rain"]}% {info["humidity"]}%'
|
| 91 |
-
)
|
| 92 |
-
lines.append(line)
|
| 93 |
-
|
| 94 |
-
return {
|
| 95 |
-
"requestId": f"desk-weather-{int(time.time())}",
|
| 96 |
-
"blocks": [
|
| 97 |
-
{
|
| 98 |
-
"type": "text",
|
| 99 |
-
"value": "\n".join(lines),
|
| 100 |
-
}
|
| 101 |
-
],
|
| 102 |
-
}
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
def main_loop():
|
| 106 |
-
while True:
|
| 107 |
-
try:
|
| 108 |
-
wait_until_7am()
|
| 109 |
-
body = create_weather_message()
|
| 110 |
-
post_group_message(TARGET_GROUP_CHAT_ID, body)
|
| 111 |
-
except Exception as e:
|
| 112 |
-
print("Error:", e)
|
| 113 |
-
time.sleep(30)
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
if __name__ == "__main__":
|
| 117 |
-
main_loop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|