-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
191 lines (157 loc) · 5.74 KB
/
index.js
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
/*
JSON2Video SDK v2.0
Author: JSON2Video.com
Description: SDK for creating videos programmatically using JSON2Video API
GET YOUR FREE APIKey at https://json2video.com/get-api-key/
CHECK DOCUMENTATION at: https://json2video.com/docs/
*/
class Base {
constructor() {
this.object = {};
this.properties = [];
}
// set(): Sets a property for the Scene or Movie
set(property, value) {
property = property.toLowerCase();
property = property.replace(/_/g, '-');
if (this.properties.indexOf(property) > -1) {
this.object[property] = value;
}
else throw `Property ${property} does not exist`;
}
// addElement(): Adds a new element in the Scene or Movie
addElement(element = null) {
if (element && typeof element == "object") {
if (!("elements" in this.object)) {
this.object.elements = [];
}
this.object.elements.push(element);
return true;
}
return false;
}
// getJSON(): Returns the data object as a JSON string
getJSON() {
return JSON.stringify(this.object, null, 2);
}
// getObject(): Returns the data object
getObject() {
return this.object;
}
}
class Scene extends Base {
constructor(...a) {
super(...a);
this.properties = ['comment', 'background-color', 'duration', 'cache'];
}
// setTransition(): Sets the transition style for this scene
setTransition(style = null, duration = null, type = null) {
if (style || duration || type) {
if (!("transition" in this.object)) this.object.transition = {};
if (style !== null) this.object.transition.style = style;
if (duration !== null) this.object.transition.duration = duration;
if (type !== null) this.object.transition.type = type;
}
};
}
class Movie extends Base {
constructor(...a) {
super(...a);
this.properties = ['comment', 'draft', 'width', 'height', 'resolution', 'exports', 'quality', 'fps', 'cache', 'template', 'variables', 'id'];
this.api_url = 'https://api.json2video.com/v2/movies';
this.apikey = null;
}
// setAPIKey(): Sets your API Key
setAPIKey = function (apikey) {
this.apikey = apikey;
};
// addScene(): Adds a new scene in the Movie
addScene = function (scene = null) {
if (scene) {
if (!("scenes" in this.object)) this.object.scenes = [];
this.object.scenes.push(scene.getObject());
return true;
}
else throw "Invalid scene";
};
// fetch(): Encapsulates API calls
fetch = async function (method = "GET", url = "", body = null, headers = {}) {
const https = require('https');
const endpoint = new URL(url);
let data = null;
if (body) {
data = JSON.stringify(body);
headers['Content-Length'] = Buffer.byteLength(data);
}
const options = {
hostname: endpoint.hostname,
port: 443,
path: endpoint.pathname + endpoint.search,
method: method,
headers: headers
};
return new Promise((resolve, reject) => {
const req = https.request(options, res => {
res.on('data', response => {
response.status = res.statusCode;
resolve(JSON.parse(response.toString()));
});
});
req.on('error', error => {
console.error(error);
reject({
status: 500,
success:false,
message: "Unknown SDK error",
error: error
});
});
if (data) req.write(data);
req.end();
});
};
// render(): Starts a new rendering job
render = async function() {
if (!this.apikey) throw "Invalid API Key";
let response = await this.fetch("POST", this.api_url, this.object, {
"Content-Type": "application/json",
"x-api-key": this.apikey
});
if (response && response.success && response.project) this.object.project = response.project;
return response;
};
// getStatus(): Gets the current project rendering status
getStatus = async function(project=null) {
if (!project) project = this.object.project??null;
if (!this.apikey) throw("Invalid API Key");
if (!("project" in this.object)) throw("Project ID not set");
let url = this.api_url + "?project=" + this.object.project;
return this.fetch("GET", url, null, {
"x-api-key": this.apikey
});
};
// waitToFinish(): Waits the current project to finish rendering by checking status every 1 second
waitToFinish = async function(callback=null) {
return new Promise((resolve, reject) => {
const interval_id = setInterval((async function() {
let response = await this.getStatus().catch((err) => {
reject(err);
});
if (response && response.success && ("movie" in response)) {
if (response.movie.status=="done") {
clearInterval(interval_id);
resolve(response);
}
if (typeof callback == "function") callback(response);
}
else {
clearInterval(interval_id);
resolve(response);
}
}).bind(this), 5000);
});
};
}
// Export Scene and Movie objects
exports.Scene = Scene;
exports.Movie = Movie;