This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessing_input.py
251 lines (236 loc) · 11.7 KB
/
processing_input.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
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
import re
import line_api
from string_changer import *
from time_table import *
import os, sys, traceback
def processing_input(events):
line_logger = logging.Logger('line_event')
try:
f = logging.FileHandler('./bot_log/line-'+datetime.datetime.today().strftime('%Y-%m')+'.log')
f.setFormatter(logging.Formatter('%(asctime)s: %(message)s'))
line_logger.addHandler(f)
except FileNotFoundError:
os.mkdir('bot_log')
f = logging.FileHandler('./bot_log/line-' + datetime.datetime.today().strftime('%Y-%m') + '.log')
f.setFormatter(logging.Formatter('%(asctime)s: %(message)s'))
line_logger.addHandler(f)
command_logger = logging.Logger('command')
f = logging.FileHandler('./bot_log/command-'+datetime.datetime.today().strftime('%Y-%m')+'.log')
f.setFormatter(logging.Formatter('%(asctime)s: %(message)s'))
command_logger.addHandler(f)
for event in events:
type_ = event['type']
command_data = list()
source_type = event['source']['type']
command_data.append(source_type)
user_id = event['source'].get('userId', '-')
command_data.append(user_id)
group_id = '-'
if source_type != 'user':
group_id = event['source'].get(source_type+'Id', '-')
command_data.append(group_id)
if type_ != 'message':
line_logger.log(30, ' '.join(command_data))
return
message_type = event['message']['type']
message_id = event['message']['id']
command_data.append(message_type)
command_data.append(message_id)
if message_type == 'text':
command_data.append(event['message']['text'])
elif message_type == 'file':
command_data.append(event['message']['fileName'])
# command_data.append(event['message']['fileSize'])
elif message_type == 'location':
command_data.append(event['message']['title'])
command_data.append(event['message']['address'])
command_data.append(event['message']['latitude'])
command_data.append(event['message']['longitude'])
elif message_type == 'sticker':
command_data.append(event['message']['packageId'])
command_data.append(event['message']['stickerId'])
command_data = map(str, command_data)
line_logger.log(30, ' '.join(command_data))
if message_type != 'text':
return
reply_token = event['replyToken']
text = event['message']['text']
words = re.split('[ \n]', text)
command_pattern = {'登録': '登録', 'register': '登録',
'削除': '削除', 'delete': '削除',
'更新': '更新', 'update': '更新',
'show': '表示'
}
date_pattern = ['\d{4}/\d{1,2}/\d{1,2}','今日', '昨日', '明日', '明後日', '明々後日', '来週']
tag_pattern = {'時間割': '時間割',
'time_table': '時間割',
'課題': '課題',
'task': '課題',
'イベント': 'イベント',
'event': 'イベント',
'課題リスト': '課題リスト',
'task_list': '課題リスト',
'イベントリスト': 'イベントリスト',
'event_list': 'イベントリスト',
'時間割変更': '時間割変更',
'time_table_change': '時間割変更',
'起動停止ランダム': '起動停止ランダム',
'卍': '卍',
'∮':'∮',
'ふとし':'ふとし'
}
flag = '表示'
for p in command_pattern.keys():
if p in words:
flag = command_pattern[p]
words.remove(p)
break
date = '今日'
date_flag = False
for d in date_pattern:
for e in words:
if re.match(d, e):
date = e
words.remove(e)
date_flag = True
break
else:
continue
break
tag = '-'
for p in tag_pattern:
if p in words:
tag = tag_pattern[p]
words.remove(p)
break
id_option = False
if 'id' in words:
id_option = True
words.remove('id')
if tag == '-':
if date_flag:
table = time_table_string(get_time_table(get_date(date)))
task = task_string(get_task(get_date(date)))
event = event_string(get_event(get_date(date)))
command_logger.log(30, '{} {} {} {} {} success'.format(user_id, group_id, flag, tag, date))
line_api.reply_message(reply_token, [table, task, event])
return
try:
if flag == '表示':
if tag == '時間割':
line_api.reply_message(reply_token, time_table_string(get_time_table(date)))
elif tag == '課題':
line_api.reply_message(reply_token, task_string(get_task(date)))
elif tag == 'イベント':
line_api.reply_message(reply_token, event_string(get_event(date)))
elif tag == '課題リスト':
if id_option:
line_api.reply_message(reply_token, task_list_string_id(get_task_list(date)))
else:
line_api.reply_message(reply_token, task_list_string(get_task_list(date)))
elif tag == 'イベントリスト':
if id_option:
line_api.reply_message(reply_token, event_list_string_id(get_event_list(date)))
else:
line_api.reply_message(reply_token, event_list_string(get_event_list(date)))
elif tag == '時間割変更':
if id_option:
line_api.reply_message(reply_token, time_table_change_list_string_id(get_time_table_change_list(date)))
else:
line_api.reply_message(reply_token, time_table_change_list_string(get_time_table_change_list(date)))
elif tag == '起動停止ランダム':
line_api.reply_message(reply_token, '機動停止ガンダム')
elif tag == '卍':
line_api.reply_message(reply_token, 'ふとし')
elif tag == '∮':
line_api.reply_message(reply_token, '卍')
elif tag == 'ふとし':
line_api.reply_message(reply_token, 'Shift_JIS')
else:
raise Exception
command_logger.log(30, '{} {} {} {} {} success'.format(user_id, group_id, flag, tag, date))
elif flag == '登録':
is_success = False
value = list()
if tag == '時間割' or tag == '時間割変更':
is_success = add_time_table_change(date, words[0], words[1])
value.extend(words[:2])
elif tag == '課題':
is_success = add_task(date, words[0], words[1])
value.extend(words[:2])
elif tag == 'イベント':
is_success = add_event(date, words[0])
value.extend(words[:1])
if is_success:
line_api.reply_message(reply_token, 'success')
command_logger.log(30, '{} {} {} {} {} {} success'.format(user_id, group_id, flag, tag, date,
' '.join(value)))
else:
line_api.reply_message(reply_token, 'failure')
command_logger.log(30, '{} {} {} {} {} {} failure'.format(user_id, group_id, flag, tag, date,
' '.join(value)))
elif flag == '削除':
is_success = False
value = list()
if tag == '時間割' or tag == '時間割変更':
if id_option:
is_success = delete_time_table_change_id(words[0])
value.extend(words[0:1])
else:
is_success = delete_time_table_change(date, words[0], words[1])
value.append(date)
value.extend(words[0:2])
elif tag == '課題':
if id_option:
is_success = delete_task_id(words[0])
value.extend(words[0:1])
else:
is_success = delete_task(date, words[0], words[1])
value.append(date)
value.extend(words[0:2])
elif tag == 'イベント':
if id_option:
is_success = delete_event_id(words[0])
value.extend(words[0:1])
else:
is_success = delete_event(date, words[0])
value.append(date)
value.extend(words[0:2])
if is_success:
line_api.reply_message(reply_token, 'success')
command_logger.log(30, '{} {} {} {} {} success'.format(user_id, group_id, flag, tag,
' '.join(value)))
else:
line_api.reply_message(reply_token, 'failure')
command_logger.log(30, '{} {} {} {} {} failure'.format(user_id, group_id, flag, tag,
' '.join(value)))
elif flag == '更新':
is_success = False
value = list()
if tag == '時間割' or tag == '時間割変更':
is_success = update_time_table_change(words[0], date, words[1], words[2])
value.extend(words[1:3])
elif tag == '課題':
is_success = update_task(words[0], date, words[1], words[2])
value.extend(words[1:3])
elif tag == 'イベント':
is_success = update_event(words[0], date, words[1])
value.extend(words[1:2])
if is_success:
line_api.reply_message(reply_token, 'success')
command_logger.log(30, '{} {} {} {} {} {} success'.format(user_id, group_id, flag, tag, date,
' '.join(value)))
else:
line_api.reply_message(reply_token, 'failure')
command_logger.log(30, '{} {} {} {} {} {} failure'.format(user_id, group_id, flag, tag, date,
' '.join(value)))
except (KeyError, ValueError) as err:
line_api.reply_message(reply_token, ['Fuck you!'])
error_logger = logging.Logger('error')
f = logging.FileHandler('./bot_log/error.log')
f.setFormatter(logging.Formatter('%(asctime)s: %(message)s'))
error_logger.addHandler(f)
error_logger.log(30, 'Error dosomething: %s', err)
error_logger.exception('Error dosomething: %s', err)