Skip to content

Commit

Permalink
Update bilinovel2epub.py
Browse files Browse the repository at this point in the history
修复Windows下文件名非法字符引发的报错
  • Loading branch information
fangxx3863 committed Mar 26, 2023
1 parent c2f8bc9 commit 1784fa0
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions bilinovel2epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import shutil
from pathlib import Path
from rich.console import Console
from rich.prompt import IntPrompt
from rich.prompt import Prompt
from rich.prompt import Confirm
from ebooklib import epub
Expand Down Expand Up @@ -66,6 +65,11 @@ def 标准化JSON(s:str)->dict:
obj = eval(s, type('js', (dict,), dict(__getitem__=lambda s, n: n))())
return obj

def clean_file_name(filename:str):
invalid_chars='[\\\:*?"<>|]'
replace_char='-'
return re.sub(invalid_chars,replace_char,filename)

# 下载函数
def 下载文件(链接, 路径='file'):
if isinstance(链接, str):
Expand All @@ -75,7 +79,7 @@ def 下载文件(链接, 路径='file'):
return
try:
文件名 = "-"
文件名 = 文件名.join(链接.split("/")[-4:])
文件名 = 文件名.join(clean_file_name(链接).split("/")[-4:])
except:
return
文件存在 = Path(f"{路径}/{文件名}")
Expand Down Expand Up @@ -402,7 +406,7 @@ def 主要():
# 如果不存在则创建目录
os.makedirs("file")
下载图片集合(图片URL集合[卷名], 4)
写到书本(书名+"_"+卷名, 作者, 内容[卷名], "cover", "file/" + "-".join(封面URL.split("/")[-4:]), "file", 书名, True)
写到书本(书名+"_"+卷名, 作者, 内容[卷名], "-".join(clean_file_name(封面URL).split("/")[-4:]), "file/" + "-".join(clean_file_name(封面URL).split("/")[-4:]), "file", 书名, True)
try:
shutil.rmtree('file')
except:
Expand All @@ -413,7 +417,7 @@ def 主要():
# 如果不存在则创建目录
os.makedirs("file")
下载文件(封面URL)
写到书本(书名, 作者, 内容, "cover", "file/" + "-".join(封面URL.split("/")[-4:]), "file")
写到书本(书名, 作者, 内容, "-".join(clean_file_name(封面URL).split("/")[-4:]), "file/" + "-".join(clean_file_name(封面URL).split("/")[-4:]), "file")
try:
shutil.rmtree('file')
except:
Expand Down Expand Up @@ -471,7 +475,7 @@ def 主要():
# 如果不存在则创建目录
os.makedirs("file")
下载图片集合(图片URL集合[卷名], 4)
写到书本(书名+"_"+卷名, 作者, 内容[卷名], "cover", "file/" + "-".join(封面URL.split("/")[-4:]), "file", 书名, True)
写到书本(书名+"_"+卷名, 作者, 内容[卷名], "-".join(clean_file_name(封面URL).split("/")[-4:]), "file/" + "-".join(clean_file_name(封面URL).split("/")[-4:]), "file", 书名, True)
try:
shutil.rmtree('file')
except:
Expand All @@ -482,7 +486,7 @@ def 主要():
# 如果不存在则创建目录
os.makedirs("file")
下载文件(封面URL)
写到书本(书名, 作者, 内容, "cover", "file/" + "-".join(封面URL.split("/")[-4:]), "file")
写到书本(书名, 作者, 内容, "-".join(clean_file_name(封面URL).split("/")[-4:]), "file/" + "-".join(clean_file_name(封面URL).split("/")[-4:]), "file")
try:
shutil.rmtree('file')
except:
Expand Down

0 comments on commit 1784fa0

Please sign in to comment.