-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathall.php
203 lines (153 loc) · 6.1 KB
/
all.php
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
<?php
/*
JSON2Video PHP SDK
This simple SDK is a wrapper for calling JSON2Video API
JSON2Video API allows you to create and edit videos programmatically
Documentation: https://json2video.com/docs/sdk
*/
namespace JSON2Video;
class Base {
protected $object, $properties;
public function __get($property) {
$property = strtolower($property);
if (in_array($property, $this->properties) && isset($this->object[$property])) {
return $this->object[$property];
}
return null;
}
public function __set($property, $value) {
$property = strtolower($property);
if (in_array($property, $this->properties)) {
$property = strtolower(str_replace('_', '-', $property));
$this->object[$property] = $value;
return $value;
}
return null;
}
public function addElement($element=null) {
if ($element && is_array($element)) {
if (!isset($this->object['elements'])) $this->object['elements'] = [];
$this->object['elements'][] = $element;
return true;
}
return false;
}
public function getJSON() {
return json_encode($this->object, JSON_PRETTY_PRINT);
}
public function getObject() {
return $this->object;
}
}
class Scene extends Base {
protected $properties = ['comment', 'background_color', 'transition', 'duration', 'cache'];
protected $object = [];
}
class Movie extends Base {
private $api_url = 'https://api.json2video.com/v2/movies';
protected $properties = ['comment', 'draft', 'width', 'height', 'resolution', 'exports', 'quality', 'fps', 'cache', 'template', 'variables', 'id'];
private $apikey = null;
protected $object = [];
public function setAPIKey($apikey) {
$this->apikey = $apikey;
}
public function addScene($scene=null) {
if ($scene && is_a($scene, 'JSON2Video\Scene')) {
if (!isset($this->object['scenes'])) $this->object['scenes'] = [];
$this->object['scenes'][] = $scene->getObject();
return true;
}
else throw new \Exception('Invalid scene');
return false;
}
private function fetch(string $method, string $url, string $body, array $headers = []) {
$context = stream_context_create([
"http" => [
"method" => $method,
"header" => implode("\r\n", $headers),
"content" => $body,
"ignore_errors" => true,
],
]);
$response = file_get_contents($url, false, $context);
$status_line = $http_response_header[0];
preg_match('{HTTP\/\S*\s(\d{3})}', $status_line, $match);
$status = $match[1];
return [
'status' => $status,
'message' => $status_line,
'response' => $response
];
}
public function render() {
if (empty($this->apikey)) throw new \Exception('Invalid API Key');
$postdata = json_encode($this->object);
if (is_null($postdata)) {
throw new \Exception('Invalid movie settings');
}
$response = $this->fetch('POST', $this->api_url, $postdata, [
"Content-Type: application/json",
"x-api-key: {$this->apikey}"
]);
if ($response) {
if ($response['status']=='200') {
$render = json_decode($response['response'], true);
if ($render['success']??false && !empty($render['project'])) $this->object['project'] = $render['project'];
else throw new \Exception("Render didn't return a project ID");
return $render;
}
elseif ($response['status']=='400') {
$api_response = json_decode($response['response'], true);
throw new \Exception('JSON Syntax error: ' . ($api_response['message'] ?? 'Unknown error'));
}
else {
throw new \Exception('API error: ' . ($response['message'] ?? 'Unknown error'));
}
}
else throw new \Error('SDK error');
return false;
}
public function getStatus($project=null) {
if (!$project) $project = $this->object['project'] ?? null;
if (empty($this->apikey)) throw new \Exception('Invalid API Key');
if (!$project) throw new \Exception('Project ID not set');
$url = $this->api_url . '?project=' . $project;
$status = $this->fetch('GET', $url, '', [
"x-api-key: {$this->apikey}"
]);
if ($status) {
if ($status['status']=='200') return json_decode($status['response'], true);
else {
throw new \Exception('API error: ' . ($status['message'] ?? 'Unknown error'));
}
}
else throw new \Error('SDK error');
return false;
}
public function waitToFinish($delay=5, $callback=null) {
$max_loops = 60;
$loops = 0;
while ($loops<$max_loops) {
$response = $this->getStatus();
if ($response && ($response['success']??false) && !empty($response['movie'])) {
if (is_callable($callback)) $callback($response['movie'], $response['remaining_quota']);
else $this->printStatus($response['movie'], $response['remaining_quota']);
if (!empty($response['movie']['status']) && $response['movie']['status']=='done') {
return $response;
}
}
else {
throw new \Error('Invalid API response');
}
sleep($delay);
$loops++;
}
}
public function printStatus($response, $quota) {
echo 'Status: ', $response['status'], ' / ', $response['message'], PHP_EOL;
if ($response['status']=='done') {
echo PHP_EOL, 'Movie URL: ', $response['url'], PHP_EOL;
echo 'Remaining quota: movies(', $quota['movies'], ') and drafts(', $quota['drafts'], ')', PHP_EOL, PHP_EOL;
}
}
}