convert_swf.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import os
  2. import sys
  3. import time
  4. sys.path.append(os.path.dirname(__file__) + "/../")
  5. from format_convert.convert_tree import _Document, _Image, _Page
  6. import base64
  7. import codecs
  8. import logging
  9. import re
  10. import traceback
  11. from format_convert import get_memory_info, timeout_decorator
  12. from format_convert.convert_image import picture2text
  13. from format_convert.swf.export import SVGExporter
  14. from format_convert.swf.movie import SWF
  15. from format_convert.utils import judge_error_code
  16. @get_memory_info.memory_decorator
  17. def swf2text(path, unique_type_dir):
  18. logging.info("into swf2text")
  19. try:
  20. try:
  21. with open(path, 'rb') as f:
  22. swf_file = SWF(f)
  23. svg_exporter = SVGExporter()
  24. svg = swf_file.export(svg_exporter)
  25. swf_str = str(svg.getvalue(), encoding='utf-8')
  26. except Exception as e:
  27. logging.info("swf format error!")
  28. traceback.print_exc()
  29. return [-3]
  30. # 正则匹配图片的信息位置
  31. result0 = re.finditer('<image id=(.[^>]*)', swf_str)
  32. image_bytes_list = []
  33. i = 0
  34. image_path_prefix = path.split(".")[-2] + "_" + path.split(".")[-1]
  35. image_path_list = []
  36. for r in result0:
  37. # 截取图片信息所在位置
  38. swf_str0 = swf_str[r.span()[0]:r.span()[1] + 1]
  39. # 正则匹配得到图片的base64编码
  40. result1 = re.search('xlink:href="data:(.[^>]*)', swf_str0)
  41. swf_str1 = swf_str0[result1.span()[0]:result1.span()[1]]
  42. reg1_prefix = 'b\''
  43. result1 = re.search(reg1_prefix + '(.[^\']*)', swf_str1)
  44. swf_str1 = swf_str1[result1.span()[0] + len(reg1_prefix):result1.span()[1]]
  45. # base64_str -> base64_bytes -> no "\\" base64_bytes -> bytes -> image
  46. base64_bytes_with_double = bytes(swf_str1, "utf-8")
  47. base64_bytes = codecs.escape_decode(base64_bytes_with_double, "hex-escape")[0]
  48. image_bytes = base64.b64decode(base64_bytes)
  49. image_bytes_list.append(image_bytes)
  50. image_path = image_path_prefix + "_page_" + str(i) + ".png"
  51. with open(image_path, 'wb') as f:
  52. f.write(image_bytes)
  53. image_path_list.append(image_path)
  54. # 正则匹配得到图片的宽高
  55. # reg2_prefix = 'width="'
  56. # result2 = re.search(reg2_prefix + '(\d+)', swf_str0)
  57. # swf_str2 = swf_str0[result2.span()[0]+len(reg2_prefix):result2.span()[1]]
  58. # width = swf_str2
  59. # reg2_prefix = 'height="'
  60. # result2 = re.search(reg2_prefix + '(\d+)', swf_str0)
  61. # swf_str2 = swf_str0[result2.span()[0]+len(reg2_prefix):result2.span()[1]]
  62. # height = swf_str2
  63. i += 1
  64. text_list = []
  65. for image_path in image_path_list:
  66. text = picture2text(image_path)
  67. if judge_error_code(text, code=[-3]):
  68. continue
  69. if judge_error_code(text):
  70. return text
  71. text = text[0]
  72. text_list.append(text)
  73. text = ""
  74. for t in text_list:
  75. text += t
  76. return [text]
  77. except Exception as e:
  78. logging.info("swf2text error!")
  79. print("swf2text", traceback.print_exc())
  80. return [-1]
  81. class SwfConvert:
  82. def __init__(self, path, unique_type_dir):
  83. self._doc = _Document(path)
  84. self.path = path
  85. self.unique_type_dir = unique_type_dir
  86. def init_package(self):
  87. try:
  88. with open(self.path, 'rb') as f:
  89. swf_file = SWF(f)
  90. svg_exporter = SVGExporter()
  91. svg = swf_file.export(svg_exporter)
  92. self.swf_str = str(svg.getvalue(), encoding='utf-8')
  93. except:
  94. logging.info("cannot open swf!")
  95. traceback.print_exc()
  96. self._doc.error_code = [-3]
  97. def convert(self):
  98. self.init_package()
  99. if self._doc.error_code is not None:
  100. return
  101. self._page = _Page(None, 0)
  102. # 正则匹配图片的信息位置
  103. result0 = re.finditer('<image id=(.[^>]*)', self.swf_str)
  104. image_no = 0
  105. image_path_prefix = self.path.split(".")[-2] + "_" + self.path.split(".")[-1]
  106. for r in result0:
  107. # 截取图片信息所在位置
  108. swf_str0 = self.swf_str[r.span()[0]:r.span()[1] + 1]
  109. # 正则匹配得到图片的base64编码
  110. result1 = re.search('xlink:href="data:(.[^>]*)', swf_str0)
  111. swf_str1 = swf_str0[result1.span()[0]:result1.span()[1]]
  112. reg1_prefix = 'b\''
  113. result1 = re.search(reg1_prefix + '(.[^\']*)', swf_str1)
  114. swf_str1 = swf_str1[result1.span()[0] + len(reg1_prefix):result1.span()[1]]
  115. # base64_str -> base64_bytes -> no "\\" base64_bytes -> bytes -> image
  116. base64_bytes_with_double = bytes(swf_str1, "utf-8")
  117. base64_bytes = codecs.escape_decode(base64_bytes_with_double, "hex-escape")[0]
  118. image_bytes = base64.b64decode(base64_bytes)
  119. image_path = image_path_prefix + "_page_" + str(image_no) + ".png"
  120. _image = _Image(image_bytes, image_path)
  121. _image.y = image_no
  122. self._page.add_child(_image)
  123. image_no += 1
  124. self._doc.add_child(self._page)
  125. def get_html(self):
  126. try:
  127. self.convert()
  128. except:
  129. traceback.print_exc()
  130. self._doc.error_code = [-1]
  131. if self._doc.error_code is not None:
  132. return self._doc.error_code
  133. return self._doc.get_html()