-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.py
129 lines (115 loc) · 4.11 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
import os
import json
from flasgger import Swagger
from resource.celebrity_resource import Celebrity
from resource.movie_resource import Movie, MovieCelebrityList, MovieList
from resource.general_resource import PhotoList
from flask import Flask
from flask import request
from flask_caching import Cache
from flask_restful import Api
from flask_cors import CORS
from provider.httprequest_provider import HttpRequestProvider
headers = {'User-Agent': 'curl/7.64.1'}
for key in os.environ:
if key.startswith("REQUEST_HEADERS_"):
headers[key.replace("REQUEST_HEADERS_","").replace("_", "-")] = os.environ.get(key)
provider = HttpRequestProvider(headers)
app = Flask(__name__)
CORS(app)
app.config.update(RESTFUL_JSON=dict(ensure_ascii=False))
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
cache.init_app(app, config={'CACHE_TYPE': 'simple'})
api = Api(app)
options = {
'provider': provider,
'logger': app.logger
}
api.add_resource(MovieList, '/movies', resource_class_kwargs=options)
api.add_resource(Movie, '/movies/<sid>', resource_class_kwargs=options)
api.add_resource(MovieCelebrityList, '/movies/<sid>/celebrities', resource_class_kwargs=options)
api.add_resource(Celebrity, '/celebrities/<cid>', resource_class_kwargs=options)
api.add_resource(PhotoList, '/photo/<sid>', resource_class_kwargs=options)
swagger_config = {
"headers": [
],
"specs": [
{
"endpoint": 'apispec',
"route": '/apispec.json',
"rule_filter": lambda rule: True, # all in
"model_filter": lambda tag: True, # all in
}
],
"static_url_path": "/flasgger_static",
# "static_folder": "static", # must be set by user
"swagger_ui": True,
"specs_route": "/"
}
swagger = Swagger(app, config=swagger_config, template_file='docs/default.yml')
@app.route('/search', methods=['GET'])
# @cache.cached(timeout=30, query_string=True)
def api_search():
return api_full_search()
@app.route('/fullsearch', methods=['GET'])
# @cache.cached(timeout=30, query_string=True)
def api_full_search():
headers = {'content-type': 'application/json'}
keyword = request.args.get("q")
app.logger.info('the parameter is given: %s', keyword)
result = ""
try:
obj = provider.search_full_list(str(keyword))
result = json.dumps(obj, ensure_ascii=False)
except Exception as ex:
print(ex)
if result:
return result, 200, headers
return f'Results Not Found: {keyword}', 404, headers
@app.route('/partialsearch', methods=['GET'])
# @cache.cached(timeout=30, query_string=True)
def api_partial_search():
headers = {'content-type': 'application/json'}
keyword = request.args.get("q")
app.logger.info('the parameter is given: %s', keyword)
result = ""
try:
obj = provider.search_partial_list(str(keyword))
result = json.dumps(obj, ensure_ascii=False)
except Exception as ex:
print(ex)
if result:
return result, 200, headers
return f'Results Not Found: {keyword}', 404, headers
@app.route('/fetchbysid', methods=['GET'])
# @cache.cached(timeout=30, query_string=True)
def api_fetch_by_sid():
headers = {'content-type': 'application/json'}
sid = request.args.get("sid")
app.logger.info('the parameter is given: %s', sid)
result = ""
try:
obj = provider.fetch_detail_info(str(sid))
result = json.dumps(obj, ensure_ascii=False)
except Exception as ex:
print(ex)
if result:
return result, 200, headers
return f'Results Not Found: {sid}', 404, headers
@app.route('/fetchceleritiesbysid', methods=['GET'])
# @cache.cached(timeout=30, query_string=True)
def api_fetch_celerities_by_sid():
headers = {'content-type': 'application/json'}
sid = request.args.get("sid")
app.logger.info('the parameter is given: %s', sid)
result = ""
try:
obj = provider.fetch_celebrities(str(sid))
result = json.dumps(obj, ensure_ascii=False)
except Exception as ex:
print(ex)
if result:
return result, 200, headers
return f'Results Not Found: {sid}', 404, headers
if __name__ == "__main__":
app.run()