-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdf2htmlEX
executable file
·64 lines (52 loc) · 1.63 KB
/
pdf2htmlEX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python3
import requests
import sys
import json
import zipfile
import tempfile
import subprocess
# api_urls = ['http://localhost:5000/pdf2htmlEX','http://lambdax.cn:5000/pdf2htmlEX','http://lambdax.cn:5001/pdf2htmlEX']
api_urls = ['http://localhost:5000/pdf2htmlEX','http://lambdax.cn:5001/pdf2htmlEX']
BIN = "/usr/local/bin/pdf2htmlEX"
def remote_convert(src, dst, argv):
for url in api_urls:
try:
files = {'file': open(src, 'rb')}
data= {'command':json.dumps(' '.join(argv))}
req = requests.post(url, files=files, data=data)
except requests.exceptions.ConnectionError:
continue
if req.status_code == 200:
fp = tempfile.TemporaryFile()
fp.write(req.content)
fp.seek(0)
with zipfile.ZipFile(fp, 'r') as myzip:
myzip.extractall(dst)
return
else:
print(req.text)
else:
print('remote api is not available!')
# raise Exception('remote api is not available! %s'%req.text)
self_convert()
def main():
file_desc = sys.argv[-1]
argv = sys.argv.copy()
if '--dest-dir' in argv:
idx = argv.index('--dest-dir')
dst = argv[idx+1] if not argv[idx+1].startswith('-') else None
del(argv[idx])
del(argv[idx])
else:
dst = './'
src = argv[-1] if not argv[-1].startswith('-') else None
assert(src)
del(argv[0])
del(argv[-1])
remote_convert(src, dst, argv)
def self_convert():
cmd = [BIN,]
cmd.extend(sys.argv[1:])
subprocess.check_output(cmd)
if __name__ == '__main__':
main()