This repository has been archived by the owner on Oct 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
usrlib.py
388 lines (340 loc) · 13.6 KB
/
usrlib.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
#encoding:UTF-8
#from threading import Thread
import pickle
from configparser import ConfigParser
from os import (path,makedirs,listdir)
from urllib import (request,parse)
from flask import jsonify
from bs4 import BeautifulSoup
HEADERS = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'
}
DIRDICT = {'novellist' : './novel/list.dat',\
'noveldata' : lambda novelname : './novel/' + novelname + '/info.dat',\
'chapter_name' : lambda novelname : './novel/' + novelname + '/chapter_name.dat',\
'chapter_link' : lambda novelname : './novel/' + novelname + '/chapter_link.dat',\
'chapter' : lambda novelname,index :'./novel/' + novelname + '/' + repr(index) + '.txt'}
CONFIG = ConfigParser()
CONFIG.read('./config.ini',encoding='utf8')
def Get_ID():
'''获取搜索网站设置的ID列表'''
ID = list()
for item in CONFIG.sections():
ID.append(item)
return ID
def Search_By_ID(novelname, id):
'''获取小说信息(目录)页
novelname:小说名
id:网站设置ID
'''
opts = CONFIG[id]
__searchdata = {}
__searchdata[opts['keyword']] = novelname #构建搜索关键词
url = opts["slink"] + parse.urlencode(__searchdata, encoding='GBK') #关键词URL编码
req = request.Request(url, None, HEADERS)
try:
data = request.urlopen(req).read() #读取搜索页面内容
except:
return -1 #网站无法连接
soup = BeautifulSoup(data, "html.parser") #构建BS数据
string = 'soup.' + opts["novel_link"]
try:
url = eval(string) #获取小说页面链接
except:
return -2
if not url.startswith('http'):
url = opts["url"] + url #构建小说页面链接
# try:
# data = request.urlopen(url).read() #读取小说信息页面内容
# except:
# return -1 #小说信息页面无法连接
return url
def Get_Novel_Info(url,id):
'''获取小说信息
url:小说信息(目录)页
id:网站ID'''
opts = CONFIG[id]
noveldata = {}
req = request.Request(url, None, HEADERS)
try:
data = request.urlopen(req).read() #读取小说页面内容
except:
return -1 #小说页面无法连接
soup = BeautifulSoup(data,"html.parser") #构建BS数据
# print(data)
#--------------------------------------------------抓取小说信息
noveldata['homepage'] = opts['url']
noveldata['infolink'] = url
noveldata['id'] = opts['id']
noveldata['website'] = opts['name']
string = 'soup.'+ opts['title']
# print(string)
try:
noveldata['title'] = eval(string)
except:
# print(url,data)
return -1
try:
string = 'soup.'+opts['content_link']
string = eval(string)
if not string.startswith('http'):
string = noveldata['homepage'] + string
noveldata['content_link'] = eval(string)
except:
pass
try:
string = 'soup.'+opts['description']
noveldata['description'] = eval(string)
except:
pass
try:
string = 'soup.'+opts['category']
noveldata['category'] = eval(string)
except:
pass
try:
string = 'soup.'+opts['author']
noveldata['author'] = eval(string)
except:
pass
try:
string = 'soup.'+opts['status']
noveldata['status'] = eval(string)
except:
pass
try:
string = 'soup.'+opts['update']
noveldata['update'] = eval(string)
except:
pass
try:
string = 'soup.'+opts['latest']
noveldata['latest'] = eval(string)
except:
pass
try:
string = 'soup.'+opts['image']
except:
pass
string = eval(string)
if not string.startswith('http'):
string = noveldata['homepage'] + string
noveldata['image'] = string
# #--------------------------------------------------创建小说文件夹
# filename = './novel/' + noveldata['title'] + '/'
# if not path.exists(filename):
# makedirs(filename)
# #--------------------------------------------------写入小说信息
# filename = './novel/' + noveldata['title'] + '/' + 'info.dat'
# pickle.dump(noveldata, open(filename, "wb"))
# #--------------------------------------------------写入小说列表
# filename = './novel/' + 'list.dat'
# novel_list = list()
# for item in listdir('./novel/'):
# if path.isfile('./novel/'+item+'info.dat'):
# novel_list.append(item)
# pickle.dump(novel_list, open(filename, "wb"))
return noveldata
def Save_Content(noveldata):
"""从预定网站下载章节列表
noveldata:小说信息字典
"""
chapter_name=[]
chapter_link=[]
opts = CONFIG[noveldata['id']]
if 'content_link' in noveldata:
url = noveldata['content_link']
else:
url = noveldata['infolink']
req = request.Request(url, None, HEADERS)
try:
data = request.urlopen(req).read() #读取目录页面内容
except:
return -1 #目录页面无法连接
soup = BeautifulSoup(data,"html.parser") #构建BS数据
#--------------------------------------------------抓取小说章节列表
string = 'soup.'+opts['chapter_list']
for chapter_list in eval(string):
string = eval(opts['chapter_name'])
string = str(string)
chapter_name.append(string)
link = eval(opts['chapter_link'])
if not link.startswith('http'):
if ((link.split("/")[0] == '') & (len(link.split("/")) <= 2)) | (len(link.split("/")) <= 1):
chapter_url = url + link
else:
chapter_url = opts['url'] + eval(opts['chapter_link'])
else:
chapter_url = link
chapter_link.append(chapter_url)
#--------------------------------------------------创建小说文件夹
if not path.exists('./novel/' + noveldata['title'] + '/'):
makedirs('./novel/' + noveldata['title'] + '/')
#--------------------------------------------------写入小说信息
pickle.dump(noveldata, open(DIRDICT['noveldata'](noveldata['title']), "wb"))
#--------------------------------------------------写入小说列表
novel_list = []
for novelname in listdir('./novel/'):
if path.isfile(DIRDICT['noveldata'](novelname)):
novel_list.append(novelname)
pickle.dump(novel_list, open(DIRDICT['novellist'], "wb"))
#--------------------------------------------------写入小说目录
pickle.dump(chapter_name, open(DIRDICT['chapter_name'](noveldata['title']), "wb"))
pickle.dump(chapter_link, open(DIRDICT['chapter_link'](noveldata['title']), "wb"))
#--------------------------------------------------写入小说章节
# if end == -1:
# end = len(self.chapter_link)
# for i in range(start,end):
# data=request.urlopen(self.chapter_link[i]).read() #读取章节内容
# soup = BeautifulSoup(data,"html.parser") #构建BS数据
# text = eval('soup.'+self.opts['text'])
# filename = './novel/' + self.noveldata['title'] + '/' + repr(i) + '.txt'
# fo = open(filename, "wb")
# fo.write(text.encode('utf8'))
# fo.close()
return True
def Get_New_Chapter_List(noveldata):
'''从预定网页更新章节列表
noveldata:小说信息字典'''
update_noveldata ={}
chapter_name=[]
chapter_link=[]
update_chapter_name =[]
rt=[]
t={}
if 'content_link' in noveldata:
url = noveldata['content_link']
else:
url = noveldata['infolink']
update_noveldata = Get_Novel_Info(url,noveldata['id'])
if update_noveldata == -1:
return '-1'
if update_noveldata['latest'] == noveldata['latest']:
return '0'
opts = CONFIG[noveldata['id']]
chapter_name = pickle.load(open(DIRDICT['chapter_name'](noveldata['title']), "rb"))
req = request.Request(url, None, HEADERS)
try:
data = request.urlopen(req).read() #读取目录页面内容
except:
return '-1' #目录页面无法连接
soup = BeautifulSoup(data,"html.parser") #构建BS数据
#--------------------------------------------------抓取小说章节列表
comstr = 'soup.'+opts['chapter_list']
for chapter_list in eval(comstr):
string = eval(opts['chapter_name'])
string = str(string)
update_chapter_name.append(string)
link = eval(opts['chapter_link'])
if not link.startswith('http'):
if ((link.split("/")[0] == '') & (len(link.split("/")) <= 2)) | (len(link.split("/")) <= 1):
chapter_url = url + link
else:
chapter_url = opts['url'] + eval(opts['chapter_link'])
else:
chapter_url = link
chapter_link.append(chapter_url)
for chapter in update_chapter_name:
if not chapter in chapter_name:
rt.append({"index": update_chapter_name.index(chapter), "name": chapter})
if 'lastread' in noveldata:
update_noveldata['lastread'] = noveldata['lastread']
pickle.dump(update_noveldata, open(DIRDICT['noveldata'](noveldata['title']), "wb"))
#--------------------------------------------------写入小说目录
pickle.dump(update_chapter_name, open(DIRDICT['chapter_name'](noveldata['title']), "wb"))
pickle.dump(chapter_link, open(DIRDICT['chapter_link'](noveldata['title']), "wb"))
#t['list'] = rt
return jsonify({'list':rt})
def escape(txt,space):
'''将txt文本中的空格、&、<、>、(")、(')转化成对应的的字符实体,以方便在html上显示'''
txt = txt.replace('&','&')
txt = txt.replace(' ',' ')
txt = txt.replace('<','<')
txt = txt.replace('>','>')
txt = txt.replace('"','"')
txt = txt.replace('\'',''')
txt = txt.replace('\r',space*'<br />')
txt = txt.replace('\n',space*'<br />')
return txt
def Load_Novel_List():
'''载入小说列表'''
try:
return pickle.load(open(DIRDICT['novellist'], "rb"))
except:
print("小说列表未创建")
return None
def Load_Novel_Data(novelname):
'''载入小说信息
novelname:小说名
'''
try:
return pickle.load(open(DIRDICT['noveldata'](novelname), "rb"))
except:
print("未找到该小说")
return None
def Load_Chapter_List(novelname):
'''载入小说章节列表
novelname:小说名'''
try:
return pickle.load(open(DIRDICT['chapter_name'](novelname), "rb"))
except:
print("未找到章节列表")
return None
def Load_Chapter(novelname,index):
'''载入本地章节内容
novelname:小说名
index:章节标志位
'''
if path.isfile(DIRDICT['chapter'](novelname,index)):
return open(DIRDICT['chapter'](novelname,index),encoding='utf8').read()
else:
return -1
def Search_Chapter(novelname,index):
'''从预定网站下载指定章节
novelname:小说名
index:章节标志位
'''
noveldata = {}
chapter_link = []
try:
noveldata = pickle.load(open(DIRDICT['noveldata'](novelname), "rb"))
chapter_link = pickle.load(open(DIRDICT['chapter_link'](novelname), "rb"))
except:
return -1
#载入网站设置
opts = CONFIG[noveldata['id']]
try:
req = request.Request(chapter_link[index], None, HEADERS)
data = request.urlopen(req).read() #读取章节内容
except:
return -2
soup = BeautifulSoup(data,"html.parser") #构建BS数据
comstr = 'soup.'+ opts['text']
text = eval(comstr)
return text
def Get_Chapter(novelname,index):
'''尝试从本地与网站载入章节
novelname:小说名
index:章节标志位
'''
text = Load_Chapter(novelname,index)
if text == -1:
text = Search_Chapter(novelname,index)
if text == -1:
return '未找到章节信息!'
elif text == -2:
return '未找到章节!'
else:
open(DIRDICT['chapter'](novelname,index), "wb").write(text.encode('utf8'))
return text
if __name__ == "__main__":
# print(Search_By_ID('星辰之主','xs111'))
opts = CONFIG['bqg5200']
__searchdata = {}
__searchdata[opts['keyword']] = '寻找走丢的舰娘'
url = opts["slink"] + parse.urlencode(__searchdata)
print(url)
req = request.Request(url, None, HEADERS)
data = request.urlopen(req).read()
print(data)
pass