-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
254 lines (194 loc) · 7.45 KB
/
app.py
File metadata and controls
254 lines (194 loc) · 7.45 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
from flask import Flask, render_template, request, jsonify, redirect
from flask_jwt_extended.utils import create_access_token, current_user
from flask_jwt_extended.view_decorators import jwt_required
from flask_jwt_extended.jwt_manager import JWTManager
from utils import sql_util, nginx_util
import job
import logging
import os
import shutil
from threading import Thread
logging.basicConfig(level=logging.INFO,
format="%(asctime)s %(levelname)s %(threadName)s %(filename)s %(message)s")
ROOT_DIR = '/mnt/media'
MOVIE_DIR_RE = '(.*?)((\d{4}))'
JOB_INTERVAL = 1800
URL_PREFIX = '/share/'
if not os.path.exists('./data/secret'):
shutil.copy('./.secret', './data/secret')
if not os.path.exists('./data/secure_password'):
shutil.copy('./.secure_password', './data/secure_password')
c = job.UpdateTask()
app = Flask(__name__, static_url_path='',
static_folder='static')
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = 1728000
with open('./data/secret') as f:
secret_key = f.read()
app.config["JWT_SECRET_KEY"] = secret_key
jwt = JWTManager(app)
@jwt.user_identity_loader
def user_identity_lookup(user):
return user.id
@jwt.user_lookup_loader
def user_lookup_callback(_jwt_header, jwt_data):
identity = jwt_data["sub"]
return sql_util.get_user_or_none_by_id(identity)
@app.route("/api/user/login", methods=["POST"])
def login():
if not request.json:
return jsonify(status='error', msg='Wrong request'), 400
username = request.json.get("username", None)
password = request.json.get("password", None)
user = sql_util.get_valified_user_or_none(username, password)
if not user:
return jsonify(status='error', msg="Wrong username or password"), 401
access_token = create_access_token(identity=user)
return jsonify(access_token=access_token)
@app.route('/')
def index():
if not sql_util.get_setting_value('inited'):
return redirect('/init_page')
return render_template('index.html')
@app.route('/init_page')
def init_app_page():
if sql_util.get_setting_value('inited'):
return redirect('/')
return render_template('init.html')
@app.route("/api/user/whoiam", methods=["GET"])
@jwt_required()
def who_i_am():
return jsonify(
id=current_user.id,
username=current_user.username,
admin=current_user.admin
)
@app.route("/api/user/new", methods=["POST"])
@jwt_required()
def add_user():
if not current_user.admin:
return jsonify(status='error', msg='Not allowed'), 400
if not request.json:
return jsonify(status='error', msg='Wrong request'), 400
username = request.json.get("username", None)
password = request.json.get("password", None)
if not username or not password:
return jsonify(status='error', msg='Wrong request'), 400
user = sql_util.add_user(username, password)
if not user:
return jsonify(status='error', msg='Username exists'), 400
return jsonify(user)
@app.route("/api/user/movies")
@jwt_required()
def get_user_movies():
user_id = current_user.id
movies_json = sql_util.get_user_movies_json(user_id)
return jsonify(movies_json)
@app.route("/api/user/movie/mark", methods=['POST'])
@jwt_required()
def mark_user_movie():
if not request.json:
return jsonify(status='error', msg='Wrong request'), 400
mid = request.json.get("mid", None)
watch_status = request.json.get("watch_status", None)
comment = request.json.get("comment", '')
rating = request.json.get("rating", -1)
user_id = current_user.id
user_movie_json = sql_util.mark_user_movie(
user_id, mid, watch_status, comment, rating)
if not user_movie_json:
return jsonify(status='error', msg='Wrong request'), 400
return jsonify(user_movie_json)
@app.route('/api/movie/<int:mid>')
@jwt_required()
def get_movie_api(mid):
user_id = current_user.id
movie_json = sql_util.get_movie_json_by_id(mid, user_id)
url_prefix = URL_PREFIX
secure_passwd = nginx_util.get_secure_passwd()
movie_json['play_links'] = nginx_util.gen_movie_links(
url_prefix, secure_passwd, movie_json['video_files'])
return jsonify(movie_json)
@app.route('/api/movie/<int:mid>/recommendations')
@jwt_required()
def get_movie_recommendations_api(mid):
recommendations_json = sql_util.get_movie_recommendations_json_by_id(mid)
return jsonify(recommendations_json)
@app.route('/api/movie/db/<int:douban_id>/videos')
@jwt_required()
def get_db_movie_videos(douban_id):
video_files_str = sql_util.get_movie_video_files_by_douban_id(
str(douban_id))
if video_files_str != '':
url_prefix = URL_PREFIX
secure_passwd = nginx_util.get_secure_passwd()
video_links = nginx_util.gen_movie_links(
url_prefix, secure_passwd, video_files_str)
return jsonify(video_links)
else:
return jsonify(status='error', msg='Not found'), 404
@app.route('/api/movie/db/<int:douban_id>/id')
@jwt_required()
def get_db_movie_id(douban_id):
id_info = sql_util.get_movie_id_by_douban_id(douban_id)
return jsonify(id_info)
@app.route('/api/movies')
@jwt_required()
def get_movies():
args = request.args.to_dict()
movies_json = sql_util.get_movies_json(args)
return jsonify(movies_json)
@app.route('/api/role/<int:rid>')
@jwt_required()
def get_role_info(rid):
role_json = sql_util.get_role_json_by_id(rid)
return jsonify(role_json)
@app.route('/api/tags/top')
@jwt_required()
def get_tags():
args = request.args.to_dict()
tag_list = sql_util.get_top_tags(args)
return jsonify(tag_list)
@app.route('/api/app/init', methods=["POST"])
def init():
if not request.json:
return jsonify(status='error', msg='Wrong request'), 400
username = request.json.get("username", None)
password = request.json.get("password", None)
root_dir = request.json.get("root_dir", ROOT_DIR)
movie_dir_re = request.json.get("movie_dir_re", MOVIE_DIR_RE)
job_interval = request.json.get("job_interval", JOB_INTERVAL)
if not username or not password:
return jsonify(status='error', msg='Wrong request'), 400
result = sql_util.init_database(username, password, root_dir,
movie_dir_re, job_interval)
if result:
return jsonify(status='success', msg='App init finished'), 200
else:
return jsonify(status='error', msg='Wrong request, app has already inited'), 400
@app.route('/api/app/movie_data/update', methods=["GET"])
@jwt_required()
def update_data_immediatlly():
if not current_user.admin:
return jsonify(status='error', msg='Wrong request'), 400
if c.is_running:
return jsonify(status='error', msg='Update job is running now'), 400
t = Thread(target=c.update_movie_data)
t.start()
return jsonify(status='success', msg='Update job started'), 200
@app.route('/api/app/movie_data/update/progress', methods=["GET"])
@jwt_required()
def get_update_progress():
if not current_user.admin:
return jsonify(status='error', msg='Wrong request'), 400
return jsonify(status='success', msg=c.get_current_msg())
@app.route('/api/app/movie_data/update/log', methods=["GET"])
@jwt_required()
def get_update_log():
if not current_user.admin:
return jsonify(status='error', msg='Wrong request'), 400
return jsonify(status='success', msg=c.messages)
if __name__ == '__main__':
os.system('service nginx restart')
t = Thread(target=c.run, args=(1800,))
t.start()
app.run(host='0.0.0.0', port=5006)