@@ -9,6 +9,7 @@ class Task(object):
9
9
def __init__ (self , access_token , connect , debug = False , ** kwargs ):
10
10
self .connect = connect
11
11
self .status_url = None
12
+ self .main_status_url = '{0}/{1}/status' .format (self .connect .url , self .connect .version )
12
13
self .task_token = None
13
14
self .upload_url = None
14
15
self .access_token = access_token
@@ -60,9 +61,6 @@ def custom_start(self, data, **kwargs):
60
61
if not self .error and self .task_token :
61
62
self ._start_encode ('start_encode2' , data )
62
63
63
-
64
-
65
-
66
64
def status (self ):
67
65
return self ._status ()
68
66
@@ -121,7 +119,6 @@ def _prepare_query(self, params):
121
119
finally :
122
120
self .message = "JSON is not well formatted"
123
121
124
-
125
122
def _prepare_data (self , profiles , video_url , ** kwargs ):
126
123
data = dict (
127
124
task_token = self .task_token ,
@@ -161,38 +158,34 @@ def _create_task(self, count):
161
158
time .sleep (SLEEP_ERROR )
162
159
self ._create_task (count + 1 )
163
160
161
+
164
162
def _start_encode (self , api_name , data ):
165
163
res = self .connect .request (api_name , data )
166
- if not res ['error' ]:
167
- self .status_url = res . get ( 'status_url' )
164
+ if not res ['error' ] and res . get ( 'status_url' ) :
165
+ self .status_url = res [ 'status_url' ]
168
166
else :
169
- self .status_url = '{0}/{1}/status' . format ( self .connect . url , self . connect . version )
170
- self .error = res [ 'error' ]
167
+ self .status_url = self .main_status_url
168
+ self .error = res . get ( 'error' )
171
169
self .message = res .get ('message' )
172
170
173
171
def _status (self ):
174
172
response = self .connect .post (self .status_url , dict (task_tokens = self .task_token ))
175
- if not response ['error' ]:
173
+ status = None
174
+
175
+ if response ['error' ] == ERROR_BAD_TOKENS :
176
+ raise ValueError ('Bad token: ' + str (self .task_token ))
177
+
178
+ if 'statuses' in response and self .task_token in response ['statuses' ]:
179
+ status = response ['statuses' ][self .task_token ]
180
+
181
+ if not status and self .status_url != self .main_status_url :
182
+ self .status_url = self .main_status_url
183
+ response = self .connect .post (self .status_url , dict (task_tokens = self .task_token ))
184
+ if 'statuses' in response and self .task_token in response ['statuses' ]:
176
185
status = response ['statuses' ][self .task_token ]
177
- if not status :
178
- status = self ._status2 ()
179
- if status .get ('status_url' ):
180
- self .status_url = status .get ('status_url' )
181
- return status
182
- else :
183
- status = self ._status2 ()
184
- return status
185
-
186
- def _status2 (self ):
187
- response = self .connect .request ('status' , {'task_tokens[]' : self .task_token })
188
- if not response ['error' ]:
189
- res = response ['statuses' ][self .task_token ]
190
- if res :
191
- if res .get ('status_url' ):
192
- self .status_url = res .get ('status_url' )
193
- return res
194
- else :
195
- return dict (error = True , message = 'Error getting status' )
196
- else :
197
- return response
186
+
187
+ if status and 'status_url' in status :
188
+ self .status_url = status ['status_url' ]
189
+
190
+ return status
198
191
0 commit comments