-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
371 lines (334 loc) · 12 KB
/
utils.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import os
from linebot import LineBotApi, WebhookParser
from linebot.models import MessageEvent, TextMessage, TextSendMessage, FlexSendMessage
import pandas as pd
import numpy as np
from random import randint
from message_template import *
import psycopg2
import os
import csv
import smtplib
import mimetypes
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
channel_access_token = os.getenv("LINE_CHANNEL_ACCESS_TOKEN", None)
def send_text_message(reply_token, text):
line_bot_api = LineBotApi(channel_access_token)
line_bot_api.reply_message(reply_token, TextSendMessage(text=text))
return "OK"
def send_flex_message(reply_token, alias, flex):
line_bot_api = LineBotApi(channel_access_token)
line_bot_api.reply_message(reply_token, FlexSendMessage(alias, flex))
"""
def send_image_url(id, img_url):
pass
def send_button_message(id, text, buttons):
pass
"""
local = False
global DATABASE_URL
def createTable():
global DATABASE_URL
if local:
DATABASE_URL = os.popen('heroku config:get DATABASE_URL -a nihongolearning').read()[:-1]
else:
DATABASE_URL = os.environ.get('DATABASE_URL')
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
cursor = conn.cursor()
createdb = '''CREATE TABLE IF NOT EXISTS favorite(
userid TEXT NOT NULL,
word_level NUMERIC NOT NULL,
word_index NUMERIC NOT NULL
);'''
cursor.execute(createdb)
conn.commit()
cursor.close()
conn.close()
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
cursor = conn.cursor()
createdb = '''CREATE TABLE IF NOT EXISTS user_state(
userid TEXT NOT NULL,
state TEXT NOT NULL
);'''
cursor.execute(createdb)
conn.commit()
cursor.close()
conn.close()
def read_state(userid):
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
cursor = conn.cursor()
query = '''SELECT * FROM user_state WHERE userid=%s '''
cursor.execute(query, (userid,))
result = cursor.fetchall()
if len(result) == 0:
query = '''INSERT INTO user_state (userid,state) VALUES (%s,%s);'''
cursor.execute(query, (userid, 'user'))
conn.commit()
cursor.close()
conn.close()
return 'user'
else:
cursor.close()
conn.close()
return result[0][1]
def update_state(userid, state):
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
cursor = conn.cursor()
query = '''UPDATE user_state SET state = %s WHERE userid = %s'''
cursor.execute(query, (state, userid))
conn.commit()
cursor.close()
conn.close()
def make_vocabulary_flex_nodb(input_level, next_state):
if local:
df = pd.read_csv(os.getcwd() + '/data/' + 'N' + input_level + '.csv', header=None, dtype=str)
else:
df = pd.read_csv('data/' + 'N' + input_level + '.csv', header=None, dtype=str)
sample = df.sample(5)
index = sample.index
index = np.array(index)
sample = np.array(sample)
level = []
japan = []
spell = []
tune = []
chinese = []
for voc in sample:
level.append('N' + str(input_level))
japan.append(voc[0])
spell.append(voc[1])
tune.append(voc[2])
chinese.append(voc[3])
flex = contentInput(level, japan, spell, tune, chinese, index, next_state)
return flex
def add_vocabulary_to_db(event, source):
user_id = event.source.user_id
if source == "vocabulary":
index_level = event.postback.data[1:]
else:
index_level = event.postback.data[2:]
index_level = index_level.split('N')
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
cursor = conn.cursor()
query = '''SELECT * FROM favorite WHERE userid=%s AND word_index=%s AND word_level=%s '''
cursor.execute(query, (user_id, int(index_level[0]), int(index_level[1])))
result = cursor.fetchall()
if len(result) == 0:
query = '''INSERT INTO favorite (userid,word_index,word_level) VALUES (%s,%s,%s);'''
cursor.execute(query, (user_id, int(index_level[0]), int(index_level[1])))
conn.commit()
cursor.close()
conn.close()
return True
else:
cursor.close()
conn.close()
return False
def make_vocabulary_flex_db(event, next_state):
global DATABASE_URL
userid = event.source.user_id
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
cursor = conn.cursor()
query = '''SELECT word_level,word_index FROM favorite WHERE userid=%s ORDER BY RANDOM() LIMIT 5 '''
cursor.execute(query, (userid,))
result = cursor.fetchall()
level = []
japan = []
spell = []
tune = []
chinese = []
index = []
for i, word in enumerate(result):
lev = str(word[0])
index.append(int(word[1]))
if local:
df = pd.read_csv(os.getcwd() + '/data/' + 'N' + lev + '.csv', header=None, dtype=str)
else:
df = pd.read_csv('data/' + 'N' + lev + '.csv', header=None, dtype=str)
#print(df.iloc[index[i]].tolist())
level.append('N' + lev)
japan.append(df.iloc[index[i]].tolist()[0])
spell.append(df.iloc[index[i]].tolist()[1])
tune.append(df.iloc[index[i]].tolist()[2])
chinese.append(df.iloc[index[i]].tolist()[3])
cursor.close()
conn.close()
if len(result) > 0:
flex = contentInput(level, japan, spell, tune, chinese, index, next_state,
num=5 if len(result) > 5 else len(result), delete=True)
else:
flex = no_favorite()
return flex
def delete_vocabulary_to_db(event):
user_id = event.source.user_id
index_level = event.postback.data[1:]
index_level = index_level.split('N')
#print(index_level)
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
cursor = conn.cursor()
query = '''SELECT * FROM favorite WHERE userid=%s AND word_index=%s AND word_level=%s '''
cursor.execute(query, (user_id, int(index_level[0]), int(index_level[1])))
result = cursor.fetchall()
if len(result) == 0:
return False
cursor.close()
conn.close()
else:
query = '''DELETE FROM favorite WHERE userid=%s AND word_index=%s AND word_level=%s '''
cursor.execute(query, (user_id, int(index_level[0]), int(index_level[1])))
conn.commit()
cursor.close()
conn.close()
return True
# def export_favorite(event):
# table=[['級數','日文','假名','重音','中文']]
# id = event.source.user_id
# conn = psycopg2.connect(DATABASE_URL, sslmode='require')
# cursor = conn.cursor()
# query = '''SELECT word_level,word_index FROM favorite WHERE userid=%s '''
# cursor.execute(query, (id,))
# result = cursor.fetchall()
# index = []
# for i,word in enumerate(result):
# lev = 'N'+str(word[0])
# if local:
# df = pd.read_csv(os.getcwd() + '/data/' + lev + '.csv',header=None, dtype=str)
# else:
# df = pd.read_csv('data/' + lev + '.csv', header=None, dtype=str)
# temp = df.iloc[int(word[1])].tolist()
# temp.insert(0,lev)
# table.append(temp)
#
#
# # --- Email 的收件人與寄件人address ---
# emailfrom = account
# emailto = event.message.text
# # # --- Email 附件檔案 Attachment -----------
# if local:
# with open(os.getcwd() +event.source.user_id+'.csv','w',newline='',encoding="utf-8") as csvfile:
# writer = csv.writer(csvfile)
# writer.writerows(table)
# fileToSend = os.getcwd() +event.source.user_id+'.csv'
# else:
# with open('data/'+event.source.user_id+'.csv', 'w', newline='') as csvfile:
# writer = csv.writer(csvfile)
# writer.writerows(table)
# fileToSend = 'data/'+event.source.user_id+'.csv'
#
#
# msg = MIMEMultipart()
# msg["From"] = emailfrom
# msg["To"] = emailto
# # --- Email 的主旨 Subject ---
# msg["Subject"] = "收藏單字"
# msg["preamble"] = 'You will not see this in a MIME-aware mail reader.\n'
#
# # ----- Email 的信件內容 Message -----
# part = MIMEText("以下為您的收藏單字", _charset="UTF-8")
#
# msg.attach(part)
# # ----- Test for Text Message -----
#
# ctype, encoding = mimetypes.guess_type(fileToSend)
# if ctype is None or encoding is not None:
# ctype = "application/octet-stream"
# maintype, subtype = ctype.split("/", 1)
# print(maintype,subtype)
#
# fp = open(fileToSend, "rb")
# attachment = MIMEBase(maintype, subtype)
# attachment.set_payload(fp.read())
# fp.close()
# encoders.encode_base64(attachment)
# attachment.add_header("Content-Disposition", "attachment", filename=fileToSend)
# msg.attach(attachment)
#
# # --- 如果是 Gmail 可使用這行 ---
# server = smtplib.SMTP('smtp.gmail.com', 587)
# server.ehlo()
# server.starttls()
# # --- 如果SMTP server 不需要登入則可把 server.login 用 # mark 掉
# server.login(username, password)
# server.sendmail(emailfrom, emailto, msg.as_string())
# server.quit()
def generate_question(event):
# print(event)
parsed_input = event.postback.data.split('N')
if 'next' in parsed_input[0]:
parsed_input[0] = parsed_input[0][4:]
if local:
df = pd.read_csv(os.getcwd() + '/data/' + 'N' + parsed_input[1] + '.csv', header=None, dtype=str)
else:
df = pd.read_csv('data/' + 'N' + parsed_input[1] + '.csv', header=None, dtype=str)
while True:
sample = df.sample(2)
index = sample.index
index = np.array(index)
sample = np.array(sample)
level = []
japan = []
spell = []
tune = []
chinese = []
for voc in sample:
level.append('N' + str(parsed_input[1]))
japan.append(voc[0])
spell.append(voc[1])
tune.append(voc[2])
chinese.append(voc[3])
if index[0] == index[1]:
continue
if '------' in spell[0] or '------' in spell[1]:
if parsed_input[0] == "spell":
continue
if '------' in spell[0] or '------' in spell[1]:
if parsed_input[0] == "chinese":
if '------' in spell[0]:
spell[0] = japan[0]
if '------' in spell[1]:
spell[1] = japan[1]
break
break
rand_num = randint(0, 1)
if parsed_input[0] == "spell":
if rand_num == 0:
spell_option = [spell[0], spell[1]]
answer = ['r', 'w']
else:
spell_option = [spell[1], spell[0]]
answer = ['w', 'r']
#print(spell_option)
flex = test_spell_option(level[rand_num], japan[rand_num], spell_option, tune[rand_num], chinese[rand_num],
index[rand_num], answer)
else:
if rand_num == 0:
chinese_option = [chinese[0], chinese[1]]
answer = ['r', 'w']
else:
chinese_option = [chinese[1], chinese[0]]
answer = ['w', 'r']
flex = test_chinese_option(level[rand_num], japan[rand_num], spell[rand_num], tune[rand_num], chinese_option,
index[rand_num], answer)
return flex
def generate_answer(event):
question_type = event.postback.data[0]
index_level = event.postback.data[10:].split('N')
#print(index_level)
lev = str(index_level[1])
index = int(index_level[0])
if local:
df = pd.read_csv(os.getcwd() + '/data/' + 'N' + lev + '.csv', header=None, dtype=str)
else:
df = pd.read_csv('data/' + 'N' + lev + '.csv', header=None, dtype=str)
#print(df.iloc[index].tolist())
level = 'N' + lev
spell = df.iloc[index].tolist()[1]
chinese = df.iloc[index].tolist()[3]
if question_type == 's':
flex = test_spell_answer_result(level, index, spell)
else:
flex = test_chinese_answer_result(level, index, chinese)
return flex