From d464a7a3e8f0303a9b5774f07a96d99e3bbb45d0 Mon Sep 17 00:00:00 2001 From: Simo-C3 Date: Mon, 14 Mar 2022 13:54:17 +0900 Subject: [PATCH] =?UTF-8?q?[add]=20=E6=8A=95=E7=A8=BF=E3=81=95=E3=82=8C?= =?UTF-8?q?=E3=81=9F=E7=94=BB=E5=83=8F=E3=81=8B=E3=82=89=E3=83=90=E3=83=BC?= =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92=E8=AA=AD=E3=81=BF=E8=BE=BC?= =?UTF-8?q?=E3=81=BF=E3=80=81=E6=9B=B8=E7=B1=8D=E6=83=85=E5=A0=B1=E3=82=92?= =?UTF-8?q?=E5=8F=96=E5=BE=97=E3=81=99=E3=82=8B=E6=A9=9F=E8=83=BD=E3=82=92?= =?UTF-8?q?=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + Pipfile | 2 ++ Pipfile.lock | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- bot.py | 33 +++++++++++++++++++++++++++------ cruds/bot.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 65ae49c..2655a98 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vscode/ .env *.pyc +static/images/* \ No newline at end of file diff --git a/Pipfile b/Pipfile index 13a71d9..64cafca 100644 --- a/Pipfile +++ b/Pipfile @@ -20,6 +20,8 @@ pyjwt = "*" cerberus = "*" line-bot-sdk = "*" flask = "*" +pyzbar = "*" +pillow = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index 385c657..41fefba 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "43d06f56959a57a201e691d96901f9e5f4928de05c68a6b469b62dd4200c1d4a" + "sha256": "66036465c645ba44b441f1f5419bf1796063c319b804996c46cc9b1f1d6e46f3" }, "pipfile-spec": 6, "requires": { @@ -596,6 +596,47 @@ "markers": "python_version >= '3.6'", "version": "==21.3" }, + "pillow": { + "hashes": [ + "sha256:011233e0c42a4a7836498e98c1acf5e744c96a67dd5032a6f666cc1fb97eab97", + "sha256:0f29d831e2151e0b7b39981756d201f7108d3d215896212ffe2e992d06bfe049", + "sha256:12875d118f21cf35604176872447cdb57b07126750a33748bac15e77f90f1f9c", + "sha256:14d4b1341ac07ae07eb2cc682f459bec932a380c3b122f5540432d8977e64eae", + "sha256:1c3c33ac69cf059bbb9d1a71eeaba76781b450bc307e2291f8a4764d779a6b28", + "sha256:1d19397351f73a88904ad1aee421e800fe4bbcd1aeee6435fb62d0a05ccd1030", + "sha256:253e8a302a96df6927310a9d44e6103055e8fb96a6822f8b7f514bb7ef77de56", + "sha256:2632d0f846b7c7600edf53c48f8f9f1e13e62f66a6dbc15191029d950bfed976", + "sha256:335ace1a22325395c4ea88e00ba3dc89ca029bd66bd5a3c382d53e44f0ccd77e", + "sha256:413ce0bbf9fc6278b2d63309dfeefe452835e1c78398efb431bab0672fe9274e", + "sha256:5100b45a4638e3c00e4d2320d3193bdabb2d75e79793af7c3eb139e4f569f16f", + "sha256:514ceac913076feefbeaf89771fd6febde78b0c4c1b23aaeab082c41c694e81b", + "sha256:528a2a692c65dd5cafc130de286030af251d2ee0483a5bf50c9348aefe834e8a", + "sha256:6295f6763749b89c994fcb6d8a7f7ce03c3992e695f89f00b741b4580b199b7e", + "sha256:6c8bc8238a7dfdaf7a75f5ec5a663f4173f8c367e5a39f87e720495e1eed75fa", + "sha256:718856856ba31f14f13ba885ff13874be7fefc53984d2832458f12c38205f7f7", + "sha256:7f7609a718b177bf171ac93cea9fd2ddc0e03e84d8fa4e887bdfc39671d46b00", + "sha256:80ca33961ced9c63358056bd08403ff866512038883e74f3a4bf88ad3eb66838", + "sha256:80fe64a6deb6fcfdf7b8386f2cf216d329be6f2781f7d90304351811fb591360", + "sha256:81c4b81611e3a3cb30e59b0cf05b888c675f97e3adb2c8672c3154047980726b", + "sha256:855c583f268edde09474b081e3ddcd5cf3b20c12f26e0d434e1386cc5d318e7a", + "sha256:9bfdb82cdfeccec50aad441afc332faf8606dfa5e8efd18a6692b5d6e79f00fd", + "sha256:a5d24e1d674dd9d72c66ad3ea9131322819ff86250b30dc5821cbafcfa0b96b4", + "sha256:a9f44cd7e162ac6191491d7249cceb02b8116b0f7e847ee33f739d7cb1ea1f70", + "sha256:b5b3f092fe345c03bca1e0b687dfbb39364b21ebb8ba90e3fa707374b7915204", + "sha256:b9618823bd237c0d2575283f2939655f54d51b4527ec3972907a927acbcc5bfc", + "sha256:cef9c85ccbe9bee00909758936ea841ef12035296c748aaceee535969e27d31b", + "sha256:d21237d0cd37acded35154e29aec853e945950321dd2ffd1a7d86fe686814669", + "sha256:d3c5c79ab7dfce6d88f1ba639b77e77a17ea33a01b07b99840d6ed08031cb2a7", + "sha256:d9d7942b624b04b895cb95af03a23407f17646815495ce4547f0e60e0b06f58e", + "sha256:db6d9fac65bd08cea7f3540b899977c6dee9edad959fa4eaf305940d9cbd861c", + "sha256:ede5af4a2702444a832a800b8eb7f0a7a1c0eed55b644642e049c98d589e5092", + "sha256:effb7749713d5317478bb3acb3f81d9d7c7f86726d41c1facca068a04cf5bb4c", + "sha256:f154d173286a5d1863637a7dcd8c3437bb557520b01bddb0be0258dcb72696b5", + "sha256:f25ed6e28ddf50de7e7ea99d7a976d6a9c415f03adcaac9c41ff6ff41b6d86ac" + ], + "index": "pypi", + "version": "==9.0.1" + }, "pluggy": { "hashes": [ "sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159", @@ -735,6 +776,15 @@ "index": "pypi", "version": "==0.0.5" }, + "pyzbar": { + "hashes": [ + "sha256:0e204b904e093e5e75aa85e0203bb0e02888105732a509b51f31cff400f34265", + "sha256:496249b546be70ec98c0ff0ad9151e73daaffff129266df86150a15dcd8dac4c", + "sha256:7d6c01d2c0a352fa994aa91b5540d1caeaeaac466656eb41468ca5df33be9f2e" + ], + "index": "pypi", + "version": "==0.1.8" + }, "requests": { "hashes": [ "sha256:68d7c56fd5a8999887728ef304a6d12edc7be74f1cfa47714fc8b414525c9a61", diff --git a/bot.py b/bot.py index cbf2254..ae54e4c 100644 --- a/bot.py +++ b/bot.py @@ -1,6 +1,8 @@ -from flask import Flask, request +from flask import Flask, request, abort from dotenv import load_dotenv +from pathlib import Path import os +from cruds.bot import save_img, get_isbn_by_bar_code, get_book_info_by_isbn from linebot import ( LineBotApi, WebhookHandler @@ -9,14 +11,20 @@ InvalidSignatureError ) from linebot.models import ( - MessageEvent, TextMessage, TextSendMessage, + MessageEvent, TextMessage, TextSendMessage, ImageMessage ) load_dotenv() YOUR_CHANNEL_ACCESS_TOKEN=os.environ.get('YOUR_CHANNEL_ACCESS_TOKEN') YOUR_CHANNEL_SECRET=os.environ.get('YOUR_CHANNEL_SECRET') -app = Flask(__name__) +SAVE_DIR=os.environ.get('SAVE_DIR') +SRC_IMG_PATH = SAVE_DIR + "/{}.jpg" + +if not os.path.isdir(SAVE_DIR): + os.mkdir(SAVE_DIR) + +app = Flask(__name__, static_folder="static", static_url_path="") line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN) handler = WebhookHandler(YOUR_CHANNEL_SECRET) @@ -39,11 +47,24 @@ def callback(): return 'OK' -@handler.add(MessageEvent, message=TextMessage) -def handle_message(event): +# 画像を受け取った際の処理 +@handler.add(MessageEvent, message=ImageMessage) +def handle_image(event): + message_id = event.message.id + src_img_path = SRC_IMG_PATH.format(message_id) # 保存する画像のパス + + save_img(message_id, src_img_path) # 画像を一時保存する + isbn = get_isbn_by_bar_code(src_img_path) # ISBNの取得 + book_info = get_book_info_by_isbn(isbn) + print(book_info) + + # 書籍情報を返す line_bot_api.reply_message( event.reply_token, - TextSendMessage(text=event.message.text)) + TextSendMessage(text=f"タイトル:{book_info['items'][0]['volumeInfo']['title']}\n著者:{book_info['items'][0]['volumeInfo']['authors'][0]}") + ) + # 一時保存していた画像を削除 + Path(SRC_IMG_PATH.format(message_id)).absolute().unlink() if __name__ == "__main__": app.run() diff --git a/cruds/bot.py b/cruds/bot.py index e69de29..a9c4f42 100644 --- a/cruds/bot.py +++ b/cruds/bot.py @@ -0,0 +1,43 @@ +from dotenv import load_dotenv +import os +from pathlib import Path +from pyzbar.pyzbar import decode +from PIL import Image +import requests + +from linebot import ( + LineBotApi +) + +load_dotenv() +YOUR_CHANNEL_ACCESS_TOKEN=os.environ.get('YOUR_CHANNEL_ACCESS_TOKEN') + +GOOGLE_API=os.environ.get('GOOGLE_API') + +line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN) + +# 写真の保存 +def save_img(message_id, src_img_path): + # message_idから画像のバイナリデータを取得 + message_content = line_bot_api.get_message_content(message_id) + with open(src_img_path, "wb") as f: + # バイナリを1024バイトずつ書き込む + for chunk in message_content.iter_content(): + f.write(chunk) + +# 画像内のバーコードからISBNの抽出 +def get_isbn_by_bar_code(src_img_path): + # 画像ファイルの指定 + img_path = Path(rf"{src_img_path}") + # バーコードの読取 + data = decode(Image.open(img_path)) + print(data) + return data[1][0].decode('utf-8', 'ignore') + +# ISBNから書籍情報を検索・取得 +def get_book_info_by_isbn(isbn): + # 検索リクエストURL + req_url = GOOGLE_API + isbn + # リクエスト + response = requests.get(req_url) + return response.json() \ No newline at end of file