From edd85309db5ebded9f5d0fed5eab6c526c4d7cf3 Mon Sep 17 00:00:00 2001 From: liyp Date: Wed, 21 Jun 2023 23:08:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8B=E8=BD=BD=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E4=BC=98=E5=8C=96=E6=90=9C=E7=B4=A2=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E6=98=BE=E7=A4=BA=EF=BC=8C=E4=BF=AE=E5=A4=8Depub?= =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E4=BF=A1=E6=81=AF=E5=8F=8A=E5=90=88=E5=B9=B6?= =?UTF-8?q?bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Download_Novel.py | 53 ++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/Download_Novel.py b/Download_Novel.py index c58e48f..a3b8a9b 100644 --- a/Download_Novel.py +++ b/Download_Novel.py @@ -196,6 +196,7 @@ class Download_Novel: time.sleep(self.interval) + # 标志位 0 为 file = 0 # 判断是否全部下载成功 for i in range(0, len(self.chapter_urls)): @@ -203,7 +204,14 @@ class Download_Novel: if not status: file += 1 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_status = True if convert_type == 0: @@ -214,7 +222,7 @@ class Download_Novel: txt_files = [] for n in range(0, len(self.chapter_urls)): 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') if convert_status: @@ -223,13 +231,6 @@ class Download_Novel: print('合并失败!请删除downloads下面目录后重新运行程序!') exit(1) - else: - print('部分文件下载失败,限制线程数可以提高下载成功率,是否重新下载个别文件?') - download = input('0 退出\n1 重试\n') - if download == 0: - exit(0) - else: - self.download_process() # 合并为txt文件 def merge_txt_file(self, merged_file_name=''): @@ -285,7 +286,8 @@ class Download_Novel: book.set_title(self.title) book.set_language('zh') book.add_author(self.author) - book.set_direction(self.intro) + book.add_metadata('DC', 'description', self.intro) + # 添加封面 # 获取图片并将其转换为字节流 response = requests.get(self.cover) @@ -296,10 +298,8 @@ class Download_Novel: print('合并中。。。。。。') # print(txt_files) - # 书籍目录 - book_spine = [] # 遍历所有txt文件 - # os.chdir(title) + # book.spine.append('nav') for i, txt_file in enumerate(txt_files): # 读取txt文件内容 with open(txt_file, 'r', encoding='utf-8') as file: @@ -314,10 +314,12 @@ class Download_Novel: # content = ['  ' + line + '
' for line in content] # 处理html文档 - content[0] = f"""

+ content[0] = f"""
\n

{content[0]}

""" for j, line in enumerate(content[1:]): content[j + 1] = '

' + line + '

\n' + + # content.append('') except IndexError as e: print(e) @@ -327,12 +329,12 @@ class Download_Novel: chapter = epub.EpubHtml(title=chapter_title, file_name='text/' + str(i) + '.xhtml') chapter.content = ''.join(content) # 将整个文件内容作为章节内容 # 下面的是将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() style = open('./css/stylesheet.css', 'r', encoding='utf-8').read() - chapter.add_item( - epub.EpubItem(uid="page_style", file_name="../style/page_styles.css", media_type="text/css", - content=page_style)) + # chapter.add_item( + # epub.EpubItem(uid="page_style", file_name="../style/page_styles.css", media_type="text/css", + # content=page_style)) chapter.add_item( epub.EpubItem(uid="page_style1", file_name="../style/page_styles1.css", media_type="text/css", content=page_style1)) @@ -342,14 +344,17 @@ class Download_Novel: # 将章节添加到书籍中 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.EpubNav()) - book.spine = book_spine - book.toc = book_spine + + # 设置书籍的样式文件 page_style = open('./css/page_styles.css', 'r', encoding='utf-8').read() @@ -394,8 +399,8 @@ if __name__ == '__main__': print(response) print('搜索到 ' + str(len(response)) + ' 个结果\n') print('---------------------------------------\n') - for i, book in enumerate(response): - print(str(i) + ' 书籍名称:' + book['articlename'] + '\n作者:' + book['author'] + '\n简介:' + book[ + for i, book in enumerate(reversed(response)): + print(str(len(response) - 1 - i) + ' 书籍名称:' + book['articlename'] + '\n作者:' + book['author'] + '\n简介:' + book[ 'intro'] + '...\n') print('---------------------------------------') print('---------------------------------------\n')