forked from hang333/HbookerAppNovelDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook.py
215 lines (203 loc) · 11.6 KB
/
book.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
import HbookerAPI
from config import *
from Epub import *
import os
import shutil
class Book:
index = None
book_id = None
book_name = None
author_name = None
cover = None
book_info = None
last_chapter_info = None
division_list = None
chapter_list = None
division_chapter_list = None
epub = None
config = None
file_path = None
def __init__(self, index, book_info):
self.index = index
self.book_info = book_info
self.book_id = book_info['book_id']
self.book_name = book_info['book_name']
self.author_name = book_info['author_name']
self.cover = book_info['cover'].replace(' ', '')
self.last_chapter_info = book_info['last_chapter_info']
self.division_list = []
self.chapter_list = []
self.division_chapter_list = {}
def get_division_list(self):
print('[提示]', '正在获取书籍分卷...')
response = HbookerAPI.Book.get_division_list(self.book_id)
if response.get('code') == '100000':
self.division_list = response['data']['division_list']
def show_division_list(self):
for division in self.division_list:
print('分卷编号:', division['division_index'], ', 分卷名:', division['division_name'])
def get_chapter_catalog(self):
print('[提示]', '正在获取书籍目录...')
self.chapter_list.clear()
for division in self.division_list:
response = HbookerAPI.Book.get_chapter_update(division['division_id'])
if response.get('code') == '100000':
self.chapter_list.extend(response['data']['chapter_list'])
self.division_chapter_list[division['division_name']] = response['data']['chapter_list']
self.chapter_list.sort(key=lambda x : int(x['chapter_index']))
def show_chapter_latest(self):
print('\t最新章节: \t章节编号:', self.chapter_list[-1]['chapter_index'], ', 章节标题:',
self.chapter_list[-1]['chapter_title'])
def download_chapter(self, chapter_index_start=None, chapter_index_end=None, copy_dir=None):
if len(self.chapter_list) == 0:
print('[提示]', '暂无书籍目录')
return
self.config = Config(os.getcwd() + '/../Hbooker/' + self.book_name + '/config.json',
os.getcwd() + '/../Hbooker/' + self.book_name)
self.config.load()
if self.config.data.get('downloaded_list') is None:
self.config.data['downloaded_list'] = []
if self.config.data.get('last_chapter_index') is None:
self.config.data['last_chapter_index'] = 0
chapter_index_start = int(chapter_index_start or int(self.config.data['last_chapter_index']) + 1)
chapter_index_end = int(chapter_index_end or len(self.chapter_list))
if chapter_index_start < 1:
chapter_index_start = 1
if chapter_index_end > len(self.chapter_list):
chapter_index_end = len(self.chapter_list)
if chapter_index_start > chapter_index_end:
print('[提示][下载]', '《' + self.book_name + '》无需下载')
return
self.file_path = os.getcwd() + '/../Hbooker/' + self.book_name + '/' + self.book_name + '.epub'
self.epub = EpubFile(self.file_path,
os.getcwd() + '/../Hbooker/cache/' + self.book_name, self.book_id, self.book_name,
self.author_name)
print('[提示][下载]', '《' + self.book_name + '》', '文件名:', self.book_name + '.epub')
self.epub.setcover(self.cover)
print('[提示][下载]', '开始下载: 起始章节编号:', chapter_index_start, ', 终止章节编号:', chapter_index_end)
for i in range(chapter_index_start, chapter_index_end + 1):
if self.download_single(i) is False:
print('[提示][下载]', '遇到未付费章节,跳过之后所有章节')
break
self.epub.export()
self.epub.export_txt()
print('[提示][下载]', '《' + self.book_name + '》下载已完成')
try:
if copy_dir is not None:
copy_dir=copy_dir.replace("?","?")
file_dir, file_name = os.path.split(self.file_path)
if not os.path.isdir(copy_dir):
os.makedirs(copy_dir)
shutil.copyfile(self.file_path, copy_dir + '/' + file_name)
shutil.copyfile(self.file_path.replace('epub', 'txt'),
copy_dir + '/' + file_name.replace('epub', 'txt'))
except Exception as e:
print('[错误]', e)
print('复制文件时出错')
def download_division(self, division_index):
division_name = None
for division in self.division_list:
if division['division_index'] == division_index:
division_name = division['division_name']
break
if division_name is None:
print('[提示]', '分卷编号不正确')
return
print('[提示]', '《' + self.book_name + '》', '下载分卷:', division_name)
if len(self.division_chapter_list.get(division_name)) > 0:
self.config = Config(os.getcwd() + '/../Hbooker/' + self.book_name + '/config-' + division_name + '.json',
os.getcwd() + '/../Hbooker/' + self.book_name)
self.config.load()
if self.config.data.get('downloaded_list') is None:
self.config.data['downloaded_list'] = []
if self.config.data.get('last_chapter_index') is None:
self.config.data['last_chapter_index'] = 0
self.file_path = os.getcwd() + '/../Hbooker/' + self.book_name + '/' + self.book_name + '-' + division_name + '.epub'
self.epub = EpubFile(
self.file_path,
os.getcwd() + '/../Hbooker/cache/' + self.book_name + '-' + division_name, self.book_id, self.book_name,
self.author_name)
print('[提示][下载]', '《' + self.book_name + '》', '文件名:', self.book_name + '-' + division_name + '.epub')
self.epub.setcover(self.cover)
for chapter_info in self.division_chapter_list[division_name]:
if self.download_single_by_id(chapter_info['chapter_index'], chapter_info['chapter_id']) is False:
print('[提示][下载]', '遇到未付费章节,跳过之后所有章节')
break
self.epub.export()
self.epub.export_txt()
print('[提示][下载]', '《' + self.book_name + '》' + division_name, '下载已完成')
else:
print('[提示]', '该分卷暂无章节')
def download_single(self, i):
i = int(i)
if self.config.data['downloaded_list'].count(i) > 0:
print('[提示][下载]', '编号:', i, ' 已下载,跳过')
return True
chapter_id = self.chapter_list[i - 1]['chapter_id']
response = HbookerAPI.Chapter.get_chapter_command(chapter_id)
if response.get('code') == '100000':
chapter_command = response['data']['command']
response2 = HbookerAPI.Chapter.get_cpt_ifm(chapter_id, chapter_command)
if response2.get('code') == '100000' and response2['data']['chapter_info'].get('chapter_title') is not None:
print('[提示][下载]', '编号:', i, ', chapter_id:', chapter_id, ', 标题:',
response2['data']['chapter_info']['chapter_title'])
if response2['data']['chapter_info']['auth_access'] == '1':
content = HbookerAPI.CryptoUtil.decrypt(response2['data']['chapter_info']['txt_content'],
chapter_command).decode('utf-8')
if content.find('\n') + 1 < len(content):
if content[-1] == '\n':
content = content[:-2]
content = content.replace('\n', '</p>\r\n<p>')
author_say = response2['data']['chapter_info']['author_say'].replace('\r', '')
author_say = author_say.replace('\n', '</p>\r\n<p>')
self.epub.addchapter(str(i), chapter_id, response2['data']['chapter_info']['chapter_title'],
'<p>' + content + '</p>\r\n<p>' + author_say + '</p>')
self.config.data['downloaded_list'].append(i)
self.config.data['last_chapter_index'] = max(i, self.config.data['last_chapter_index'])
self.config.save()
return True
else:
print('[提示][下载]', '该章节未付费,无法下载')
return False
else:
self.config.data['downloaded_list'].append(i)
self.config.data['last_chapter_index'] = max(i, self.config.data['last_chapter_index'])
self.config.save()
print('[提示][下载]', '编号:', i, ', chapter_id:', chapter_id, ', 该章节为空章节,标记为已下载')
return True
def download_single_by_id(self, chapter_index, chapter_id):
chapter_index = int(chapter_index)
response = HbookerAPI.Chapter.get_chapter_command(chapter_id)
if response.get('code') == '100000':
chapter_command = response['data']['command']
response2 = HbookerAPI.Chapter.get_cpt_ifm(chapter_id, chapter_command)
if response2.get('code') == '100000' and response2['data']['chapter_info'].get('chapter_title') is not None:
print('[提示][下载]', '编号:', chapter_index, ', chapter_id:', chapter_id, ', 标题:',
response2['data']['chapter_info']['chapter_title'])
if response2['data']['chapter_info']['auth_access'] == '1':
content = HbookerAPI.CryptoUtil.decrypt(response2['data']['chapter_info']['txt_content'],
chapter_command).decode('utf-8')
if content.find('\n') + 1 < len(content):
content = content[content.find('\n') + 1:]
if content[-1] == '\n':
content = content[:-2]
content = content.replace('\n', '</p>\r\n<p>')
else:
content = ''
author_say = response2['data']['chapter_info']['author_say'].replace('\r', '')
author_say = author_say.replace('\n', '</p>\r\n<p>')
self.epub.addchapter(str(chapter_index), chapter_id, response2['data']['chapter_info']['chapter_title'],
'<p>' + content + '</p>\r\n<p>' + author_say + '</p>')
self.config.data['downloaded_list'].append(chapter_index)
self.config.data['last_chapter_index'] = max(chapter_index, self.config.data['last_chapter_index'])
self.config.save()
return True
else:
print('[提示][下载]', '该章节未付费,无法下载')
return False
else:
self.config.data['downloaded_list'].append(chapter_index)
self.config.data['last_chapter_index'] = max(chapter_index, self.config.data['last_chapter_index'])
self.config.save()
print('[提示][下载]', '编号:', chapter_index, 'chapter_id:', chapter_id, ', 该章节为空章节,标记为已下载')
return True