|
|
@@ -0,0 +1,68 @@
|
|
|
+import base64
|
|
|
+import hashlib
|
|
|
+import time
|
|
|
+import re
|
|
|
+import requests
|
|
|
+import zipfile
|
|
|
+import os
|
|
|
+from filetools import FileTools
|
|
|
+from template import Template
|
|
|
+
|
|
|
+file_tools= FileTools()
|
|
|
+template=Template(file_tools.get_ab_path('templates'))
|
|
|
+
|
|
|
+class Epub:
|
|
|
+ id = ""
|
|
|
+ name = ""
|
|
|
+ temp_dir = "" # 相对路径
|
|
|
+
|
|
|
+ def __init__(self, name):
|
|
|
+ self.name=name
|
|
|
+ self.id = str(base64.b64encode(bytes(hashlib.md5(base64.b64encode(
|
|
|
+ bytes(name, encoding='utf-8'))).hexdigest(), encoding='utf-8'))[-10:-1], encoding='utf-8')
|
|
|
+ self.temp_dir='temp/%s%s' % (int(time.time()), self.id)
|
|
|
+ file_tools.mkdir(self.temp_dir)
|
|
|
+ self.write_base_file()
|
|
|
+
|
|
|
+ def get_path(self, file):
|
|
|
+ return "%s/%s" % (self.temp_dir, file)
|
|
|
+
|
|
|
+ def write_base_file(self):
|
|
|
+ file_tools.mkdir(self.get_path('META-INF'))
|
|
|
+ file_tools.mkdir(self.get_path('OEBPF'))
|
|
|
+ file_tools.mkdir(self.get_path('OEBPF/content'))
|
|
|
+ file_tools.mkdir(self.get_path('OEBPF/css'))
|
|
|
+ file_tools.mkdir(self.get_path('OEBPF/images'))
|
|
|
+ file='mimetype'; file_tools.write(self.get_path(file), template.get(file))
|
|
|
+ file='META-INF/container.xml'; file_tools.write(self.get_path(file), template.get(file))
|
|
|
+ file='OEBPF/css/ebook.css'; file_tools.write(self.get_path(file), template.get(file))
|
|
|
+ file='OEBPF/css/ebook.css'; file_tools.write(self.get_path(file), template.get(file))
|
|
|
+ file='OEBPF/ebook.opf'; file_tools.write(self.get_path(file), template.get(file, name=self.name, id=self.id))
|
|
|
+ file='OEBPF/navigation.ncx'; file_tools.write(self.get_path(file), template.get(file, name=self.name, id=self.id))
|
|
|
+
|
|
|
+ def image_process(self, text):
|
|
|
+ pattern = re.compile(r'https://sdjrzk-1251357229.cos.ap-guangzhou.myqcloud.com/exam/paper/\d+/images/\d+.[a-z]{3}')
|
|
|
+ res=pattern.findall(text)
|
|
|
+ if res:
|
|
|
+ header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
|
|
|
+ 'Chrome/63.0.3239.132 Safari/537.36'}
|
|
|
+ for image in res:
|
|
|
+ temp_file=image.replace('/images/','_').replace('https://sdjrzk-1251357229.cos.ap-guangzhou.myqcloud.com/exam/paper/','%s/OEBPF/images/' % self.temp_dir)
|
|
|
+ pic=requests.get(image, headers=header)
|
|
|
+ print(file_tools.write_b(temp_file, pic.content))
|
|
|
+ return text.replace('/images/','_').replace('https://sdjrzk-1251357229.cos.ap-guangzhou.myqcloud.com/exam/paper/','../images/')
|
|
|
+
|
|
|
+ def process(self, text):
|
|
|
+ cwd=os.getcwd()
|
|
|
+ text = self.image_process(text)
|
|
|
+ file='OEBPF/content/s1.xhtml'; file_tools.write(self.get_path(file), template.get(file, title=self.name, data=text))
|
|
|
+ z=zipfile.ZipFile(file_tools.get_ab_path("output/%s.epub"%self.name), 'w', zipfile.ZIP_STORED)
|
|
|
+ os.chdir(file_tools.get_ab_path(self.temp_dir))
|
|
|
+ for dirpath, dirnames, filenames in os.walk('./'):
|
|
|
+ for filename in filenames:
|
|
|
+ z.write(os.path.join(dirpath, filename))
|
|
|
+ os.chdir(cwd)
|
|
|
+
|
|
|
+ def clean(self):
|
|
|
+ file_tools.rm(self.temp_dir)
|
|
|
+
|