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 15, 2023
1 parent daf44a2 commit 5ef624f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 35 deletions.
51 changes: 33 additions & 18 deletions src/fanqie_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def fanqie_d(url, encoding, user_agent, path_choice, data_folder, start_chapter_

chapter_id = None

with open(file_path, "wb") as f:
try:
# 遍历每个章节链接
for chapter in chapters[start_index:]:

Expand Down Expand Up @@ -201,24 +201,39 @@ def fanqie_d(url, encoding, user_agent, path_choice, data_folder, start_chapter_
# 在小说内容字符串中添加章节标题和内容
content += f"\n\n\n{chapter_title}\n{chapter_text}"

# 根据编码转换小说内容字符串为二进制数据
data = content.encode(encoding, errors='ignore')
# 打印进度信息
print(f"已获取 {chapter_title}")

# 保存小说更新源文件
upd_file_path = os.path.join(data_folder, f"{title}.upd")
# 获取当前系统时间
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 创建要写入元信息文件的内容
new_content = f"{current_time}\n{url}\n{chapter_id}\n{encoding}"
# 打开文件并完全覆盖内容
with open(upd_file_path, "w") as file:
file.write(new_content)
print("[DEBUG]已保存.upd更新元数据文件")

# 根据编码转换小说内容字符串为二进制数据
data = content.encode(encoding, errors='ignore')

# 保存文件
with open(file_path, "wb") as f:
f.write(data)

# 打印进度信息
print(f"已获取 {chapter_title}")
# 打印完成信息
print(f"已保存{title}.txt")
except Exception as e:
# 捕获所有异常,及时保存文件
print(f"发生异常: \n{e}")
print("正在尝试保存文件...")
# 根据编码转换小说内容字符串为二进制数据
data = content.encode(encoding, errors='ignore')

# 保存文件
with open(file_path, "wb") as f:
f.write(data)

# 保存小说更新源文件
upd_file_path = os.path.join(data_folder, f"{title}.upd")
# 获取当前系统时间
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 创建要写入元信息文件的内容
new_content = f"{current_time}\n{url}\n{chapter_id}\n{encoding}"
# 打开文件并完全覆盖内容
with open(upd_file_path, "w") as file:
file.write(new_content)
print("[DEBUG]已保存.upd更新元数据文件")

# 打印完成信息
print(f"已保存{title}.txt")
print("文件已保存!")
return
50 changes: 33 additions & 17 deletions src/fanqie_normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def fanqie_n(url, encoding, user_agent, path_choice, data_folder, start_chapter_

chapter_id = None

with open(file_path, "wb") as f:
try:
# 遍历每个章节链接
for chapter in chapters[start_index:]:
time.sleep(0.5)
Expand Down Expand Up @@ -173,23 +173,39 @@ def fanqie_n(url, encoding, user_agent, path_choice, data_folder, start_chapter_
# 在小说内容字符串中添加章节标题和内容
content += f"\n\n\n{chapter_title}\n{chapter_text}"

# 根据编码转换小说内容字符串为二进制数据
data = content.encode(encoding, errors='ignore')
# 打印进度信息
print(f"已获取 {chapter_title}")

# 保存小说更新源文件
upd_file_path = os.path.join(data_folder, f"{title}.upd")
# 获取当前系统时间
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 创建要写入元信息文件的内容
new_content = f"{current_time}\n{url}\n{chapter_id}\n{encoding}"
# 打开文件并完全覆盖内容
with open(upd_file_path, "w") as file:
file.write(new_content)

# 根据编码转换小说内容字符串为二进制数据
data = content.encode(encoding, errors='ignore')

# 保存文件
with open(file_path, "wb") as f:
f.write(data)

# 打印进度信息
print(f"已获取 {chapter_title}")
# 打印完成信息
print(f"已保存{title}.txt")

except Exception as e:
# 捕获所有异常,及时保存文件
print(f"发生异常: \n{e}")
print("正在尝试保存文件...")
# 根据编码转换小说内容字符串为二进制数据
data = content.encode(encoding, errors='ignore')

# 保存文件
with open(file_path, "wb") as f:
f.write(data)

# 保存小说更新源文件
upd_file_path = os.path.join(data_folder, f"{title}.upd")
# 获取当前系统时间
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 创建要写入元信息文件的内容
new_content = f"{current_time}\n{url}\n{chapter_id}\n{encoding}"
# 打开文件并完全覆盖内容
with open(upd_file_path, "w") as file:
file.write(new_content)

# 打印完成信息
print(f"已保存{title}.txt")
print("文件已保存!")
return

0 comments on commit 5ef624f

Please sign in to comment.