|
@@ -2,6 +2,8 @@
|
|
|
from demeter.core import *
|
|
|
from datetime import *
|
|
|
import uuid
|
|
|
+import os
|
|
|
+import os.path
|
|
|
import requests
|
|
|
import shutil
|
|
|
|
|
@@ -82,6 +84,7 @@ class Convert(object):
|
|
|
|
|
|
info['local'] = local
|
|
|
info['pdf'] = filepath + '/' + filename + '.pdf'
|
|
|
+
|
|
|
info['html'] = filepath + '/' + filename + '.html'
|
|
|
info['url'] = info['html'].replace(Demeter.path + 'runtime', '')
|
|
|
info['path'] = filepath + '/'
|
|
@@ -93,7 +96,7 @@ class Convert(object):
|
|
|
self.download(file, local);
|
|
|
return info
|
|
|
|
|
|
- def download(file, local):
|
|
|
+ def download(self, file, local):
|
|
|
if 'http' in file:
|
|
|
r = requests.get(file, stream=True)
|
|
|
with open(local, 'wb') as up:
|
|
@@ -108,12 +111,91 @@ class Convert(object):
|
|
|
return True
|
|
|
return False
|
|
|
|
|
|
- def handle(self, file, key, ext, path, pdf):
|
|
|
- convert = 'libreoffice --invisible --convert-to pdf ' + file
|
|
|
+ def command(self, info):
|
|
|
+ convert = 'cd ' + info['path'] + ' && '
|
|
|
+ convert = convert + 'libreoffice --invisible --convert-to pdf ' + info['local']
|
|
|
convert = convert + ' && '
|
|
|
convert = convert + 'pdf2htmlEX --zoom 1.3 --no-drm 1 --split-pages 1 '
|
|
|
convert = convert + '--embed-css 1 --embed-javascript 0 --embed-image 0 --embed-font 1 '
|
|
|
- convert = convert + '--embed-external-font 0 --dest-dir '+path+' --page-filename %d.page ' + pdf
|
|
|
-
|
|
|
- print convert
|
|
|
- return
|
|
|
+ convert = convert + '--embed-external-font 0 --dest-dir '+info['path']+' --page-filename %d.page ' + info['pdf']
|
|
|
+
|
|
|
+ return convert
|
|
|
+
|
|
|
+
|
|
|
+ def total(self, path):
|
|
|
+ page = 0
|
|
|
+ for parentdir,dirname,filenames in os.walk(path):
|
|
|
+ for filename in filenames:
|
|
|
+ if os.path.splitext(filename)[1]=='.page':
|
|
|
+ page = page + 1
|
|
|
+ return page
|
|
|
+
|
|
|
+ def handle(self, id):
|
|
|
+ model = Demeter.model('convert')
|
|
|
+ model.id = id
|
|
|
+ info = model.select(type='fetchone')
|
|
|
+
|
|
|
+ if not info:
|
|
|
+ return
|
|
|
+ status = True
|
|
|
+ if info['status'] == 1 or info['status'] == 4:
|
|
|
+ status = False
|
|
|
+
|
|
|
+ if info and status == False:
|
|
|
+ model.id = id
|
|
|
+ update = {}
|
|
|
+ update['status'] = 2
|
|
|
+
|
|
|
+
|
|
|
+ if not File.exists(info['local']):
|
|
|
+ self.download(info['file'], info['local'])
|
|
|
+
|
|
|
+ if not File.exists(info['html']):
|
|
|
+ handle = self.command(info)
|
|
|
+ print handle
|
|
|
+ return
|
|
|
+ Shell.popen(handle)
|
|
|
+ if File.exists(info['html']):
|
|
|
+
|
|
|
+ self.string_switch(info['html'], "taste", "tasting")
|
|
|
+
|
|
|
+ page = self.total(info['path'])
|
|
|
+ model.id = id
|
|
|
+ update = {}
|
|
|
+ update['page'] = page
|
|
|
+ update['status'] = 3
|
|
|
+ model.update(update)
|
|
|
+ return
|
|
|
+
|
|
|
+ model.id = id
|
|
|
+ update = {}
|
|
|
+ update['status'] = 4
|
|
|
+ model.update(update)
|
|
|
+
|
|
|
+ def string_switch(self, x,y,z,s=1):
|
|
|
+ with open(x, "r", encoding="utf-8") as f:
|
|
|
+
|
|
|
+ lines = f.readlines()
|
|
|
+
|
|
|
+ with open(x, "w", encoding="utf-8") as f_w:
|
|
|
+
|
|
|
+ n = 0
|
|
|
+
|
|
|
+ if s == 1:
|
|
|
+ for line in lines:
|
|
|
+ if y in line:
|
|
|
+ line = line.replace(y,z)
|
|
|
+ f_w.write(line)
|
|
|
+ n += 1
|
|
|
+ break
|
|
|
+ f_w.write(line)
|
|
|
+ n += 1
|
|
|
+
|
|
|
+ for i in range(n,len(lines)):
|
|
|
+ f_w.write(lines[i])
|
|
|
+
|
|
|
+ elif s == 'g':
|
|
|
+ for line in lines:
|
|
|
+ if y in line:
|
|
|
+ line = line.replace(y,z)
|
|
|
+ f_w.write(line)
|