Skip to content

Commit

Permalink
[add] 投稿された画像からバーコードを読み込み、書籍情報を取得する機能を実装
Browse files Browse the repository at this point in the history
  • Loading branch information
Simo-C3 committed Mar 14, 2022
1 parent 1836bae commit d464a7a
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode/
.env
*.pyc
static/images/*
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pyjwt = "*"
cerberus = "*"
line-bot-sdk = "*"
flask = "*"
pyzbar = "*"
pillow = "*"

[dev-packages]

Expand Down
52 changes: 51 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 27 additions & 6 deletions bot.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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()
43 changes: 43 additions & 0 deletions cruds/bot.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit d464a7a

Please sign in to comment.