1- from . custom_params import Query
1+ from . custom_params import Query , CustomTranscodingParams
22from . const import *
33import time
4+ import json
5+ from . utils import is_json , rm_key_if_null
46
57
68
@@ -17,6 +19,74 @@ def __init__(self, access_token, connect, debug=False, **kwargs):
1719
1820
1921
22+ def start (self , profiles , video_url , ** kwargs ):
23+ """Creating task and starting encode
24+
25+ :param profiles: String or List object. Profile uuid
26+ :param transfer_method: String. Transfer method uuid
27+ :param video_url: String. Url of source video
28+ :param payload: String.
29+ :return: None
30+
31+ """
32+ if not self .error :
33+ self ._create_task (1 )
34+ data = self ._prepare_data (profiles , video_url , ** kwargs )
35+
36+ if not self .error and self .task_token :
37+ self ._start_encode ('start_encode' , data )
38+
39+
40+ def custom_start (self , data , ** kwargs ):
41+ """Creating task and starting encode
42+
43+ :param query: JSON object for query param. For examples: https://docs.qencode.com
44+ :param payload: String.
45+ :return: None
46+
47+ """
48+ if data is None :
49+ self .error = True
50+ self .message = 'Params is required'
51+
52+ if not self .error :
53+ self ._create_task (1 )
54+
55+ if not self .error :
56+ data = self ._prepare_data_custom (self ._prepare_query (data ), ** kwargs )
57+
58+ if not self .error and self .task_token :
59+ self ._start_encode ('start_encode2' , data )
60+
61+
62+ def status (self ):
63+ return self ._status ()
64+
65+ def main_status (self ):
66+ return self ._status2 ()
67+
68+ def progress_changed (self , callback , * args , ** kwargs ):
69+ while 1 :
70+ status = self ._status ()
71+ if status ['error' ]:
72+ return callback (status , * args , ** kwargs )
73+ callback (status , * args , ** kwargs )
74+ if status .get ('status' ) in COMPLETED_STATUS :
75+ break
76+ time .sleep (SLEEP_REGULAR )
77+
78+
79+ def task_completed (self , callback , * args , ** kwargs ):
80+ while 1 :
81+ status = self ._status ()
82+ if status ['error' ]:
83+ return callback (status , * args , ** kwargs )
84+ if status .get ('status' ) in COMPLETED_STATUS :
85+ return callback (status , * args , ** kwargs )
86+ if status .get ('status' ) in COMPLETED_STATUS :
87+ break
88+ time .sleep (SLEEP_REGULAR )
89+
2090 def _prepare_data (self , profiles , video_url , ** kwargs ):
2191 data = dict (
2292 task_token = self .task_token ,
@@ -27,7 +97,30 @@ def _prepare_data(self, profiles, video_url, **kwargs):
2797 data .update (kwargs )
2898 return data
2999
30- def _prepare_data2 (self , query_json , ** kwargs ):
100+ def _prepare_query (self , params ):
101+ if isinstance (params , CustomTranscodingParams ):
102+ query_obj = Query ()
103+ query_obj .params = params
104+ query_obj .validate_params ()
105+ if query_obj .error :
106+ self .error = query_obj .error
107+ self .message = query_obj .message
108+ query_obj .prepare_params ()
109+ if query_obj .error :
110+ self .error = query_obj .error
111+ self .message = query_obj .message
112+ return query_obj .query
113+
114+ if isinstance (params , dict ):
115+ query = rm_key_if_null (params )
116+ return json .dumps (query )
117+
118+ if isinstance (params , str ):
119+ if is_json (params ):
120+ query = rm_key_if_null (params )
121+ return query
122+
123+ def _prepare_data_custom (self , query_json , ** kwargs ):
31124 data = dict (
32125 task_token = self .task_token ,
33126 query = query_json
@@ -82,90 +175,3 @@ def _status2(self):
82175 else :
83176 return response
84177
85-
86-
87-
88- def start (self , profiles , video_url , ** kwargs ):
89- """Creating task and starting encode
90-
91- :param profiles: String or List object. Profile uuid
92- :param transfer_method: String. Transfer method uuid
93- :param video_url: String. Url of source video
94- :param payload: String.
95- :return: None
96-
97- """
98- if not self .error :
99- self ._create_task (1 )
100- data = self ._prepare_data (profiles , video_url , ** kwargs )
101-
102- if not self .error and self .task_token :
103- self ._start_encode ('start_encode' , data )
104-
105-
106- def custom_start (self , params , ** kwargs ):
107- """Creating task and starting encode
108-
109- :param query: JSON object for query param. For examples: https://docs.qencode.com
110- :param payload: String.
111- :return: None
112-
113- """
114- query_obj = Query ()
115- query_obj .params = params
116- query_obj .validate_params ()
117- if query_obj .error :
118- self .error = query_obj .error
119- self .message = query_obj .message
120- return
121-
122- query_obj .prepare_params ()
123- if query_obj .error :
124- self .error = query_obj .error
125- self .message = query_obj .message
126-
127- if not self .error :
128- self ._create_task (1 )
129- data = self ._prepare_data2 (query_obj .query , ** kwargs )
130-
131- if not self .error and self .task_token :
132- self ._start_encode ('start_encode2' , data )
133-
134-
135- def status (self ):
136- """Getting status of encode from master-server.
137- real time updating.
138-
139- :return: JSON object:
140- {
141- 'status' : 'encoding', 'percent': '50.90' 'error': 0 ...
142- }
143-
144- """
145- return self ._status ()
146-
147- def main_status (self ):
148- return self ._status2 ()
149-
150- def progress_changed (self , callback , * args , ** kwargs ):
151- while 1 :
152- status = self ._status ()
153- if status ['error' ]:
154- return callback (status , * args , ** kwargs )
155- callback (status , * args , ** kwargs )
156- if status .get ('status' ) in COMPLETED_STATUS :
157- break
158- time .sleep (SLEEP_REGULAR )
159-
160-
161- def task_completed (self , callback , * args , ** kwargs ):
162- while 1 :
163- status = self ._status ()
164- if status ['error' ]:
165- return callback (status , * args , ** kwargs )
166- if status .get ('status' ) in COMPLETED_STATUS :
167- return callback (status , * args , ** kwargs )
168- if status .get ('status' ) in COMPLETED_STATUS :
169- break
170- time .sleep (SLEEP_REGULAR )
171-
0 commit comments