-
Notifications
You must be signed in to change notification settings - Fork 6
/
manage.py
496 lines (419 loc) · 14.6 KB
/
manage.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import os
from flask import *
from manager import *
import io
# import urllib.parse
import threading
import re
import time
import requests
import hashlib
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from database import DataBase
import error_report
import xiaoice
db = DataBase()
app = Flask(__name__)
threads = []
my_email = 'LanceLiang2018@163.com'
my_password = '1352040930wenku8'
def get_icon(email):
return'https://s.gravatar.com/avatar/' + hashlib.md5(email.lower().encode()).hexdigest() + '?s=34'
def has_file(target):
r = requests.get(target, stream=True)
if int(r.status_code) == 200:
return True
return False
def file_size(target):
r = requests.get(target, stream=True)
if int(r.status_code) == 200:
return int(r.headers['Content-Length'])
return 0
def local_check(book_id):
wk = Wenku8ToEpub()
filename_ = wk.id2name(book_id) + '.epub'
info = wk.book_info(book_id)
if info is None:
return '1' # 需要更新
# 检查上次上传时间
last_time = v2_check_time(filename_)
if last_time is None:
return '1'
last_time = last_time[:10]
if last_time > info['update']:
return '0'
return '1' # 需要更新
def send_email(user, email, message):
# print(user, message)
my_sender = 'LanceLiang2018@163.com' # 发件人邮箱账号
my_pass = '1352040930smtp' # 发件人邮箱密码
# my_user = '1352040930@qq.com' # 收件人邮箱账号
try:
# print('try to send:', user)
msg = MIMEText(message, 'plain', 'utf-8')
msg['From'] = formataddr(["USER:%s" % user, my_sender]) # 括号里的对应发件人邮箱昵称、发件人邮箱账号
msg['To'] = formataddr(['Lance Liang', my_sender]) # 括号里的对应收件人邮箱昵称、收件人邮箱账号
msg['Subject'] = "来自 %s(%s) 的新消息" % (user, email) # 邮件的主题,也可以说是标题
server = smtplib.SMTP_SSL("smtp.163.com", 465) # 发件人邮箱中的SMTP服务器,端口是465
server.login(my_sender, my_pass) # 括号中对应的是发件人邮箱账号、邮箱密码
server.sendmail(my_sender, [my_sender, ], msg.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.quit() # 关闭连接
except Exception as e:
print(e)
error_report.report_it(e)
def send_email_2(user, email, message):
my_sender = 'LanceLiang2018@163.com' # 发件人邮箱账号
my_pass = '1352040930smtp' # 发件人邮箱密码
# my_user = '1352040930@qq.com' # 收件人邮箱账号
try:
# print('try to send:', user)
msg = MIMEText(message, 'plain', 'utf-8')
msg['From'] = formataddr(["Lance Liang", my_sender]) # 括号里的对应发件人邮箱昵称、发件人邮箱账号
msg['To'] = formataddr(['%s' % user, email]) # 括号里的对应收件人邮箱昵称、收件人邮箱账号
msg['Subject'] = "Re:您在wenku8.herokuapp.com的反馈" # 邮件的主题,也可以说是标题
server = smtplib.SMTP_SSL("smtp.163.com", 465) # 发件人邮箱中的SMTP服务器,端口是465
server.login(my_sender, my_pass) # 括号中对应的是发件人邮箱账号、邮箱密码
server.sendmail(my_sender, [email, ], msg.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.quit() # 关闭连接
except Exception as e:
print(e)
error_report.report_it(e)
@app.route('/', methods=['GET'])
def index():
# return '<a href="https://github.com/LanceLiang2018/Wenku8ToEpub-Online">' \
# 'https://github.com/LanceLiang2018/Wenku8ToEpub-Online</a>'
local = False
if os.environ.get('WENKU8_LOCAL', 'False') == 'True':
local = True
urls = make_urls()
return render_template('index.html', local=local, urls=urls)
@app.route('/bookinfo/<int:book_id>', methods=['GET'])
def get_bookinfo(book_id: int):
wk = Wenku8ToEpub()
filename_ = wk.id2name(book_id) + '.epub'
info = wk.book_info(book_id)
if info is None:
return json.dumps({})
# 检查上次上传时间
last_time = v2_check_time(filename_)
info['update_time'] = last_time
return json.dumps(info)
@app.route('/bookinfo_dmzj/<int:book_id>', methods=['GET'])
def get_dmzj_bookinfo(book_id: int):
de = Dmzj2Epub()
info = de.info(book_id)
filename_ = 'dmzj_%s.epub' % info['name']
if info is None:
return json.dumps({})
# 检查上次上传时间
last_time = v2_check_time(filename_)
info['update_time'] = last_time
return json.dumps(info)
@app.route('/v2/check/<book_id>', methods=['GET'])
def v2_check(book_id):
book_id = str(book_id)
if book_id.startswith('dmzj_'):
# 没有动态获取,直接返回“需要”
de = Dmzj2Epub()
try:
book_id = int(book_id.split('dmzj_')[-1])
except ValueError:
return '1'
info = de.info(book_id)
if info is None:
return '1'
# 检查上次上传时间
filename_ = 'dmzj_%s.epub' % info['name']
last_time = v2_check_time(filename_)
if last_time is None:
return '1'
last_time = last_time[:10]
update = list(time.localtime(info['last_update_time']))
update_time = '%s-%s-%s' % (update[0], update[1], update[2])
if last_time > update_time:
return '0'
return '1' # 需要更新
try:
book_id = int(book_id)
except ValueError:
return '1'
wk = Wenku8ToEpub()
filename_ = wk.id2name(book_id) + '.epub'
info = wk.book_info(book_id)
if info is None:
return '1' # 需要更新
# 检查上次上传时间
last_time = v2_check_time(filename_)
if last_time is None:
return '1'
last_time = last_time[:10]
if last_time > info['update']:
return '0'
return '1' # 需要更新
@app.route('/v2/search/<string:key>', methods=['GET'])
def v2_search(key: str):
wk = Wenku8ToEpub()
results = wk.search(key)
return json.dumps(results)
@app.route('/v2_dmzj/search/<string:key>', methods=['GET'])
def v2_dmzj_search(key: str):
de = Dmzj2Epub()
results = de.search(key)
return json.dumps(results)
@app.route('/v2/name/<string:book_name>')
def v2_jump_by_name(book_name):
filename = "%s.epub" % book_name
target = 'https://light-novel-1254016670.cos.ap-guangzhou.myqcloud.com/%s' % filename
if has_file(target):
return target
return ''
@app.route('/v2/cache/<int:book_id>')
def v2_cache(book_id: int, image=False):
wk = Wenku8ToEpub()
filename_ = wk.id2name(book_id)
if filename_ == '':
return '1'
for t in threads:
if t['bid'] == book_id:
return '2'
mlogger = MLogger()
th = threading.Thread(target=v2_work, args=(book_id, None, mlogger, image))
th.setDaemon(True)
th.start()
# filename = "%s.epub" % filename_
# url = 'https://light-novel-1254016670.cos.ap-guangzhou.myqcloud.com/%s' % filename
threads.append({
'bid': book_id,
'th': th,
'messages': mlogger,
# 'result': url
})
# url = work(book_id)
return '0'
@app.route('/v2_dmzj/cache/<int:book_id>')
def v2_dmzj_cache(book_id: int, image=False):
de = Dmzj2Epub()
info = de.info(book_id)
if info is None:
return '1'
for t in threads:
if t['bid'] == book_id:
return '2'
mlogger = MLogger()
th = threading.Thread(target=v2_dmzj_work, args=(book_id, None, mlogger, image))
th.setDaemon(True)
th.start()
# filename = "%s.epub" % filename_
# url = 'https://light-novel-1254016670.cos.ap-guangzhou.myqcloud.com/%s' % filename
threads.append({
'bid': 'dmzj_' + str(book_id),
'th': th,
'messages': mlogger,
# 'result': url
})
# url = work(book_id)
return '0'
@app.route('/v2/cache_img/<int:book_id>')
def v2_cache_img(book_id: int):
return v2_cache(book_id, image=True)
@app.route('/v2_dmzj/cache_img/<int:book_id>')
def v2_dmzj_cache_img(book_id: int):
return v2_dmzj_cache(book_id, image=True)
@app.route('/v2/cache_status/<book_id>')
def v2_cache_status(book_id):
book_id = str(book_id)
for t in threads:
if t['bid'] == book_id:
if t['th'].isAlive():
return '0'
else:
# url = t['result']
threads.remove(t)
url = th_results.get(str(book_id))
if url is None:
return '1'
return url
return '1'
@app.route('/v2/cache_logs/<book_id>')
def v2_cache_logs(book_id):
book_id = str(book_id)
for t in threads:
if t['bid'] == book_id:
data = t['messages'].read_all()
return data
return ''
@app.route('/v2/get/<int:book_id>')
def v2_get(book_id: int):
wk = Wenku8ToEpub()
filename_ = wk.id2name(book_id)
if filename_ == '':
return ''
filename = "%s.epub" % filename_
filename = urllib.parse.quote(filename)
target = 'https://light-novel-1254016670.cos.ap-guangzhou.myqcloud.com/%s' % filename
if has_file(target):
return target
return ''
@app.route('/v2_dmzj/get/<int:book_id>')
def v2_dmzj_get(book_id: int):
de = Dmzj2Epub()
info = de.info(book_id)
if info is None:
return ''
filename_ = 'dmzj_%s' % info['name']
filename = "%s.epub" % filename_
filename = urllib.parse.quote(filename)
target = 'https://light-novel-1254016670.cos.ap-guangzhou.myqcloud.com/%s' % filename
if has_file(target):
return target
return ''
@app.route('/v2/comments', methods=['GET'])
def v2_comments():
data = db.get_comments(show_email=False)
return json.dumps(data)
@app.route('/v2/feedback', methods=['POST'])
def v2_feedback():
form = dict(request.form)
message = form.get('message', '')
user = form.get('user', '')
email = form.get('email', '')
password = form.get('password', '')
head = get_icon(email)
logger.info(str((user, email, message, password)))
if len(password) > 0:
if password == my_password:
# 老子是管理员,给别人发消息,user是名字。
target_email = db.find_email(user)
if '' == target_email:
return '邮箱查找失败'
send_email_2(user, target_email, message)
db.put_comment('Lance->@%s' % user, my_email, message, head)
return '管理员操作成功'
else:
return '管理员密码错误'
else:
send_email(user, email, message)
db.put_comment(user, email, message, head)
pass
return ''
@app.route('/v2/visitors')
def v2_visitors():
api = 'https://api.baidu.com/json/tongji/v1/ReportService/getData'
r = requests.post(api)
return '0'
@app.route('/cache/<int:book_id>')
def cache(book_id: int):
wk = Wenku8ToEpub()
filename_ = wk.id2name(book_id)
if filename_ == '':
return '没有这个小说!'
url = work(book_id)
return redirect(url)
@app.route('/cache_img/<int:book_id>')
def cache_img(book_id: int):
wk = Wenku8ToEpub()
filename_ = wk.id2name(book_id)
if filename_ == '':
return '没有这个小说!'
url = work4(book_id)
return redirect(url)
@app.route('/no_cache/<int:book_id>')
def no_cache(book_id: int):
wk = Wenku8ToEpub()
filename_ = wk.id2name(book_id)
if filename_ == '':
return '没有这个小说!'
data = work3(book_id)
fp = io.BytesIO(data)
# urlencode方案
# filename_ = urllib.parse.urlencode({'': filename_})[1:] + '.epub'
# latin-1 方案
filename_ = ("%s.epub" % filename_).encode().decode('latin-1')
response = make_response(send_file(fp, attachment_filename="%s" % filename_))
response.headers["Content-Disposition"] = "attachment; filename=%s;" % filename_
return response
# url = work3(book_id)
# return redirect(url)
@app.route('/get/<int:book_id>')
def get(book_id: int):
wk = Wenku8ToEpub()
filename_ = wk.id2name(book_id)
if filename_ == '':
return '没有这个小说!'
filename = "%s.epub" % filename_
return redirect('https://light-novel-1254016670.cos.ap-guangzhou.myqcloud.com/%s' % filename)
@app.route('/name/<string:book_name>')
def jump_by_name(book_name: str):
filename = "%s.epub" % book_name
return redirect('https://light-novel-1254016670.cos.ap-guangzhou.myqcloud.com/%s' % filename)
@app.route('/search')
def search():
args0 = dict(request.args)
args = {}
for arg in args0:
v = args0[arg]
if type(v) is list:
args[arg] = v[0]
else:
args[arg] = v
# print(args)
method = args['method']
search_key = args['search_key']
bid = None
if method == 'id':
try:
bid = int(search_key)
except ValueError:
return "ID输入错误!"
return redirect('/get/%s' % bid)
elif method == 'name':
return redirect('/name/%s' % search_key)
elif method == 'cache':
return redirect('/cache/%s' % search_key)
elif method == 'cache_img':
return redirect('/cache_img/%s' % search_key)
else:
return '参数不正确'
@app.route('/favicon.ico', methods=['GET'])
def favicon():
return redirect('/static/favicon.ico')
@app.route('/baidu_verify_kBBfcDGnTX.html', methods=['GET'])
def baidu_verify():
return 'kBBfcDGnTX'
@app.route('/chat/<string:text>', methods=['GET', 'POST'])
def server_chat(text):
try:
# 先下载
headers_data = requests.get('https://cdn-1254016670.cos.ap-chengdu.myqcloud.com/headers.txt').content
with open('headers.txt', 'wb') as f:
f.write(headers_data)
response = xiaoice.chat(text)
# 然后上传
with open('headers.txt', 'rb') as f:
response1 = client2.put_object(
Bucket=bucket2,
Body=f.read(),
# Key=filename_md5,
Key="headers.txt",
StorageClass='STANDARD',
EnableMD5=False
)
logger.info(str(response1))
except Exception as e:
result = {
'data': '',
'other': str(e),
'code': 1
}
return 'foo(' + json.dumps(result) + ')'
result = {
'data': response,
'other': response1,
'code': 0
}
return 'foo(' + json.dumps(result) + ')'
if __name__ == '__main__':
# os.environ['WENKU8_LOCAL'] = "True"
app.run("0.0.0.0", port=int(os.environ.get('PORT', '8000')), debug=False)