11import json
22import time
33
4- from const import *
5- from custom_params import CustomTranscodingParams , Query
6- from utils import is_json , rm_key_if_null
4+ from . import const as constants
5+ from ._compat import string_types
6+ from .custom_params import CustomTranscodingParams , Query
7+ from .utils import is_json , rm_key_if_null
78
89
910class Task (object ):
@@ -19,19 +20,20 @@ def __init__(self, access_token, connect, debug=False, **kwargs):
1920 self ._debug = debug
2021 self .message = ''
2122 self .error = None
22- self .repeat = kwargs .get ('repeats' ) if kwargs .get ('repeats' ) else REPEAT
23+ self .repeat = (
24+ kwargs .get ('repeats' ) if kwargs .get ('repeats' ) else constants .REPEAT
25+ )
2326 self ._create_task (1 )
2427
2528 def start (self , profiles , video_url , ** kwargs ):
26- """Creating task and starting encode
27-
28- :param profiles: String or List object. Profile uuid
29- :param transfer_method: String. Transfer method uuid
30- :param video_url: String. Url of source video
31- :param payload: String.
32- :return: None
33-
34- """
29+ """Creating task and starting encode.
30+
31+ :param profiles: String or List object. Profile uuid
32+ :param transfer_method: String. Transfer method uuid
33+ :param video_url: String. Url of source video
34+ :param payload: String.
35+ :return: None
36+ """
3537 if not self .error :
3638 # self._create_task(1)
3739 data = self ._prepare_data (profiles , video_url , ** kwargs )
@@ -42,11 +44,11 @@ def start(self, profiles, video_url, **kwargs):
4244 def custom_start (self , data , ** kwargs ):
4345 """Creating task and starting encode
4446
45- :param query: JSON object for query param. For examples: https://docs.qencode.com
46- : param payload: String.
47- :return: None
48-
49- """
47+ :param query:
48+ JSON object for query param. For examples: https://docs.qencode.com
49+ :param payload: String.
50+ :return: None
51+ """
5052 if data is None :
5153 self .error = True
5254 self .message = 'Params is required'
@@ -75,20 +77,20 @@ def progress_changed(self, callback, *args, **kwargs):
7577 if status ['error' ]:
7678 return callback (status , * args , ** kwargs )
7779 callback (status , * args , ** kwargs )
78- if status .get ('status' ) in COMPLETED_STATUS :
80+ if status .get ('status' ) in constants . COMPLETED_STATUS :
7981 break
80- time .sleep (SLEEP_REGULAR )
82+ time .sleep (constants . SLEEP_REGULAR )
8183
8284 def task_completed (self , callback , * args , ** kwargs ):
8385 while 1 :
8486 status = self ._status ()
8587 if status ['error' ]:
8688 return callback (status , * args , ** kwargs )
87- if status .get ('status' ) in COMPLETED_STATUS :
89+ if status .get ('status' ) in constants . COMPLETED_STATUS :
8890 return callback (status , * args , ** kwargs )
89- if status .get ('status' ) in COMPLETED_STATUS :
91+ if status .get ('status' ) in constants . COMPLETED_STATUS :
9092 break
91- time .sleep (SLEEP_REGULAR )
93+ time .sleep (constants . SLEEP_REGULAR )
9294
9395 def _prepare_query (self , params ):
9496 if isinstance (params , CustomTranscodingParams ):
@@ -108,20 +110,19 @@ def _prepare_query(self, params):
108110 query = rm_key_if_null (params )
109111 return json .dumps (query )
110112
111- if isinstance (params , basestring ):
113+ if isinstance (params , string_types ):
112114 if is_json (params ):
113115 query = rm_key_if_null (params )
114116 return query
115117 else :
116118 self .error = True
119+ error_msg = "JSON is not well formatted"
117120 try :
118- self .message = "JSON is not well formatted: {0} Is not defined" .format (
119- params
120- )
121- except Exception as e :
121+ self .message = "{}: {} Is not defined" .format (error_msg , params )
122+ except Exception :
122123 pass
123124 finally :
124- self .message = "JSON is not well formatted"
125+ self .message = error_msg
125126
126127 def _prepare_data (self , profiles , video_url , ** kwargs ):
127128 data = dict (
@@ -157,8 +158,8 @@ def _create_task(self, count):
157158 self .message = res .get ('message' )
158159
159160 if self .error and self .error == 8 :
160- if count < REPEAT :
161- time .sleep (SLEEP_ERROR )
161+ if count < constants . REPEAT :
162+ time .sleep (constants . SLEEP_ERROR )
162163 self ._create_task (count + 1 )
163164
164165 def _start_encode (self , api_name , data ):
@@ -174,7 +175,7 @@ def _status(self):
174175 response = self .connect .post (self .status_url , dict (task_tokens = self .task_token ))
175176 status = None
176177
177- if response ['error' ] == ERROR_BAD_TOKENS :
178+ if response ['error' ] == constants . ERROR_BAD_TOKENS :
178179 raise ValueError ('Bad token: ' + str (self .task_token ))
179180
180181 if 'statuses' in response and self .task_token in response ['statuses' ]:
0 commit comments