-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess_movie_name_chatBot.py
227 lines (183 loc) · 8.03 KB
/
guess_movie_name_chatBot.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
# -*- coding: utf-8 -*-
import json
import urllib.request
import random
from slackclient import SlackClient
from flask import Flask, request, make_response
from datetime import datetime
app = Flask(__name__)
TASK_IDS = {}
slack_token =
slack_client_id =
slack_client_secret =
slack_verification =
sc = SlackClient(slack_token)
task_id = 'LB-2375'
attachments_json = [
{
"callback_id": "quizGame",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [
{
"name": "game",
"text": "정답 확인",
"style": "danger",
"type": "button",
"value": "war",
"confirm": {
"title": "정답은?",
"text": "quiz 또는 QUIZ를 입력해 주세요",
"ok_text": "맞음",
"dismiss_text": "틀림"
}
},
{
"name": "game",
"text": "개봉일 힌트 보기",
"type": "button",
"value": "war",
"confirm": {
"title": "Hint!",
"text": "quiz 또는 QUIZ를 입력해 주세요"
}
},
{
"name": "game",
"text": "첫글자 힌트 보기",
"type": "button",
"value": "war",
"confirm": {
"title": "Hint!",
"text": "quiz 또는 QUIZ를 입력해 주세요"
}
},
{
"name": "game",
"text": "감독 힌트 보기",
"type": "button",
"value": "war",
"confirm": {
"title": "Hint!",
"text": "quiz 또는 QUIZ를 입력해 주세요"
}
}
]
}
]
def get_director(mv_name):
base_url = "http://www.kobis.or.kr/kobisopenapi/webservice/rest/movie/searchMovieList.json"
api_key = "9e24d40e601d03984a93fc216b030a09"
query_url = '{}?key={}&movieNm={}'.format(base_url, api_key, urllib.parse.quote(mv_name)) # 박스오피스
with urllib.request.urlopen(query_url) as fin:
return json.loads(fin.read().decode('utf-8'))
def simplify_director(result, mv_code):
name = [entry.get("directors")[0].get("peopleNm") for entry in result.get('movieListResult').get('movieList') if entry.get("movieCd") == mv_code]
return name[0]
def isHangul(ch): # 주어진 문자가 한글인지 아닌지 리턴해주는 함수
JAMO_START_LETTER = 44032
JAMO_END_LETTER = 55203
return ord(ch) >= JAMO_START_LETTER and ord(ch) <= JAMO_END_LETTER
def cho(text):
CHOSUNG_START_LETTER = 4352
JAMO_START_LETTER = 44032
JAMO_END_LETTER = 55203
JAMO_CYCLE = 588
chosung = ""
for ch in text:
if isHangul(ch): # 한글인 경우에만 초성을 추출하고 그렇지 않은 경우엔 문자를 그대로 출력합니다.
chosung += chr(int((ord(ch) - JAMO_START_LETTER) / JAMO_CYCLE) + CHOSUNG_START_LETTER)
else:
chosung += ch
print("안녕하세요! 즐거운 초성 퀴즈 시간입니다. 어떤 영화의 초성인지 맞춰보세요!")
return chosung
def get_date():
start = datetime.strptime('11/11/2003 08:30 AM', '%m/%d/%Y %I:%M %p') # 최초DB 2003/11/11 부터~
end = datetime.now()
random_date = start + (end - start) * random.random()
target_dt_str = random_date.strftime('%Y%m%d')
return target_dt_str
def get_movies():
base_url = "http://www.kobis.or.kr/kobisopenapi/webservice/rest/boxoffice/searchDailyBoxOfficeList.json"
api_key = "9e24d40e601d03984a93fc216b030a09"
target_dt_str = get_date()
query_url = '{}?key={}&targetDt={}&itemPerPage={}'.format(base_url, api_key, target_dt_str, "8") # 박스오피스
with urllib.request.urlopen(query_url) as fin:
return json.loads(fin.read().decode('utf-8'))
def simplify(result):
temp_num = random.randrange(6, 9)
return [
result.get('boxOfficeResult').get('dailyBoxOfficeList')[temp_num].get('movieNm'),
result.get('boxOfficeResult').get('dailyBoxOfficeList')[temp_num].get('openDt'),
result.get('boxOfficeResult').get('dailyBoxOfficeList')[temp_num].get('movieCd')
]
def check_input(text):
keywords = []
text = text.lower()
if 'quiz' in text or '퀴즈' in text:
movies = get_movies() # movies 타입은 dict
movie_info = simplify(movies) # 영화제목과 개봉일, 타입은 list
# movie_info[0] = 영화제목
# movie_info[1] = 개봉일
# movie_info[2] = 영화 코드
print(movie_info)
keywords.append(movie_info[0])
keywords.append(cho(movie_info[0]))
keywords.append(movie_info[1])
keywords.append(movie_info[2])
# keywords 에는 영화제목, 영화제목 초성, 개봉일이 순서대로 들어감.
return u'\n'.join(keywords) # 한글 지원을 위해 앞에 unicode u를 붙혀준다.
else:
keywords.append("quiz <- 를 입력해 주세요 \n quiz <- 를 입력해 주세요 \n \n")
return u'\n'.join(keywords)
# 이벤트 핸들하는 함수
def _event_handler(event_type, slack_event):
print(slack_event["event"])
if event_type == "app_mention":
channel = slack_event["event"]["channel"]
text = slack_event["event"]["text"]
keywords = check_input(text)
movie_name, movie_chosung, movie_open_day, movie_code = keywords.split('\n')
print(type(movie_name))
if not 'quiz' in movie_name:
dir_info = get_director(movie_name)
dir_name = simplify_director(dir_info, movie_code)
print(type(movie_chosung))
print(movie_name[0])
attachments_json[0].get('actions')[0].get('confirm')['text'] = movie_name + "\t/\t감독 : " + dir_name + "\t/\t 개봉일 - " + movie_open_day
attachments_json[0]['text'] = "영화 제목 초성 : \t" + movie_chosung
attachments_json[0].get('actions')[1].get('confirm')['text'] = "\t 개봉일 - " + movie_open_day
attachments_json[0].get('actions')[2].get('confirm')['text'] = movie_name[0] + movie_chosung[1:]
attachments_json[0].get('actions')[3].get('confirm')['text'] = "\t 감독 - " + dir_name
sc.api_call(
"chat.postMessage",
channel=channel,
text="안녕하세요! 즐거운 초성 퀴즈 시간입니다. 어떤 영화의 초성인지 맞춰보세요!!",
attachments=attachments_json
)
return make_response("App mention message has been sent", 200, )
else:
sc.api_call(
"chat.postMessage",
channel=channel,
text="quiz <- 를 입력하세요",
)
return make_response("App mention message has been sent", 200, )
@app.route("/listening", methods=["GET", "POST"])
def hears():
slack_event = json.loads(request.data)
if "challenge" in slack_event:
return make_response(slack_event["challenge"], 200, {"content_type": "application/json"})
if slack_verification != slack_event.get("token"):
message = "Invalid Slack verification token: %s" % (slack_event["token"])
make_response(message, 403, {"X-Slack-No-Retry": 1})
if "event" in slack_event:
event_type = slack_event["event"]["type"]
return _event_handler(event_type, slack_event)
return make_response("[NO EVENT IN SLACK REQUEST] These are not the droids\
you're looking for.", 404, {"X-Slack-No-Retry": 1})
@app.route("/", methods=["GET"])
def index():
return "<h1>Server is ready!!!!!!.</h1>"
if __name__ == '__main__':
app.run(debug=True)