Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
shing-yu committed Oct 28, 2023
1 parent e119390 commit df19ff9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
15 changes: 6 additions & 9 deletions src/fanqie_epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@
from ebooklib import epub
from urllib.parse import urljoin
import re
import datetime
import os
import time
import json
import public as p


# 定义正常模式用来下载番茄小说的函数
def fanqie_epub(url, encoding, user_agent, path_choice, data_folder, start_chapter_id):
def fanqie_epub(url, user_agent, path_choice, start_chapter_id):
headers = {
"User-Agent": user_agent
}
Expand All @@ -56,7 +54,7 @@ def fanqie_epub(url, encoding, user_agent, path_choice, data_folder, start_chapt
title = p.rename(title)

# 获取小说信息
info = soup.find("div", class_="page-header-info").get_text()
# info = soup.find("div", class_="page-header-info").get_text()

# 获取小说简介
intro = soup.find("div", class_="page-abstract-content").get_text()
Expand Down Expand Up @@ -126,7 +124,7 @@ def fanqie_epub(url, encoding, user_agent, path_choice, data_folder, start_chapt
start_index = i

# 定义目录索引
SY = ()
toc_index = ()

chapter_id_name = 0
# 遍历每个章节链接
Expand Down Expand Up @@ -175,7 +173,7 @@ def fanqie_epub(url, encoding, user_agent, path_choice, data_folder, start_chapt
text = epub.EpubHtml(title=chapter_title, file_name=f'chapter_{volume_id}_{chapter_id_name}.xhtml')
text.content = chapter_text

SY = SY + (text,)
toc_index = toc_index + (text,)
book.spine.append(text)

# 加入epub
Expand All @@ -184,7 +182,7 @@ def fanqie_epub(url, encoding, user_agent, path_choice, data_folder, start_chapt
# 打印进度信息
print(f"已获取 {chapter_title}")
book.toc = book.toc + ((epub.Section(volume_title),
SY,),)
toc_index,),)
except BaseException as e:
# 捕获所有异常,及时保存文件
print(f"发生异常: \n{e}")
Expand Down Expand Up @@ -229,8 +227,7 @@ def fanqie_epub(url, encoding, user_agent, path_choice, data_folder, start_chapt
# 定义文件名
file_path = title + ".epub"

epub.write_epub(file_path, book,{})
epub.write_epub(file_path, book, {})

print("文件已保存!")
return

2 changes: 1 addition & 1 deletion src/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def perform_user_mode_action():
return_info = fc.fanqie_c(page_url, txt_encoding, ua, type_path_num, start_chapter_id)
elif mode == 4:
# 调用番茄epub电子书模式函数
return_info = fe.fanqie_epub(page_url, txt_encoding, ua, type_path_num, data_path, start_chapter_id)
return_info = fe.fanqie_epub(page_url, ua, type_path_num, start_chapter_id)


# 检查更新
Expand Down

0 comments on commit df19ff9

Please sign in to comment.