izuemon commited on
Commit
e4f6d97
·
verified ·
1 Parent(s): 3fb1bc0

Update jihou.py

Browse files
Files changed (1) hide show
  1. jihou.py +112 -24
jihou.py CHANGED
@@ -1,20 +1,33 @@
1
  import time
2
  import requests
3
  import os
 
4
  from datetime import datetime, timedelta
5
  from zoneinfo import ZoneInfo
6
 
7
- BASE_URL = "https://desk-api.channel.io/desk/channels/200605"
8
- TARGET_GROUP_CHAT_ID = "463667"
9
 
10
  HEADERS = {
11
  "accept-language": "ja",
12
  "x-account": os.getenv("dmsendertoken")
13
  }
14
 
15
- # 日本時間(JST)
16
  JST = ZoneInfo("Asia/Tokyo")
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  def post_group_message(chat_id, body):
20
  url = f"{BASE_URL}/groups/{chat_id}/messages"
@@ -23,8 +36,10 @@ def post_group_message(chat_id, body):
23
  return r.json()
24
 
25
 
 
 
26
  def create_time_signal_body():
27
- now = datetime.now(JST) # 日本時間
28
  hour = now.hour
29
 
30
  if hour < 12:
@@ -38,36 +53,109 @@ def create_time_signal_body():
38
 
39
  return {
40
  "requestId": f"desk-web-time-signal-{int(time.time())}",
41
- "blocks": [
42
- {
43
- "type": "text",
44
- "value": message
45
- }
46
- ],
47
- "buttons": None,
48
- "form": None,
49
- "webPage": None,
50
- "files": None,
51
- "customPayload": None
52
  }
53
 
54
 
55
  def wait_until_next_hour():
56
- now = datetime.now(JST) # 日本時間
57
- next_hour = (
58
- now.replace(minute=0, second=0, microsecond=0)
59
- + timedelta(hours=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  )
61
- sleep_seconds = (next_hour - now).total_seconds()
62
- time.sleep(sleep_seconds)
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  def main_loop():
66
  while True:
67
  try:
68
- wait_until_next_hour()
69
- body = create_time_signal_body()
70
- post_group_message(TARGET_GROUP_CHAT_ID, body)
 
 
 
 
 
 
 
 
 
71
  except Exception as e:
72
  print("Error:", e)
73
  time.sleep(10)
 
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/227312"
9
+ TARGET_GROUP_CHAT_ID = "536135"
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"
 
36
  return r.json()
37
 
38
 
39
+ # ---------- 時報(既存機能) ----------
40
+
41
  def create_time_signal_body():
42
+ now = datetime.now(JST)
43
  hour = now.hour
44
 
45
  if hour < 12:
 
53
 
54
  return {
55
  "requestId": f"desk-web-time-signal-{int(time.time())}",
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:
70
+ return json.load(f)
71
+
72
+
73
+ def fetch_weather(todoufuken):
74
+ lats = ",".join(str(p["lat"]) for p in todoufuken)
75
+ lons = ",".join(str(p["lon"]) for p in todoufuken)
76
+
77
+ url = (
78
+ "https://api.open-meteo.com/v1/forecast"
79
+ f"?latitude={lats}"
80
+ f"&longitude={lons}"
81
+ "&hourly=relative_humidity_2m,apparent_temperature,temperature_2m,"
82
+ "precipitation_probability,weather_code"
83
+ "&forecast_days=1"
84
  )
 
 
85
 
86
+ r = requests.get(url)
87
+ r.raise_for_status()
88
+ return r.json()
89
+
90
+
91
+ def create_weather_body():
92
+ now = datetime.now(JST)
93
+ today_6am = now.replace(hour=6, minute=0, second=0, microsecond=0)
94
+
95
+ todoufuken = load_todoufuken()
96
+ data = fetch_weather(todoufuken)
97
+
98
+ lines = ["<b>☀️ 本日の天気予報(6時以降)</b>"]
99
+
100
+ for idx, pref in enumerate(todoufuken):
101
+ hourly = data["hourly"]
102
+
103
+ times = hourly["time"]
104
+ target_index = None
105
+
106
+ for i, t in enumerate(times):
107
+ t_jst = datetime.fromisoformat(t).replace(tzinfo=ZoneInfo("UTC")).astimezone(JST)
108
+ if t_jst >= today_6am:
109
+ target_index = i
110
+ break
111
+
112
+ if target_index is None:
113
+ continue
114
+
115
+ wc = hourly["weather_code"][target_index]
116
+ at = hourly["apparent_temperature"][target_index]
117
+ temp = hourly["temperature_2m"][target_index]
118
+ pop = hourly["precipitation_probability"][target_index]
119
+ rh = hourly["relative_humidity_2m"][target_index]
120
+
121
+ line = (
122
+ f"{pref['name']} "
123
+ f"{WEATHER_CODE_MAP.get(wc, wc)} "
124
+ f"{at}℃ {temp}℃ {pop}% {rh}%"
125
+ )
126
+ lines.append(line)
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
+ if now.minute == 0:
150
+ body = create_time_signal_body()
151
+ post_group_message(TARGET_GROUP_CHAT_ID, body)
152
+
153
+ if now.hour == 7 and now.minute == 0:
154
+ body = create_weather_body()
155
+ post_group_message(TARGET_GROUP_CHAT_ID, body)
156
+
157
+ time.sleep(60)
158
+
159
  except Exception as e:
160
  print("Error:", e)
161
  time.sleep(10)