-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCloudConvert.py
256 lines (189 loc) · 6.46 KB
/
CloudConvert.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import requests
import time
class CloudConvert():
"""
Low level interface to the CloudConvert service
"""
@staticmethod
def start(inputformat, outputformat, apikey):
"""
Inits the process in the remote server
"""
url = (
"https://api.cloudconvert.org/process?"
"inputformat={inputf}&outputformat={outputf}&apikey={api}"
).format(
inputf=inputformat,
outputf=outputformat,
api=apikey)
return requests.get(url).json()
@staticmethod
def upload(fname, outformat, process_url, options=None):
"""
Uploads a file to be converted
"""
if options is None:
options = {} # TODO
with open(fname, "rb") as f:
requests.post(process_url,
data={
"outputformat": outformat
},
files={"file": f})
@staticmethod
def status(process_url):
"""
Checks the conversion status of a process
"""
return requests.get(process_url).json()
@staticmethod
def download(process_url):
"""
Returns a file-like object containing the file
"""
url = "https:" + CloudConvert.status(process_url)["output"]["url"]
return requests.get(url, stream=True).raw
@staticmethod
def cancel(process_url):
"""
Cancels the conversion methon at any point.
Currently there is no way of resuming
"""
url = (
"{purl}/cancel"
).format(purl=process_url)
requests.get(url)
@staticmethod
def delete(process_url):
"""
Deletes files of a conversion process
"""
url = (
"{purl}/delete"
).format(purl=process_url)
requests.get(url)
@staticmethod
def list(apikey):
"""
Returns the conversion history of the supplied apikey.
"""
url = (
"https://api.cloudconvert.org/processes?apikey={api}"
).format(api=apikey)
return requests.get(url).json()
@staticmethod
def conversion_types(inputformat=None, outputformat=None):
"""
Returns a dict with all te possible conversions and
conversion specific options.
Arguments:
inputformat(str) -> input format to lookup [optional]
outputformat(str) -> outpu format to lookup [optional]
"""
kwargs = {"inputformat": inputformat,
"outputformat": outputformat}
url = "https://api.cloudconvert.org/conversiontypes"
if inputformat or outputformat:
toappend = [param + "=" + kwargs[param]
for param in kwargs
if kwargs[param] is not None]
url += "?" + "&".join(toappend)
return requests.get(url).json()
class ConversionProcessException(Exception):
pass
class ConversionProcess():
def __init__(self, apikey):
self.apikey = apikey
self.url = None
self.fromfile = None
self.fromformat = None
self.tofile = None
self.toformat = None
def _get_format(self, f):
return f.split(".")[-1]
def init(self, fromfile, tofile, fromformat=None, toformat=None):
"""
Prepares the conversion
"""
self.fromfile = fromfile
self.tofile = tofile
# If this doesn't get resolved right,
# user has to provide the correct ones.
self.fromformat = self._get_format(fromfile) if fromformat is None else fromformat
self.toformat = self._get_format(tofile) if toformat is None else toformat
j = CloudConvert.start(self.fromformat, self.toformat, self.apikey)
if 'error' in j:
raise ConversionProcessException(j["error"])
self.url = "https:" + j["url"]
return j["id"] # pid
def is_possible(self):
"""
Checks if there is a conversion type between the two formats.
Returns boolean.
"""
if self.fromformat is None or self.toformat is None:
return False
else:
return bool(
CloudConvert.conversion_types(
inputformat=self.fromformat,
outputformat=self.toformat
)
)
def start(self):
"""
Uploads the file hence starting the conversion process
"""
CloudConvert.upload(
self.fromfile, self.fromformat, self.url)
def status(self):
"""
Returns the status of the process
"""
# TODO: Make it more beautiful, not just raw json response
return CloudConvert.status(self.url)
def cancel(self):
"""
Cancels the process. Currently there is no way of resuming.
"""
CloudConvert.cancel(self.url)
def delete(self):
"""
Deletes the files from the current process.
Note: files will get automatically deleted after a fixed period of time
available trough status()
Note: if the process is alredy running, it's first cancelled
"""
CloudConvert.delete(self.url)
def wait_for_completion(self, check_interval=1):
"""
This blocks until the process status["step"] changes to "finished"
when returns True or "error", in which case, returns False.
If there is an error other than the conversion failing,
it will raise 'ConversionProcessException'
Arguments:
check_interval(int) -> seconds to wait between each check.
"""
while True:
time.sleep(check_interval)
status = self.status()
if status.get("error", False):
raise ConversionProcessException(status["error"])
step = status["step"]
if step == "finished":
return True
elif step == "error":
return False
def download(self):
"""
Returns a file-like object with the output file, for download.
"""
# File-like object
return CloudConvert.download(self.url)
def save(self):
"""
Saves the output with the designated filename and extension
"""
download = self.download()
with open(self.tofile, "wb") as f: # Important to set mode to wb
f.write(download.read())