-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
396 lines (348 loc) · 10.6 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
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
import re
from urllib import parse
from flask import Flask
from flask import render_template
from flask import redirect
from flask import url_for
from flask import request
from flask import abort
import httpx
import zlib
#import json
#import os
from datetime import datetime
#import copy
#from flask_caching import Cache
app = Flask(__name__)
app.jinja_env.auto_reload = True
'''
config = {
"DEBUG": True, # some Flask specific configs
"CACHE_TYPE": "SimpleCache", # Flask-Caching related configs
"CACHE_DEFAULT_TIMEOUT": 300
}
app.config.from_mapping(config)
cache = Cache(app)
'''
prefix = "http://"
class DataStore():
'''
全局变量
'''
id_to_name = {}
id_to_url = {}
id_to_totalindex = {}
id_index_to_title = {}
hostip = ""
shelf = {}
catalogs = {}
#status = {}
store = DataStore()
def is_legado(ip):
"""
检查ip是否是阅读app
"""
if ip and check_ip(ip):
try:
r = httpx.get(prefix + ip, timeout=2)
if r.status_code == 200 and r.text.find("Legado") > -1:
return True
else:
return False
except:
return False
def check_ip(str):
"""
检查ip和端口格式是否正确
"""
re_exp = '^[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\:([1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]{1}|6553[0-5]|[1-9][0-9]{0,3})$'
res = re.search(re_exp, str)
if res:
return True
else:
return False
def get_bookshelf():
"""
获取书架
赋值全局变量
id_to_url
id_to_name
id_to_totalindex
hostip
"""
print("请求app获取书架")
hostip = request.cookies.get('hostip')
if hostip is None or check_ip(hostip) is False:
return False
store.id_index_to_title = {}
store.hostip = hostip
try:
books = httpx.get(prefix + hostip + "/getBookshelf")
except:
return False
books = books.json()["data"]
store.shelf = books
for book in books:
url_id = zlib.crc32(book["bookUrl"].encode('utf8'))
book["id"] = url_id
book["unread"] = book["totalChapterNum"] - book["durChapterIndex"] - 1
store.id_to_url[str(url_id)] = book["bookUrl"]
store.id_to_name[str(url_id)] = book["name"]
store.id_to_totalindex[str(url_id)] = book["totalChapterNum"]
return books
def get_chapterlist(bookurl):
'''
获取目录
赋值全局变量
id_index_to_title
'''
print("请求app获取目录")
hostip = store.hostip
bookurl = parse.quote(bookurl)
try:
r = httpx.get(prefix + hostip + "/getChapterList?url=" + bookurl)
r = r.json()["data"]
except Exception as e:
print("获取目录出错:", str(e))
return False
url_id = zlib.crc32(r[0]["bookUrl"].encode('utf8'))
store.id_index_to_title[str(url_id)] = {}
for i in r:
store.id_index_to_title[str(url_id)][str(i["index"])] = i["title"]
return r
#@cache.cached(timeout=3600, key_prefix="content/%s")
def get_book_content(bookurl, bookindex, n):
'''
获取正文
超时重试3次
'''
print("请求app获取正文")
hostip = store.hostip
bookurl = parse.quote(bookurl)
if n <= 3:
r = httpx.get(prefix + hostip + "/getBookContent?url=" + bookurl +
"&index=" + bookindex,
timeout=10)
data = r.json().get("data")
error = r.json().get("errorMsg")
if data:
return data
elif error == "未找到":
return None
else:
return ""
'''
def mkdir(path):
folder = os.path.exists(path)
if not folder:
os.makedirs(path)
def get_local_txt(url_id, index):
path = './data/' + str(url_id) + '/'
filename = str(index) + '.json'
try:
with open(path + filename, "rt", encoding='utf8') as f:
r = json.load(f)
return r
except FileNotFoundError:
return False
def set_local_txt(url_id, index, data):
path = './data/' + str(url_id) + '/'
filename = str(index) + '.json'
mkdir(path)
with open(path + filename, "w+", encoding='utf8') as f:
json.dump(data, f, ensure_ascii=False)
'''
def sync_mark(url_id, title, index):
'''
同步书签
post("/saveBookProgress", {
name: this.$store.state.readingBook.bookName,
author: this.$store.state.readingBook.bookAuthor,
durChapterIndex: index,
durChapterPos: 0,
durChapterTime: new Date().getTime(),
durChapterTitle: title,
})
'''
#hostip = store.hostip
#books = store.shelf
#ts = int(datetime.now().timestamp() * 1000)
for book in store.shelf:
if book["id"] == url_id:
'''
mark = copy.copy(book)
mark.pop("id")
mark.pop("unread")
mark["durChapterIndex"] = index
mark["durChapterTitle"] = title
mark["durChapterTime"] = ts
'''
mark = {
"name": book["name"],
"author": book["author"],
"durChapterIndex": index,
"durChapterPos": 0,
"durChapterTime": int(datetime.now().timestamp() * 1000),
"durChapterTitle": title
}
#httpx.post(prefix + hostip + "/saveBook", data=json.dumps(mark))
return mark
"""
def post_save_mark():
'''
保存书签
post("/saveBookProgress", {
name: this.$store.state.readingBook.bookName,
author: this.$store.state.readingBook.bookAuthor,
durChapterIndex: index,
durChapterPos: 0,
durChapterTime: new Date().getTime(),
durChapterTitle: title,
})
'''
#if not store.status:
# return
url_id = store.status["id"]
title = store.status["title"]
index = store.status["index"]
hostip = store.hostip
books = store.shelf
#ct = int(datetime.now().timestamp() * 1000)
for book in books:
if book["id"] == url_id:
mark = {
"name": book["name"],
"author": book["author"],
"durChapterIndex": index,
"durChapterPos": 0,
"durChapterTime": int(datetime.now().timestamp() * 1000),
"durChapterTitle": title
}
#r = httpx.post(prefix + hostip + "/saveBookProgress",
# data=json.dumps(mark))
print(mark)
mark = {}
#store.status = {}
break
"""
@app.route('/', methods=["GET"])
def hello():
return redirect(url_for("bookshelf"))
@app.route('/bookshelf/')
def bookshelf():
"""
书架页面
"""
#cache.clear()
books = get_bookshelf()
if books:
return render_template('bookshelf.html', books=books)
else:
return redirect(url_for("set_ip"))
@app.route('/bookshelf/<int:bookid>/')
def catalog(bookid):
"""
目录页面
"""
if not store.shelf:
get_bookshelf()
if store.shelf:
bookurl = store.id_to_url.get(str(bookid))
if not bookurl:
abort(404)
name = store.id_to_name[str(bookid)]
#r = store.catalogs.get(str(bookid))
#if r is None:
# r = get_chapterlist(bookurl)
r = get_chapterlist(bookurl)
if r:
#store.catalogs[str(bookid)] = r
return render_template('catalog.html', catalogs=r, name=name)
else:
return render_template('error.html', msg="请检查网络连接")
else:
return redirect(url_for("bookshelf"))
@app.route('/bookshelf/<int:bookid>/<int:index>/')
#@cache.cached(timeout=300)
def content(bookid, index):
"""
内容页面
"""
if not store.shelf:
get_bookshelf()
if store.shelf:
bookurl = store.id_to_url.get(str(bookid))
if not bookurl:
abort(404)
elif not store.id_index_to_title.get(str(bookid)):
get_chapterlist(bookurl)
n = 1
while n <= 3:
try:
r = get_book_content(bookurl, str(index), n)
n = 3
except httpx.ReadTimeout:
print("超时重试:", n, "次")
r = False
except Exception as e:
# 其他错误跳出循环
print("其他错误:" + str(e))
n = 3
r = False
n += 1
if r is None:
return render_template('error.html', msg="没有找到该章节"), 404
if r is False:
return render_template('error.html', msg="请检查网络连接"), 504
content = re.split(r'\n', r)
name = store.id_to_name[str(bookid)]
title = store.id_index_to_title[str(bookid)][str(index)]
curid = str(bookid)
total_index = store.id_to_totalindex[str(bookid)]
if index - 1 >= 0:
prev_index = index - 1
else:
prev_index = -1
if index + 1 <= total_index - 1:
next_index = index + 1
else:
next_index = -1
#prev_index = index - 1
#next_index = index + 1
word = re.sub(r"\s", "", r)
data = {
"content": content,
"name": name,
"title": title,
"bookid": curid,
"prev_index": prev_index,
"next_index": next_index,
"characters_num": len(word)
}
mark = sync_mark(bookid, data["title"], index)
#store.status = {"id": bookid, "title": data["title"], "index": index}
else:
return redirect(url_for("bookshelf"))
return render_template('content.html',
content=data["content"],
name=data["name"],
title=data["title"],
bookid=data["bookid"],
prev_index=data["prev_index"],
next_index=data["next_index"],
characters_num=data["characters_num"],
mark=mark)
#mark=json.dumps(mark))
@app.errorhandler(404)
def page_not_found(e):
return render_template('error.html', msg=e), 404
@app.route('/set_ip', methods=['GET', "POST"])
def set_ip():
if request.method == 'POST':
hostip = request.cookies.get('hostip')
if check_ip(hostip) and is_legado(hostip):
return redirect(url_for("bookshelf"))
else:
return render_template('bookshelf.html', islegado=False)
else:
return render_template('bookshelf.html', islegado=False)