@@ -62,8 +62,8 @@ class Scene extends Base {
6262}
6363
6464class Movie extends Base {
65- private $ api_url = 'https://api.json2video.com/v1 /movies ' ;
66- protected $ properties = ['comment ' , 'project ' , 'width ' , 'height ' , 'resolution ' , 'quality ' , 'fps ' , 'cache ' ];
65+ private $ api_url = 'https://api.json2video.com/v2 /movies ' ;
66+ protected $ properties = ['comment ' , 'draft ' , 'width ' , 'height ' , 'resolution ' , 'quality ' , 'fps ' , 'cache ' ];
6767
6868 private $ apikey = null ;
6969
@@ -123,7 +123,12 @@ public function render() {
123123 ]);
124124
125125 if ($ response ) {
126- if ($ response ['status ' ]=='200 ' ) return json_decode ($ response ['response ' ], true );
126+ if ($ response ['status ' ]=='200 ' ) {
127+ $ render = json_decode ($ response ['response ' ], true );
128+ if ($ render ['success ' ]??false && !empty ($ render ['project ' ])) $ this ->object ['project ' ] = $ render ['project ' ];
129+ else throw new \Exception ("Render didn't return a project ID " );
130+ return $ render ;
131+ }
127132 elseif ($ response ['status ' ]=='400 ' ) {
128133 $ api_response = json_decode ($ response ['response ' ], true );
129134 throw new \Exception ('JSON Syntax error: ' . ($ api_response ['message ' ] ?? 'Unknown error ' ));
@@ -138,12 +143,14 @@ public function render() {
138143 return false ;
139144 }
140145
141- public function getStatus () {
146+ public function getStatus ($ project =null ) {
147+
148+ if (!$ project ) $ project = $ this ->object ['project ' ] ?? null ;
142149
143150 if (empty ($ this ->apikey )) throw new \Exception ('Invalid API Key ' );
144- if (empty ( $ this -> object [ ' project ' ]) ) throw new \Exception ('Project ID not set ' );
151+ if (! $ project ) throw new \Exception ('Project ID not set ' );
145152
146- $ url = $ this ->api_url . '?project= ' . $ this -> object [ ' project ' ] ;
153+ $ url = $ this ->api_url . '?project= ' . $ project ;
147154
148155 $ status = $ this ->fetch ('GET ' , $ url , '' , [
149156 "x-api-key: {$ this ->apikey }"
@@ -162,36 +169,35 @@ public function getStatus() {
162169
163170 public function waitToFinish ($ delay =5 , $ callback =null ) {
164171
165- $ max_loops = 100 ;
172+ $ max_loops = 60 ;
166173 $ loops = 0 ;
167174
168175 while ($ loops <$ max_loops ) {
169176 $ response = $ this ->getStatus ();
170177
171- if ($ response && ($ response ['success ' ]??false ) && isset ($ response ['movies ' ]) && count ($ response ['movies ' ])==1 ) {
172- if (isset ($ response ['movies ' ][0 ]['status ' ]) && $ response ['movies ' ][0 ]['status ' ]=='done ' ) {
173- if (is_callable ($ callback )) $ callback ($ response ['movies ' ][0 ]);
174- else $ this ->printStatus ($ response ['movies ' ][0 ]);
178+ if ($ response && ($ response ['success ' ]??false ) && !empty ($ response ['movie ' ])) {
175179
176- return $ response ['movies ' ][0 ];
180+ if (is_callable ($ callback )) $ callback ($ response ['movie ' ], $ response ['remaining_quota ' ]);
181+ else $ this ->printStatus ($ response ['movie ' ], $ response ['remaining_quota ' ]);
182+
183+ if (!empty ($ response ['movie ' ]['status ' ]) && $ response ['movie ' ]['status ' ]=='done ' ) {
184+ return $ response ;
177185 }
178186 }
179187 else {
180188 throw new \Error ('Invalid API response ' );
181189 }
182190
183- if (is_callable ($ callback )) $ callback ($ response ['movies ' ][0 ]);
184- else $ this ->printStatus ($ response ['movies ' ][0 ]);
185-
186191 sleep ($ delay );
187192 $ loops ++;
188193 }
189194 }
190195
191- public function printStatus ($ response ) {
192- echo 'Status: ' , $ response ['status ' ], ' / ' , $ response ['task ' ], PHP_EOL ;
196+ public function printStatus ($ response, $ quota ) {
197+ echo 'Status: ' , $ response ['status ' ], ' / ' , $ response ['message ' ], PHP_EOL ;
193198 if ($ response ['status ' ]=='done ' ) {
194- echo PHP_EOL , 'Movie URL: ' , $ response ['url ' ], PHP_EOL , PHP_EOL ;
199+ echo PHP_EOL , 'Movie URL: ' , $ response ['url ' ], PHP_EOL ;
200+ echo 'Remaining quota: movies( ' , $ quota ['movies ' ], ') and drafts( ' , $ quota ['drafts ' ], ') ' , PHP_EOL , PHP_EOL ;
195201 }
196202 }
197203}
0 commit comments