convert.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. File.mkdir(info['path'])
  86. convert = 'cd ' + info['path'] + ' && '
  87. if info['ext'] != '.pdf':
  88. convert = convert + 'libreoffice --invisible --convert-to pdf ' + info['local']
  89. convert = convert + ' && '
  90. else:
  91. info['pdf'] = info['local']
  92. convert = convert + 'pdf2htmlEX --zoom 1.3 --no-drm 1 --split-pages 1 '
  93. convert = convert + '--embed-css 1 --embed-javascript 0 --embed-image 0 --embed-font 1 --process-outline 0 '
  94. convert = convert + '--embed-external-font 0 --dest-dir '+info['path']+' --page-filename %d.page ' + info['pdf']
  95. return convert
  96. def total(self, path):
  97. page = 0
  98. for parentdir,dirname,filenames in os.walk(path):
  99. for filename in filenames:
  100. if os.path.splitext(filename)[1]=='.page':
  101. page = page + 1
  102. return page
  103. def handle(self, id):
  104. model = Demeter.model('convert')
  105. model.id = id
  106. info = model.select(type='fetchone')
  107. if not info:
  108. return
  109. status = True
  110. if info['status'] == 1 or info['status'] == 4:
  111. status = False
  112. if info and status == False:
  113. model.id = id
  114. update = {}
  115. update['status'] = 2
  116. model.update(update)
  117. if not File.exists(info['local']):
  118. self.download(info['file'], info['local'])
  119. if not File.exists(info['html']):
  120. handle = self.command(info)
  121. Shell.popen(handle)
  122. if File.exists(info['html']):
  123. #self.string_switch(info['html'], "taste", "tasting")
  124. # 获取有多少页
  125. page = self.total(info['path'])
  126. model.id = id
  127. update = {}
  128. update['page'] = page
  129. update['status'] = 3
  130. model.update(update)
  131. return
  132. model.id = id
  133. update = {}
  134. update['status'] = 4
  135. model.update(update)
  136. def string_switch(self, x,y,z,s=1):
  137. with open(x, "r", encoding="utf-8") as f:
  138. #readlines以列表的形式将文件读出
  139. lines = f.readlines()
  140. with open(x, "w", encoding="utf-8") as f_w:
  141. #定义一个数字,用来记录在读取文件时在列表中的位置
  142. n = 0
  143. #默认选项,只替换第一次匹配到的行中的字符串
  144. if s == 1:
  145. for line in lines:
  146. if y in line:
  147. line = line.replace(y,z)
  148. f_w.write(line)
  149. n += 1
  150. break
  151. f_w.write(line)
  152. n += 1
  153. #将剩余的文本内容继续输出
  154. for i in range(n,len(lines)):
  155. f_w.write(lines[i])
  156. #全局匹配替换
  157. elif s == 'g':
  158. for line in lines:
  159. if y in line:
  160. line = line.replace(y,z)
  161. f_w.write(line)