-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetjson.py
277 lines (224 loc) · 8.31 KB
/
getjson.py
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import requests
import json
import discord
from discord.ext import tasks
from dotenv import load_dotenv
import os
import datetime
from time import sleep
from senseibot.logger import logger, set_log_level
set_log_level("DEBUG")
# from collections import defaultdict
load_dotenv()
DISCORD_TOKEN = os.environ["DISCORD_TOKEN"]
HEADERS = {"Authorization": f"Bot {DISCORD_TOKEN}"}
SHARE_URL = os.environ["SHARE_URL"]
# CANCELLED: DefaultDict[str, bool] = defaultdict(bool)
CHANNEL_ID = 918004115026616340
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
# start = calendar['start_time']
@client.event
async def on_ready():
print("We have logged in as {0.user}".format(client))
await client.change_presence(activity=discord.Game("Json"))
def get_user(name, discriminator):
user = discord.utils.get(client.users, name=name, discriminator=discriminator)
if user is None:
logger.debug(f"Can't find user: {name}#{discriminator}")
return None
return user
def get_json(year, month, day):
"""calendar情報を取得する
Returns:
その日のスケージュールを辞書のリストで返す。
list [{
"summary": str,
"description" : str,
"start_time": str,
"end_time": str,
"room" :str
} ... ]
"""
url = requests.get(
f"https://hanashiainokairegistration.herokuapp.com/tobot/{year}/{month}/{day}/"
)
text = url.text
logger.debug(text)
if text is None:
return []
else:
data = json.loads(text)
schedules = data[f"{year:04d}.{month:02d}.{day:02d}"]
return schedules
def get_time():
"""今の時間を(year, month, day, hour, minute)で返す"""
now = datetime.datetime.now()
year = now.year # 年 <- int
month = now.month # 月 <- int
day = now.day # 日 <- int
hour = now.hour # 時 <- int
minute = now.minute # 分 <- int
return (year, month, day, hour, minute)
def time_equal(time1, time2):
"""時間を比較するときに使う"""
assert len(time1) == len(time2)
assert len(time1) == 5
# time1 = (year, month, day, hour, minute)
# time2 = (year, month, day, hour, minute)
# Noneの場合は、指定なし
# TODO: time1, time2 をNamed Tupleとかにしたいね
ret = True
for t1, t2 in zip(time1, time2):
if t1 is None or t2 is None or t1 == t2:
continue
if t1 != t2:
ret = False
break
logger.debug(f"Comparing time: {time1} , {time2} : {ret}")
return ret
# # 負の数の時のパッチ
# if time1 < 0:
@tasks.loop(seconds=60)
async def check_loop():
await client.wait_until_ready()
year, month, day, hour, minute = get_time()
logger.debug("Starting schedule check")
schedules = get_json(year, month, day)
logger.debug(f"schedules found: {schedules}")
for schedule in schedules:
participants = schedule.get('participants', [])
user_mentions = []
for participant in participants:
name, discriminator = participant.split('#')
user = get_user(name, discriminator)
if user is not None:
user_mentions.append(user.mention)
## ここ
msg = (
' '.join(user_mentions)
+ "\n5分後に勉強会 【"
+ schedule["summary"]
+ "】 が始まります\n"
+ "内容 【"
+ schedule["description"]
+ "】\n開始時間は 【"
+ schedule["start_time"]
+ "】\n終了時間は 【"
+ schedule["end_time"]
+ "】\n勉強部屋は "
+ schedule["room"]
+ "のチャンネルです\n"
)
now_msg = (
' '.join(user_mentions)
+"\n今から勉強会 【"
+ schedule["summary"]
+ "】 が始まります\n"
+ "内容 【"
+ schedule["description"]
+ "】\n開始時間は 【"
+ schedule["start_time"]
+ "】\n終了時間は 【"
+ schedule["end_time"]
+ "】\n勉強部屋は "
+ schedule["room"]
+ "のチャンネルです\n"
)
# await send_text(msg)
# ssはschedule start(スケジュールの開始時間)の略
ss_year, ss_month, ss_day = year, month, day # <- 今日なのでnowと同じ
# <- これはschedulesのstart_timeを分割してintにしたもの
ss_hour, ss_minute = list(map(int, schedule["start_time"].split(":")))
# スケジュールの15分前かどうかをチェック
pre_15 = datetime.datetime(
ss_year, ss_month, ss_day, ss_hour, ss_minute
) - datetime.timedelta(minutes=5)
if time_equal(
(year, month, day, hour, minute),
(pre_15.year, pre_15.month, pre_15.day, pre_15.hour, pre_15.minute),
):
# 15分前なら送信する
await send_text(msg)
# スケジュールの開始時かどうかをチェック
if time_equal(
(year, month, day, hour, minute),
(ss_year, ss_month, ss_day, ss_hour, ss_minute),
):
await send_text(now_msg)
# @tasks.loop(seconds=60)
# async def loop():
# # botが起動するまで待つ
# await client.wait_until_ready()
# channel = client.get_channel(CHANNEL_ID)
# dt_now = datetime.datetime.now()
# year = dt_now.year
# month = str(dt_now.month).zfill(2)
# day = str(dt_now.day).zfill(2)
# url = requests.get(
# f"https://hanashiainokairegistration.herokuapp.com/tobot/{year}/{month}/{day}/")
# text = url.text
# data = json.loads(text)
# schedule = data[f"{year}.{month}.{day}"]
# for i in range(len(schedule)):
# schedule_dict = schedule[i]
# print(schedule_dict["summary"])
# await channel.send(f"""{month}/{day} {schedule_dict["start_time"]}~{schedule_dict["end_time"]} {schedule_dict["summary"]} 勉強部屋{schedule_dict["room"]}""")
async def send_text(msg):
logger.debug(msg)
channel = client.get_channel(CHANNEL_ID)
logger.debug(f"Sending to channel:{channel}")
# TODOメッセージ表示をわかりやすく
await channel.send(msg)
#
# ('\n'.join([
# '```',
# f'{msg}',
# '```',
# ]))
@tasks.loop(seconds=5)
async def morning_event():
# ---現在の時刻を取得---
now = get_time()
year, month, day, hour, minute = now
# ---8時じゃなかったら何もしない---
if time_equal((None, None, None, 8, 0), now):
return
# ---8時に実行する内容---
# 今日の勉強会のデータを取得
schedules = get_json(year, month, day)
# 今日の勉強会を送信する設定
msg = schedules # <- TODO: これを綺麗にしといて
send_text.start(msg, now)
# 今日の15分前と開始時の通知を設定する
for schedule in schedules:
# ssはSchedule Startの頭文字
ss_year, ss_month, ss_day = year, month, day # <- 今日なのでnowと同じ
# <- これはschedulesのstart_timeを分割してintにしたもの
ss_hour, ss_minute = list(map(int, schedule["start_time"].split(":")))
# 15分前に通知を送信する設定
msg = schedule # <-15分前に送信する内容
await send_text.start(
msg, (ss_year, ss_month, ss_day, ss_hour, ss_minute - 1)
) # <- 15分前に送信する
# 開始時に通知を送信する設定
msg = schedule # <-開始時に送信する内容
await send_text.start(
msg, (ss_year, ss_month, ss_day, ss_hour, ss_minute)
) # <- 開始時に送信する
# 15分ごとに登録状況をチェック
if __name__ == "__main__":
# Botの起動とDiscordサーバーへの接続
check_loop.start()
client.run(DISCORD_TOKEN)
# dt_now = datetime.datetime.now()
# year = dt_now.year
# month = str(dt_now.month).zfill(2)
# day = str(dt_now.day).zfill(2)
# # ループ処理実行
# while True:
# print(datetime.datetime.now().minute) # <- 今の時間を出力
# break
# sleep(1) # <- 1秒まつ
# TODO main.pyとの統合