epub.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import base64
  2. import hashlib
  3. import time
  4. import re
  5. import requests
  6. import zipfile
  7. import os
  8. from filetools import FileTools
  9. from template import Template
  10. file_tools= FileTools()
  11. template=Template(file_tools.get_ab_path('templates'))
  12. class Epub:
  13. id = ""
  14. name = ""
  15. temp_dir = "" # 相对路径
  16. def __init__(self, name):
  17. self.name=name
  18. self.id = str(base64.b64encode(bytes(hashlib.md5(base64.b64encode(
  19. bytes(name, encoding='utf-8'))).hexdigest(), encoding='utf-8'))[-10:-1], encoding='utf-8')
  20. self.temp_dir='temp/%s%s' % (int(time.time()), self.id)
  21. file_tools.mkdir(self.temp_dir)
  22. self.write_base_file()
  23. def get_path(self, file):
  24. return "%s/%s" % (self.temp_dir, file)
  25. def write_base_file(self):
  26. file_tools.mkdir(self.get_path('META-INF'))
  27. file_tools.mkdir(self.get_path('OEBPF'))
  28. file_tools.mkdir(self.get_path('OEBPF/content'))
  29. file_tools.mkdir(self.get_path('OEBPF/css'))
  30. file_tools.mkdir(self.get_path('OEBPF/images'))
  31. file='mimetype'; file_tools.write(self.get_path(file), template.get(file))
  32. file='META-INF/container.xml'; file_tools.write(self.get_path(file), template.get(file))
  33. file='OEBPF/css/ebook.css'; file_tools.write(self.get_path(file), template.get(file))
  34. file='OEBPF/css/ebook.css'; file_tools.write(self.get_path(file), template.get(file))
  35. file='OEBPF/ebook.opf'; file_tools.write(self.get_path(file), template.get(file, name=self.name, id=self.id))
  36. file='OEBPF/navigation.ncx'; file_tools.write(self.get_path(file), template.get(file, name=self.name, id=self.id))
  37. def image_process(self, text):
  38. pattern = re.compile(r'https://sdjrzk-1251357229.cos.ap-guangzhou.myqcloud.com/exam/paper/\d+/images/\d+.[a-z]{3}')
  39. res=pattern.findall(text)
  40. if res:
  41. header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
  42. 'Chrome/63.0.3239.132 Safari/537.36'}
  43. for image in res:
  44. temp_file=image.replace('/images/','_').replace('https://sdjrzk-1251357229.cos.ap-guangzhou.myqcloud.com/exam/paper/','%s/OEBPF/images/' % self.temp_dir)
  45. pic=requests.get(image, headers=header)
  46. print(file_tools.write_b(temp_file, pic.content))
  47. return text.replace('/images/','_').replace('https://sdjrzk-1251357229.cos.ap-guangzhou.myqcloud.com/exam/paper/','../images/')
  48. def process(self, text):
  49. cwd=os.getcwd()
  50. text = self.image_process(text)
  51. file='OEBPF/content/s1.xhtml'; file_tools.write(self.get_path(file), template.get(file, title=self.name, data=text))
  52. z=zipfile.ZipFile(file_tools.get_ab_path("output/%s.epub"%self.name), 'w', zipfile.ZIP_STORED)
  53. os.chdir(file_tools.get_ab_path(self.temp_dir))
  54. for dirpath, dirnames, filenames in os.walk('./'):
  55. for filename in filenames:
  56. z.write(os.path.join(dirpath, filename))
  57. os.chdir(cwd)
  58. def clean(self):
  59. file_tools.rm(self.temp_dir)