/
app.py
131 lines (110 loc) · 3.99 KB
/
app.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
from flask import Flask, request, abort
import os
import re
import copy
import json
import pickle
import pandas as pd
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage, StickerMessage, StickerSendMessage,
TemplateSendMessage, CarouselTemplate, CarouselColumn, URIAction
)
app = Flask(__name__)
#環境変数取得
CHANNEL_ACCESS_TOKEN = os.environ["CHANNEL_ACCESS_TOKEN"]
CHANNEL_SECRET = os.environ["CHANNEL_SECRET"]
line_bot_api = LineBotApi(CHANNEL_ACCESS_TOKEN)
handler = WebhookHandler(CHANNEL_SECRET)
usecols = ['Name', 'Japanese name', 'Score']
anime = pd.read_csv('data/anime.csv', usecols=usecols)
anime['Score'] = anime['Score'].replace('Unknown', 0).astype(float)
anime_image = pd.read_csv('data/anime_image.csv')
pkl_file = open('data/anime_indices.pkl', 'rb')
indices = pickle.load(pkl_file)
@app.route("/")
def hello_world():
return "おすすめアニメ"
@app.route("/callback", methods=['POST'])
def callback():
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=StickerMessage)
def handle_sticker_message(event):
line_bot_api.reply_message(
event.reply_token,
StickerSendMessage(package_id=event.message.package_id, sticker_id=event.message.sticker_id)
)
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
text = event.message.text
result = re.match('TOP(\d+)', text, re.IGNORECASE)
if result:
n = int(result.group(1))
if n > 100:
n = 100
top_n = anime.sort_values('Score', ascending=False).head(n)
titles = ''
for row in enumerate(top_n['Japanese name']):
titles += f"{row[1]}, "
line_bot_api.reply_message(
event.reply_token, [
TextSendMessage(text=f"アニメTOP{n}"),
TextSendMessage(text=titles)
]
)
else:
query1 = anime['Japanese name'].str.contains(text, case=False)
query2 = anime['Name'].str.contains(text, case=False)
anime_index = anime[query1 | query2].head(1).index
if anime_index.empty:
line_bot_api.reply_message(
event.reply_token, [
TextSendMessage(text='見当たらない'),
StickerSendMessage(package_id='6136', sticker_id='10551393')
]
)
else:
titles = ''
contents = []
for i, number in enumerate(indices[anime_index][0][:6]):
if i == 0:
target = f"「{anime.at[number, 'Japanese name']}」をご覧になったあなたへ"
continue
title = anime.at[number, 'Japanese name']
id = anime_image.at[number, 'MAL_ID']
contents.append(CarouselColumn(
thumbnail_image_url=anime_image.at[number, 'IMAGE_URL'],
text=title,
actions=[
URIAction(
label='detail',
uri=f'https://myanimelist.net/anime/{id}'
)
]
))
titles += f"{title}, "
line_bot_api.reply_message(
event.reply_token, [
TextSendMessage(text=target),
TemplateSendMessage(alt_text=titles, template=CarouselTemplate(columns=contents, image_size='contain'))
]
)
if __name__ == "__main__":
# app.run()
port = int(os.getenv("PORT"))
app.run(host="0.0.0.0", port=port)