-
Notifications
You must be signed in to change notification settings - Fork 3
/
Epub.py
212 lines (195 loc) · 8.22 KB
/
Epub.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import os
import zipfile
from Lib.Network import Network
def mkdir(path):
if os.path.exists(path) != True:
os.mkdir(path)
def ZIP_single(root_path, dir_path):
'''
root_path:文件夹根目录
dir_path:文件夹内文件夹路径
'''
# 获取子目录的名称
dir_name = os.path.basename(dir_path)
zip_file_path = os.path.join(root_path, dir_name + ".epub")
with zipfile.ZipFile(zip_file_path, "w", compression=zipfile.ZIP_STORED) as zipf:
# 递归地添加子目录中的文件和文件夹到归档文件
for root, dirs, files in os.walk(dir_path):
for file in files:
file_path = os.path.join(root, file)
arc_name = os.path.relpath(file_path, dir_path)
zipf.write(file_path, arcname=arc_name)
class Epub():
def __init__(self, name, url) -> None:
self.name = name
self.url = url
self.forder_init()
self.list = []
self.id = 1
self.pics = []
def forder_init(self):
mkdir(".tmp")
mkdir(f".tmp/{self.name}")
mkdir(f".tmp/{self.name}/META-INF")
with open(f".tmp/{self.name}/META-INF/container.xml", "w", encoding="utf-8") as f:
f.write('''<?xml version="1.0" encoding="UTF-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>''')
with open(f".tmp/{self.name}/mimetype", "w", encoding="utf-8") as f:
f.write('''application/epub+zip''')
mkdir(f".tmp/{self.name}/OEBPS")
mkdir(f".tmp/{self.name}/OEBPS/Images")
mkdir(f".tmp/{self.name}/OEBPS/Styles")
mkdir(f".tmp/{self.name}/OEBPS/Text")
with open("cover.jpg","rb") as f:
b = f.read()
with open(f'.tmp/{self.name}/OEBPS/Images/cover.jpg',"wb") as f:
f.write(b)
def plugin(self, Net: Network):
self.s = Net
def download(self,url:list):
fin = []
for i in url:
try:
r = self.s.get(i)
if r.status_code == 200:
with open(f'''.tmp/{self.name}/OEBPS/Images/{i.split("/")[-1]}''',"wb") as f:
f.write(r.content)
self.pics.append(i)
fin.append(i)
except:
print(f"[ERR]:\t{i}\t下载失败,是否将文件正常加入EPUB图片清单和插入章节Y/N")
inputs = input('>')
if inputs == "Y":
print(f'''[TIPS]:您需要手动下载该文件置于.tmp/{self.name}/OEBPS/Images/{i.split("/")[-1]}''')
self.pics.append(i)
fin.append(i)
return fin
def cover(self, text):
with open(f".tmp/{self.name}/OEBPS/Text/cover.xhtml", "w", encoding="utf-8") as f:
f.write(f'''<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>书籍封面</title>
</head>
<body>
<div style="text-align: center; padding: 0pt; margin: 0pt;">
<svg xmlns="http://www.w3.org/2000/svg" height="100%" preserveAspectRatio="xMidYMid meet" version="1.1" viewBox="0 0 179 248" width="100%" xmlns:xlink="http://www.w3.org/1999/xlink">
<image height="248" width="179" xlink:href="../Images/cover.jpg"></image>
</svg>
</div>
<h1>{self.name}</h1>
<h2>{self.url}</h2>
<h3>更新時間: 我不知道</h3>
<h3>簡介:</h3>
''')
o = text.replace("\n", "").split("<br />")
for i in o:
if i != "":
f.write(f"<p> {i}</p>\n")
f.write("</body>\n</html>\n")
self.list.append({
"id": "cover",
"url": "Text/cover.xhtml",
"title": "书籍封面"
})
def add_text(self, text, title, pics=[]):
pics = self.download(pics)
o = text.replace("\n", "").split("<br />")
with open(f".tmp/{self.name}/OEBPS/Text/{self.id:06d}.xhtml", "w", encoding="utf-8") as f:
f.write(f'''<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{title}</title>
</head>
<body>
<h3>{title}</h3>\n''')
for i in o:
if i != "":
f.write(f"<p> {i}</p>\n")
for i in pics:
f.write(
f'''<p> <img src="../Images/{i.split("/")[-1]}" alt=""/></p>\n''')
f.write("</body></html>")
self.list.append({
"id": f"{self.id:06d}",
"url": f"Text/{self.id:06d}.xhtml",
"title": title
})
self.id += 1
def finish(self):
with open(f".tmp/{self.name}/OEBPS/toc.ncx", "w", encoding="utf-8") as f:
f.write(f'''<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN"
"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
<head>
<meta content="hbooker:100012892" name="dtb:uid"/>
<meta content="2" name="dtb:depth"/>
<meta content="0" name="dtb:totalPageCount"/>
<meta content="0" name="dtb:maxPageNumber"/>
</head>
<docTitle>
<text>{self.name}</text>
</docTitle>
<docAuthor>
<text>{self.url}</text>
</docAuthor>
<navMap>\n''')
i = 1
while i <= len(self.list):
f.write(
f'''<navPoint id="{self.list[i-1]["id"]}" playOrder="{i}"><navLabel><text>{self.list[i-1]["title"]}</text></navLabel><content src="{self.list[i-1]["url"]}" /></navPoint>\n''')
i += 1
f.write("</navMap>\n</ncx>\n")
with open(f".tmp/{self.name}/OEBPS/content.opf", "w", encoding="utf-8") as f:
f.write(f'''<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="BookId" version="2.0">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:identifier id="BookId">{self.url}</dc:identifier>
<dc:title>{self.name}</dc:title>
<dc:creator opf:role="aut">{self.url}</dc:creator>
<dc:language>zh-CN</dc:language>
<dc:publisher></dc:publisher>
</metadata>
<manifest>
<item href="toc.ncx" id="ncx" media-type="application/x-dtbncx+xml" />
<item href="Images/cover.jpg" id="cover.jpg" media-type="image/jpeg" />
''')
#可以在此处</metadata>之前加入<meta name="cover" content="cover.jpg"/>,加入后QQ的epub插件会出现异常,移除后正常,但封面章节内的文字依旧不显示
for i in self.list:
f.write(
f'''<item href="{i["url"]}" id="{i["url"].replace("Text/", "")}" media-type="application/xhtml+xml" />\n''')
for i in self.pics:
f.write(
f'''<item href="Images/{i.split("/")[-1]}" id="{i.split("/")[-1]}" media-type="image/jpeg" />\n''')
f.write('''</manifest>\n<spine toc="ncx">\n''')
for i in self.list:
f.write(
'''<itemref idref="{}" />\n'''.format(i["url"].replace("Text/", "")))
f.write(
'''</spine>\n<guide>\n<reference href="Text/cover.xhtml" title="书籍封面" type="cover" />\n</guide>\n</package>''')
ZIP_single(".tmp", os.path.join(".tmp", self.name))
class TXT():
def __init__(self,name) -> None:
self.f = open(f".tmp/{name}.txt","w",encoding="utf-8")
def add(self,text):
self.f.write(text.replace("<br />",""))
self.f.write("\n")
def __del__(self):
self.f.close()
if __name__ == '__main__':
e = Epub("test", "adw")
e.cover("ABABA")
n = Network({})
e.plugin(n)
e.add_text("aaa", "wuti", [
"https://image.nmb.best/image/2022-09-13/6320726e73bd9.jpg"])
e.finish()