-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapp.py
More file actions
142 lines (116 loc) · 3.36 KB
/
app.py
File metadata and controls
142 lines (116 loc) · 3.36 KB
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
from openBD import openBD
from ElasticsearchWrapper import ElasticsearchWrapper
from flask import Flask, render_template, request, jsonify
import json
# import logging
class CustomFlask(Flask):
'''
テンプレートのデリミタがVue.jsと競合するので、Flask側でデリミタを別の文字に変更する
参照:https://muunyblue.github.io/0b7acbba52fb92b2e9c818f7f56bac99.html
'''
jinja_options = Flask.jinja_options.copy()
jinja_options.update(dict(
block_start_string='(%',
block_end_string='%)',
variable_start_string='((',
variable_end_string='))',
comment_start_string='(#',
comment_end_string='#)',
))
app = CustomFlask(__name__)
#app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False # jsonifyで日本語が文字化けする場合の対処
# ロギング
# logging.basicConfig(
# level=logging.DEBUG,
# format='%(asctime)s %(levelname)-8s %(module)-18s %(funcName)-10s %(lineno)4s: %(message)s'
# )
@app.route("/")
def index():
'''
画面
'''
return render_template("index.html")
@app.route("/get")
def get():
'''
ISBNに対応する書籍情報の取得
'''
# パラメータからISBNコードを取得
isbn = request.args.get('isbn', default=None)
# 必要な情報を取得する
json_data = openBD().get_json(isbn) if isbn else {}
# dict型をJSON型のレスポンスに変換
response = jsonify(json_data)
return response
@app.route("/regist")
def regist():
'''
ISBNに対応する書籍情報を取得して、Elasticsearchに登録
'''
# パラメータからISBNコードを取得
isbn = request.args.get('isbn', default=None)
# logging.debug(isbn)
# 必要な情報を取得する
json_data = openBD().get_json(isbn) if isbn else {}
if json_data == None:
json_data = {}
if len(json_data) > 0:
# Elasticsearch
es = ElasticsearchWrapper('openbd', 'openbd-index')
json_data["dummy"] = "1"
# 追加
es.insert_one(json_data)
# dict型をJSON型のレスポンスに変換
response = jsonify(json_data)
return response
@app.route("/search")
def search():
'''
検索
'''
# パラメータからISBNコードを取得
isbn = request.args.get('isbn', default=None)
title = request.args.get('title', default=None)
publisher = request.args.get('publisher', default=None)
pubdate = request.args.get('pubdate', default=None)
cover = request.args.get('cover', default=None)
author = request.args.get('author', default=None)
# 検索の項目名、項目値のDictionary
items = {}
if isbn != None:
items['isbn'] = isbn
if title != None:
items['title'] = title
if publisher != None:
items['publisher'] = publisher
if pubdate != None:
items['pubdate'] = pubdate
if cover != None:
items['cover'] = cover
if author != None:
items['author'] = author
# Elasticsearch
es = ElasticsearchWrapper('openbd', 'openbd-index')
# 検索
json_data = es.search_and(items)
# dict型をJSON型のレスポンスに変換
response = jsonify(json_data)
return response
@app.route("/list")
def list():
'''
検索
'''
# 検索の項目名、項目値のDictionary
items = {}
items["dummy"] = "1"
# Elasticsearch
es = ElasticsearchWrapper('openbd', 'openbd-index')
# 検索
json_data = es.search_and(items)
# dict型をJSON型のレスポンスに変換
response = jsonify(json_data)
return response
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=8080)