-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
148 lines (119 loc) Β· 4.23 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import secrets
from flask import Flask, send_from_directory, request, flash, redirect, url_for
from werkzeug.utils import secure_filename
from werkzeug.datastructures import FileStorage
from flask_cors import CORS
from flask_graphql import GraphQLView
from admin import init_admin, init_login
from crawler import crawl_new_card, crawl_new_umamusume
from database import Base, engine, db_session
from schema import schema
app = Flask(__name__)
app.debug = True
CORS(app)
app.add_url_rule(
'/graphql',
view_func=GraphQLView.as_view(
'graphql',
schema=schema,
graphiql=True # for having the GraphiQL interface
)
)
root_password = os.environ['ROOT_PASSWORD']
Base.metadata.create_all(engine)
init_login(app)
init_admin(app, db_session)
UPLOAD_FOLDER = f'{os.path.abspath(os.path.dirname(__file__))}/static/images'
ALLOWED_EXTENSIONS = {'png'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['SECRET_KEY'] = secrets.token_urlsafe(16)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/images/<path:path>')
def send_image(path):
return send_from_directory(app.config['UPLOAD_FOLDER'], path)
@app.route('/upload', methods=['POST'])
def get_image():
params = request.get_json()
input_password = request.headers.get('Authorization')
if not input_password == root_password:
return 'field: root_password is missing or do not correct', 401
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return 'no file part', 400
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return 'no file name', 400
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
print(f"{app.config['UPLOAD_FOLDER']}/{filename}")
return 'correct', 200
else:
return 'something wrong', 500
@app.route('/ops/new/card', methods=['POST'])
def new_support_card():
params = request.get_json()
uri = params.get('new_card_uri')
input_password = params.get('root_password')
if not input_password == root_password:
return 'field: root_password is missing or do not correct', 401
if not uri:
return 'field: new_card_uri need.', 400
if crawl_new_card(uri):
return 'ok', 201
else:
return 'crawl failed', 202
@app.route('/ops/new/uma', methods=['POST'])
def new_umamusume():
params = request.get_json()
uri = params.get('new_card_uri')
input_password = params.get('root_password')
if not input_password == root_password:
return 'field: root_password is missing or do not correct', 401
if not uri:
return 'field: new_card_uri need.', 400
if crawl_new_umamusume(uri):
return 'ok', 201
else:
return 'crawl failed', 202
@app.route('/health-check')
def health_check():
return 'ok', 200
@app.route('/ops/update/card', methods=['POST'])
def update_support_card():
params = request.get_json()
uri = params.get('new_card_uri')
input_password = params.get('root_password')
if not input_password == root_password:
return 'field: root_password is missing or do not correct', 401
if not uri:
return 'field: new_card_uri need.', 400
if crawl_new_card(uri, True):
return 'ok', 201
else:
return 'crawl failed', 202
@app.route('/ops/update/uma', methods=['POST'])
def update_umamusume():
params = request.get_json()
uri = params.get('new_card_uri')
input_password = params.get('root_password')
if not input_password == root_password:
return 'field: root_password is missing or do not correct', 401
if not uri:
return 'field: new_card_uri need.', 400
if crawl_new_umamusume(uri, True):
return 'ok', 201
else:
return 'crawl failed', 202
@app.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=5000)