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

Commit

Permalink
feat(Standard): 对部分输出着色
Browse files Browse the repository at this point in the history
  • Loading branch information
shing-yu committed Nov 10, 2023
1 parent 9a19f46 commit 4daa162
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 26 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ beautifulsoup4
packaging
ebooklib
tqdm
colorama

# api版本
flask
Expand Down
5 changes: 4 additions & 1 deletion src/fanqie_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
from tqdm import tqdm
import hashlib
import public as p
from colorama import Fore, Style, init

init(autoreset=True)


def fanqie_b(encoding, user_agent, path_choice, data_folder):
Expand Down Expand Up @@ -170,7 +173,7 @@ def download_novels(url, encoding, user_agent, path_choice, folder_path, data_fo
api_data = api_response.json()
except Exception as e:
if retry_count == 1:
tqdm.write(f"错误:{e}")
tqdm.write(Fore.RED + Style.BRIGHT + f"发生异常: {e}")
tqdm.write(f"{chapter_title} 获取失败,正在尝试重试...")
tqdm.write(f"第 ({retry_count}/3) 次重试获取章节内容")
retry_count += 1 # 否则重试
Expand Down
5 changes: 4 additions & 1 deletion src/fanqie_chapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import time
from tqdm import tqdm
import public as p
from colorama import Fore, Style, init

init(autoreset=True)


# 定义分章节保存模式用来下载番茄小说的函数
Expand Down Expand Up @@ -126,7 +129,7 @@ def fanqie_c(url, encoding, user_agent, path_choice, start_chapter_id):
api_data = api_response.json()
except Exception as e:
if retry_count == 1:
tqdm.write(f"错误:{e}")
tqdm.write(Fore.RED + Style.BRIGHT + f"发生异常: {e}")
tqdm.write(f"{chapter_title} 获取失败,正在尝试重试...")
tqdm.write(f"第 ({retry_count}/3) 次重试获取章节内容")
retry_count += 1 # 否则重试
Expand Down
29 changes: 16 additions & 13 deletions src/fanqie_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
from tqdm import tqdm
import hashlib
import public as p
from colorama import Fore, Style, init

init(autoreset=True)


# 定义调试模式用来下载番茄小说的函数
Expand All @@ -50,24 +53,24 @@ def fanqie_d(url, encoding, user_agent, path_choice, data_folder, start_chapter_
# 获取小说标题
title = soup.find("h1").get_text()

print(f"[DEBUG]已获取小说标题")
print(Fore.YELLOW + Style.BRIGHT + f"[DEBUG]已获取小说标题")

# 替换非法字符
title = p.rename(title)

print(f"[DEBUG]已尝试替换非法字符")
print(Fore.YELLOW + Style.BRIGHT + f"[DEBUG]已尝试替换非法字符")

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

# if mode == 1:
print(f"[DEBUG]已获取小说信息")
print(Fore.YELLOW + Style.BRIGHT + f"[DEBUG]已获取小说信息")

# 获取小说简介
intro = soup.find("div", class_="page-abstract-content").get_text()

# if mode == 1:
print(f"[DEBUG]已获取小说简介")
print(Fore.YELLOW + Style.BRIGHT + f"[DEBUG]已获取小说简介")

# 拼接小说内容字符串
content = f"""如果需要小说更新,请勿修改文件名
Expand All @@ -82,13 +85,13 @@ def fanqie_d(url, encoding, user_agent, path_choice, data_folder, start_chapter_
"""

# if mode == 1:
print(f"[DEBUG]已拼接小说简介字符串")
print(Fore.YELLOW + Style.BRIGHT + f"[DEBUG]已拼接小说简介字符串")

# 获取所有章节链接
chapters = soup.find_all("div", class_="chapter-item")

# if mode == 1:
print(f"[DEBUG]已获取所有章节链接")
print(Fore.YELLOW + Style.BRIGHT + f"[DEBUG]已获取所有章节链接")

# 检查用户是否指定起始章节
start_index = 0
Expand All @@ -111,7 +114,7 @@ def fanqie_d(url, encoding, user_agent, path_choice, data_folder, start_chapter_
# 创建一个Tkinter窗口,但不显示它
root = tk.Tk()
root.withdraw()
print("[DEBUG]已创建tkinter隐形窗口")
print(Fore.YELLOW + Style.BRIGHT + "[DEBUG]已创建tkinter隐形窗口")

print("您选择了自定义保存路径,请您在弹出窗口中选择路径。")

Expand Down Expand Up @@ -148,21 +151,21 @@ def fanqie_d(url, encoding, user_agent, path_choice, data_folder, start_chapter_
time.sleep(0.25)
# 获取章节标题
chapter_title = chapter.find("a").get_text()
tqdm.write(f"[DEBUG]正在获取章节:{chapter_title}")
tqdm.write(Fore.YELLOW + Style.BRIGHT + f"[DEBUG]正在获取章节:{chapter_title}")

# 获取章节网址
chapter_url = urljoin(url, chapter.find("a")["href"])

# 获取章节 id
chapter_id = re.search(r"/(\d+)", chapter_url).group(1)

tqdm.write(f"[DEBUG]章节id:{chapter_id}")
tqdm.write(Fore.YELLOW + Style.BRIGHT + f"[DEBUG]章节id:{chapter_id}")

# 构造 api 网址
api_url = (f"https://novel.snssdk.com/api/novel/book/reader/full/v1/?device_platform=android&"
f"parent_enterfrom=novel_channel_search.tab.&aid=2329&platform_id=1&group_id="
f"{chapter_id}&item_id={chapter_id}")
tqdm.write(f"[DEBUG]api网址:{api_url}")
tqdm.write(Fore.YELLOW + Style.BRIGHT + f"[DEBUG]api网址:{api_url}")

# 尝试获取章节内容
chapter_content = None
Expand All @@ -172,14 +175,14 @@ def fanqie_d(url, encoding, user_agent, path_choice, data_folder, start_chapter_
# 获取 api 响应
api_response = requests.get(api_url, headers=headers)

tqdm.write(f"[DEBUG]HTTP状态码:{api_response}")
tqdm.write(Fore.YELLOW + Style.BRIGHT + f"[DEBUG]HTTP状态码:{api_response}")

# 解析 api 响应为 json 数据
api_data = api_response.json()

except Exception as e:
if retry_count == 1:
tqdm.write(f"错误:{e}")
tqdm.write(Fore.RED + Style.BRIGHT + f"发生异常: {e}")
tqdm.write(f"{chapter_title} 获取失败,正在尝试重试...")
tqdm.write(f"第 ({retry_count}/3) 次重试获取章节内容")
retry_count += 1 # 否则重试
Expand Down Expand Up @@ -242,7 +245,7 @@ def fanqie_d(url, encoding, user_agent, path_choice, data_folder, start_chapter_
# 打开文件并完全覆盖内容
with open(upd_file_path, "w") as file:
file.write(meta_content)
print("[DEBUG]已保存.upd更新元数据文件到用户文件夹")
print(Fore.YELLOW + Style.BRIGHT + "[DEBUG]已保存.upd更新元数据文件到用户文件夹")
print("已完成")

except BaseException as e:
Expand Down
7 changes: 5 additions & 2 deletions src/fanqie_epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import json
from tqdm import tqdm
import public as p
from colorama import Fore, Style, init

init(autoreset=True)


# 定义正常模式用来下载番茄小说的函数
Expand Down Expand Up @@ -163,7 +166,7 @@ def fanqie_epub(url, user_agent, path_choice):
api_data = api_response.json()
except Exception as e:
if retry_count == 1:
tqdm.write(f"错误:{e}")
tqdm.write(Fore.RED + Style.BRIGHT + f"发生异常: {e}")
tqdm.write(f"{chapter_title} 获取失败,正在尝试重试...")
tqdm.write(f"第 ({retry_count}/3) 次重试获取章节内容")
retry_count += 1 # 否则重试
Expand Down Expand Up @@ -206,7 +209,7 @@ def fanqie_epub(url, user_agent, path_choice):
toc_index,),)
except BaseException as e:
# 捕获所有异常,及时保存文件
print(f"发生异常: \n{e}")
print(Fore.RED + Style.BRIGHT + f"发生异常: \n{e}")
return

# 添加 navigation 文件
Expand Down
8 changes: 6 additions & 2 deletions src/fanqie_normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
from tqdm import tqdm
import hashlib
import public as p
from colorama import Fore, Style, init

init(autoreset=True)


# 定义正常模式用来下载番茄小说的函数
Expand Down Expand Up @@ -151,7 +154,7 @@ def fanqie_n(url, encoding, user_agent, path_choice, data_folder, start_chapter_
api_data = api_response.json()
except Exception as e:
if retry_count == 1:
tqdm.write(f"错误:{e}")
tqdm.write(Fore.RED + Style.BRIGHT + f"发生异常: {e}")
tqdm.write(f"{chapter_title} 获取失败,正在尝试重试...")
tqdm.write(f"第 ({retry_count}/3) 次重试获取章节内容")
retry_count += 1 # 否则重试
Expand Down Expand Up @@ -216,8 +219,9 @@ def fanqie_n(url, encoding, user_agent, path_choice, data_folder, start_chapter_
print("已完成")

except BaseException as e:

# 捕获所有异常,及时保存文件
print(f"发生异常: \n{e}")
print(Fore.RED + Style.BRIGHT + f"发生异常: \n{e}")
print("正在尝试保存文件...")
# 根据编码转换小说内容字符串为二进制数据
data = content.encode(encoding, errors='ignore')
Expand Down
17 changes: 10 additions & 7 deletions src/fanqie_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
from tqdm import tqdm
import hashlib
import public as p
from colorama import Fore, Style, init

init(autoreset=True)


# 定义番茄更新函数
Expand Down Expand Up @@ -98,7 +101,7 @@ def fanqie_update(user_agent, data_folder):
hash_sha256.update(chunk)
fact_sha256 = hash_sha256.hexdigest()
if fact_sha256 != save_sha256:
print("hash校验未通过!")
print(Fore.RED + Style.BRIGHT + "hash校验未通过!")
while True:
upd_choice = input(f"这往往意味着文件已被修改,是否继续更新?(yes/no):")
if upd_choice == "yes":
Expand All @@ -110,10 +113,10 @@ def fanqie_update(user_agent, data_folder):
else:
print("输入错误,请重新输入")
if skip_this == 1:
print(f"《{novel_name}》的更新已取消")
print(Fore.RED + Style.BRIGHT + f"《{novel_name}》的更新已取消")
continue
else:
print("hash校验通过!")
print(Fore.GREEN + Style.BRIGHT + "hash校验通过!")
print(f"上次更新时间{last_update_time}")
result = download_novel(url, encoding, user_agent, last_chapter_id, txt_file_path)
if result == "DN":
Expand Down Expand Up @@ -196,7 +199,7 @@ def download_novel(url, encoding, user_agent, start_chapter_id, txt_file_path):
api_data = api_response.json()
except Exception as e:
if retry_count == 1:
tqdm.write(f"错误:{e}")
tqdm.write(Fore.RED + Style.BRIGHT + f"发生异常: {e}")
tqdm.write(f"{chapter_title} 获取失败,正在尝试重试...")
tqdm.write(f"第 ({retry_count}/3) 次重试获取章节内容")
retry_count += 1 # 否则重试
Expand Down Expand Up @@ -295,18 +298,18 @@ def onefile(user_agent, data_folder):
hash_sha256.update(chunk)
fact_sha256 = hash_sha256.hexdigest()
if fact_sha256 != save_sha256:
print("hash校验未通过!")
print(Fore.RED + Style.BRIGHT + "hash校验未通过!")
while True:
upd_choice = input(f"这往往意味着文件已被修改,是否继续更新?(yes/no):")
if upd_choice == "yes":
break
elif upd_choice == "no":
print("更新已取消")
print(Fore.RED + Style.BRIGHT + "更新已取消")
return
else:
print("输入错误,请重新输入")
else:
print("hash校验通过!")
print(Fore.GREEN + Style.BRIGHT + "hash校验通过!")
print(f"上次更新时间{last_update_time}")
result = download_novel(url, encoding, user_agent, last_chapter_id, txt_file_path)
if result == "DN":
Expand Down

0 comments on commit 4daa162

Please sign in to comment.