-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtask.py
More file actions
65 lines (48 loc) · 2.01 KB
/
task.py
File metadata and controls
65 lines (48 loc) · 2.01 KB
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
65
import convertapi
from convertapi import file_param, format_detector, utils
from .result import Result
DEFAULT_URL_FORMAT = 'web'
class Task:
def __init__(self, from_format, to_format, params, timeout = None, is_async = False):
self.from_format = from_format
self.to_format = to_format
self.params = params
self.timeout = timeout or convertapi.conversion_timeout
self.is_async = is_async
self.default_params = {
'Timeout': self.timeout,
'StoreFile': True,
}
def run(self):
params = self.__normalize_params()
from_format = self.from_format or self.__detect_format()
timeout = self.timeout + convertapi.conversion_timeout_delta if self.timeout else None
base_path = 'convert' if not self.is_async else 'async/convert'
path = "%s/%s/to/%s" % (base_path, from_format, self.to_format)
if 'converter' in params:
path += "/converter/%s" % (params['converter'])
response = convertapi.client.post(path, params, timeout = timeout)
return Result(response)
def __normalize_params(self):
params = {}
for k, v in self.params.items():
if k == 'File':
params[k] = file_param.build(v)
elif k == 'Files':
results = utils.map_in_parallel(file_param.build, v, convertapi.max_parallel_uploads)
for idx, val in enumerate(results):
key = '%s[%i]' % (k, idx)
params[key] = val
else:
params[k] = v
params.update(self.default_params)
return params
def __detect_format(self):
if str(self.to_format).lower() == 'zip':
return 'any'
if 'Url' in self.params:
return DEFAULT_URL_FORMAT
if 'File' in self.params:
return format_detector.detect(self.params['File'])
if 'Files' in self.params:
return format_detector.detect(self.params['Files'][0])