修复下载功能,优化搜索结果显示,修复epub基本信息及合并bug

This commit is contained in:
liyp 2023-06-21 23:08:26 +08:00
parent fd3bd8656e
commit edd85309db

View file

@ -196,6 +196,7 @@ class Download_Novel:
time.sleep(self.interval) time.sleep(self.interval)
# 标志位 0 为
file = 0 file = 0
# 判断是否全部下载成功 # 判断是否全部下载成功
for i in range(0, len(self.chapter_urls)): for i in range(0, len(self.chapter_urls)):
@ -203,7 +204,14 @@ class Download_Novel:
if not status: if not status:
file += 1 file += 1
break break
if not file: if file:
print('部分文件下载失败,限制线程数可以提高下载成功率,是否重新下载个别文件?')
download = input('0 退出\n1 重试\n')
if download == 0:
exit(0)
else:
self.download_process()
else:
convert_type = int(input('下载成功!\n请输入要合并的格式:\n0 TxT文件\n1 Epub文件\n')) convert_type = int(input('下载成功!\n请输入要合并的格式:\n0 TxT文件\n1 Epub文件\n'))
convert_status = True convert_status = True
if convert_type == 0: if convert_type == 0:
@ -214,7 +222,7 @@ class Download_Novel:
txt_files = [] txt_files = []
for n in range(0, len(self.chapter_urls)): for n in range(0, len(self.chapter_urls)):
txt_files.append(self.file_path + str(n) + '.txt') txt_files.append(self.file_path + str(n) + '.txt')
# print('txt_files:',txt_files)
convert_status = self.merge_txt_to_epub(txt_files, self.download_path + self.title + '.epub') convert_status = self.merge_txt_to_epub(txt_files, self.download_path + self.title + '.epub')
if convert_status: if convert_status:
@ -223,13 +231,6 @@ class Download_Novel:
print('合并失败请删除downloads下面目录后重新运行程序') print('合并失败请删除downloads下面目录后重新运行程序')
exit(1) exit(1)
else:
print('部分文件下载失败,限制线程数可以提高下载成功率,是否重新下载个别文件?')
download = input('0 退出\n1 重试\n')
if download == 0:
exit(0)
else:
self.download_process()
# 合并为txt文件 # 合并为txt文件
def merge_txt_file(self, merged_file_name=''): def merge_txt_file(self, merged_file_name=''):
@ -285,7 +286,8 @@ class Download_Novel:
book.set_title(self.title) book.set_title(self.title)
book.set_language('zh') book.set_language('zh')
book.add_author(self.author) book.add_author(self.author)
book.set_direction(self.intro) book.add_metadata('DC', 'description', self.intro)
# 添加封面 # 添加封面
# 获取图片并将其转换为字节流 # 获取图片并将其转换为字节流
response = requests.get(self.cover) response = requests.get(self.cover)
@ -296,10 +298,8 @@ class Download_Novel:
print('合并中。。。。。。') print('合并中。。。。。。')
# print(txt_files) # print(txt_files)
# 书籍目录
book_spine = []
# 遍历所有txt文件 # 遍历所有txt文件
# os.chdir(title) # book.spine.append('nav')
for i, txt_file in enumerate(txt_files): for i, txt_file in enumerate(txt_files):
# 读取txt文件内容 # 读取txt文件内容
with open(txt_file, 'r', encoding='utf-8') as file: with open(txt_file, 'r', encoding='utf-8') as file:
@ -314,10 +314,12 @@ class Download_Novel:
# content = ['&nbsp;&nbsp;' + line + '<br>' for line in content] # content = ['&nbsp;&nbsp;' + line + '<br>' for line in content]
# 处理html文档 # 处理html文档
content[0] = f""" <div class="calibre2" id="calibre_pb_0"></div><h1 class="kindle-cn-heading" id="calibre_pb_1"> content[0] = f""" <div class="calibre2" id="calibre_pb_0"></div>\n<h1 class="kindle-cn-heading" id="calibre_pb_1">
{content[0]} </h1> """ {content[0]} </h1> """
for j, line in enumerate(content[1:]): for j, line in enumerate(content[1:]):
content[j + 1] = '<p class="calibre3">' + line + '</p>\n' content[j + 1] = '<p class="calibre3">' + line + '</p>\n'
# content.append('</body></html>') # content.append('</body></html>')
except IndexError as e: except IndexError as e:
print(e) print(e)
@ -327,12 +329,12 @@ class Download_Novel:
chapter = epub.EpubHtml(title=chapter_title, file_name='text/' + str(i) + '.xhtml') chapter = epub.EpubHtml(title=chapter_title, file_name='text/' + str(i) + '.xhtml')
chapter.content = ''.join(content) # 将整个文件内容作为章节内容 chapter.content = ''.join(content) # 将整个文件内容作为章节内容
# 下面的是将css文件引用到单个章节里面 # 下面的是将css文件引用到单个章节里面
page_style = open('./css/page_styles.css', 'r', encoding='utf-8').read() # page_style = open('./css/page_styles.css', 'r', encoding='utf-8').read()
page_style1 = open('./css/page_styles1.css', 'r', encoding='utf-8').read() page_style1 = open('./css/page_styles1.css', 'r', encoding='utf-8').read()
style = open('./css/stylesheet.css', 'r', encoding='utf-8').read() style = open('./css/stylesheet.css', 'r', encoding='utf-8').read()
chapter.add_item( # chapter.add_item(
epub.EpubItem(uid="page_style", file_name="../style/page_styles.css", media_type="text/css", # epub.EpubItem(uid="page_style", file_name="../style/page_styles.css", media_type="text/css",
content=page_style)) # content=page_style))
chapter.add_item( chapter.add_item(
epub.EpubItem(uid="page_style1", file_name="../style/page_styles1.css", media_type="text/css", epub.EpubItem(uid="page_style1", file_name="../style/page_styles1.css", media_type="text/css",
content=page_style1)) content=page_style1))
@ -342,14 +344,17 @@ class Download_Novel:
# 将章节添加到书籍中 # 将章节添加到书籍中
book.add_item(chapter) book.add_item(chapter)
book.spine.append(chapter)
book_spine.append(chapter) book.toc.append(epub.Link('text/' + str(i) + '.xhtml', chapter_title, str(i)))
# print('xxxxxxxx:','text/' + str(i) + '.xhtml', chapter_title, str(i))
# 将目录添加到书籍中 # 将目录添加到书籍中
# book.toc = toc
# book.spine = book_spine
book.add_item(epub.EpubNcx()) book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav()) book.add_item(epub.EpubNav())
book.spine = book_spine
book.toc = book_spine
# 设置书籍的样式文件 # 设置书籍的样式文件
page_style = open('./css/page_styles.css', 'r', encoding='utf-8').read() page_style = open('./css/page_styles.css', 'r', encoding='utf-8').read()
@ -394,8 +399,8 @@ if __name__ == '__main__':
print(response) print(response)
print('搜索到 ' + str(len(response)) + ' 个结果\n') print('搜索到 ' + str(len(response)) + ' 个结果\n')
print('---------------------------------------\n') print('---------------------------------------\n')
for i, book in enumerate(response): for i, book in enumerate(reversed(response)):
print(str(i) + ' 书籍名称:' + book['articlename'] + '\n作者:' + book['author'] + '\n简介:' + book[ print(str(len(response) - 1 - i) + ' 书籍名称:' + book['articlename'] + '\n作者:' + book['author'] + '\n简介:' + book[
'intro'] + '...\n') 'intro'] + '...\n')
print('---------------------------------------') print('---------------------------------------')
print('---------------------------------------\n') print('---------------------------------------\n')