convert.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # -*- coding: utf-8 -*-
  2. from demeter.core import *
  3. from datetime import *
  4. import uuid
  5. import os
  6. import os.path
  7. class Convert(object):
  8. def getKey(self, site_key, file):
  9. return Demeter.md5(str(site_key) + '_' + file)
  10. def get(self, site, site_key, file):
  11. convert = Demeter.model('convert')
  12. convert.site_id = site
  13. convert.key = self.getKey(site_key, file)
  14. data = convert.select(type='fetchone')
  15. return data
  16. def update(self, site, site_key, file):
  17. info = self.getFile(site_key, file)
  18. convert = Demeter.model('convert')
  19. convert.site_id = site
  20. convert.key = info['key']
  21. data = convert.select(type='fetchone')
  22. if not data:
  23. convert.site_id = site
  24. convert.file = info['file']
  25. convert.key = info['key']
  26. convert.name = info['name']
  27. convert.ext = info['ext']
  28. convert.local = info['local']
  29. convert.path = info['path']
  30. convert.pdf = info['pdf']
  31. convert.html = info['html']
  32. convert.url = info['url']
  33. id = convert.insert()
  34. info['status'] = 1
  35. info['id'] = id
  36. else:
  37. info['id'] = data['id']
  38. info['status'] = data['status']
  39. return info
  40. def getFile(self, site_key, file):
  41. info = {}
  42. (filepath,temp) = os.path.split(file)
  43. (filename,extension) = os.path.splitext(temp)
  44. info['file'] = file
  45. info['key'] = self.getKey(site_key, file)
  46. info['ext'] = extension
  47. info['name'] = filename
  48. info = self.getLocalFile(site_key, file, info)
  49. return info
  50. def getLocalFile(self, site_key, file, info):
  51. day = str(date.today())
  52. day = day.split('-')
  53. #filename = Demeter.md5(str(uuid.uuid5(uuid.uuid1(), info['key'])))
  54. filename = info['key']
  55. filepath = str(site_key) + '/' + day[0] + '/' + day[1] + '/' + day[2]
  56. filepath = File.mkdirs(os.path.join(Demeter.path, 'runtime','upload', filepath)) + '/' + filename
  57. local = filepath + info['ext']
  58. info['local'] = local
  59. info['pdf'] = filepath + '/' + filename + '.pdf'
  60. # 这里要增加权限控制 html不能直接访问
  61. info['html'] = filepath + '/' + filename + '.html'
  62. info['url'] = info['html'].replace(Demeter.path + 'runtime', '')
  63. info['path'] = filepath + '/'
  64. return info
  65. if File.exists(local):
  66. return info
  67. else:
  68. self.download(file, local);
  69. return info
  70. def download(self, file, local):
  71. if 'http' in file:
  72. import requests
  73. r = requests.get(file, stream=True)
  74. with open(local, 'wb') as up:
  75. for chunk in r.iter_content(chunk_size=1024):
  76. if chunk:
  77. up.write(chunk)
  78. else:
  79. import shutil
  80. shutil.copyfile(file, local)
  81. if File.exists(local):
  82. return True
  83. return False
  84. def command(self, info):
  85. convert = 'cd ' + info['path'] + ' && '
  86. convert = convert + 'libreoffice --invisible --convert-to pdf ' + info['local']
  87. convert = convert + ' && '
  88. convert = convert + 'pdf2htmlEX --zoom 1.3 --no-drm 1 --split-pages 1 '
  89. convert = convert + '--embed-css 1 --embed-javascript 0 --embed-image 0 --embed-font 1 --process-outline 0 '
  90. convert = convert + '--embed-external-font 0 --dest-dir '+info['path']+' --page-filename %d.page ' + info['pdf']
  91. return convert
  92. def total(self, path):
  93. page = 0
  94. for parentdir,dirname,filenames in os.walk(path):
  95. for filename in filenames:
  96. if os.path.splitext(filename)[1]=='.page':
  97. page = page + 1
  98. return page
  99. def handle(self, id):
  100. model = Demeter.model('convert')
  101. model.id = id
  102. info = model.select(type='fetchone')
  103. if not info:
  104. return
  105. status = True
  106. if info['status'] == 1 or info['status'] == 4:
  107. status = False
  108. if info and status == False:
  109. model.id = id
  110. update = {}
  111. update['status'] = 2
  112. model.update(update)
  113. if not File.exists(info['local']):
  114. self.download(info['file'], info['local'])
  115. if not File.exists(info['html']):
  116. handle = self.command(info)
  117. Shell.popen(handle)
  118. if File.exists(info['html']):
  119. #self.string_switch(info['html'], "taste", "tasting")
  120. # 获取有多少页
  121. page = self.total(info['path'])
  122. model.id = id
  123. update = {}
  124. update['page'] = page
  125. update['status'] = 3
  126. model.update(update)
  127. return
  128. model.id = id
  129. update = {}
  130. update['status'] = 4
  131. model.update(update)
  132. def string_switch(self, x,y,z,s=1):
  133. with open(x, "r", encoding="utf-8") as f:
  134. #readlines以列表的形式将文件读出
  135. lines = f.readlines()
  136. with open(x, "w", encoding="utf-8") as f_w:
  137. #定义一个数字,用来记录在读取文件时在列表中的位置
  138. n = 0
  139. #默认选项,只替换第一次匹配到的行中的字符串
  140. if s == 1:
  141. for line in lines:
  142. if y in line:
  143. line = line.replace(y,z)
  144. f_w.write(line)
  145. n += 1
  146. break
  147. f_w.write(line)
  148. n += 1
  149. #将剩余的文本内容继续输出
  150. for i in range(n,len(lines)):
  151. f_w.write(lines[i])
  152. #全局匹配替换
  153. elif s == 'g':
  154. for line in lines:
  155. if y in line:
  156. line = line.replace(y,z)
  157. f_w.write(line)