-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
317 lines (270 loc) · 7.39 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* eslint-disable indent */
import inquirer from "inquirer";
import gradient from "gradient-string";
import chalk from "chalk";
import fs from "fs";
import ora from "ora";
import { dirname, join } from "path";
import { fileURLToPath, pathToFileURL } from "url";
import yaml from "yaml";
import processAiData from "./src/modules/AiDataProcessor.js";
import fetchSurah from "./src/modules/fetchSurah.js";
import ImagesGenerator from "./src/modules/imagesGenerator.js";
import VideosGenerator from "./src/modules/videosGenerator.js";
import { initCache } from "./src/modules/cachedFetch.js";
import S3 from "./src/modules/putToS3.js";
import Reel from "./src/modules/publishReel.js";
const settings = yaml.parse(fs.readFileSync("./settings.yaml", "utf8"));
function choices() {
return inquirer.prompt({
type: "list",
name: "choice",
message: "Choose what to do?",
choices: [
"Create and upload video from API (quran.com)",
"Create and upload video from AI data",
"Upload exist video (preview.mp4)",
],
});
}
async function main() {
greeting();
initCache(".cache");
const answer = await choices();
if (answer.choice === "Create and upload video from AI data") {
const video = await AIChose();
console.log(
gradient.passion("Preview video:"),
chalk.blueBright(
pathToFileURL(
fileURLToPath(join(dirname(import.meta.url), "preview.mp4")),
),
),
);
const publishAnswer = await inquirer.prompt({
type: "confirm",
message: "Publish Video?",
name: "confirm",
});
if (publishAnswer.confirm) {
await publish(video);
}
} else if (answer.choice === "Create and upload video from API (quran.com)") {
const video = await APIChose();
console.log(
gradient.passion("Preview video:"),
chalk.blueBright(
pathToFileURL(
fileURLToPath(join(dirname(import.meta.url), "preview.mp4")),
),
),
);
const publishAnswer = await inquirer.prompt({
type: "confirm",
message: "Publish Video?",
name: "confirm",
});
if (publishAnswer.confirm) {
await publish(video);
}
} else {
await publish(fs.readFileSync("preview.mp4"));
}
}
main();
function greeting() {
console.log(
gradient.retro.multiline(`
... * . _ .
* . * . * (_) *
. |* .. * ..
. * \\| * ___ . . *
* \\/ |/ \\/{o,o} .
_\\_\\ | / /) )* _/_ *
\\ \\| /,--"-"--- ..
_-----\` |(,__,__/__/_ .
\\ || ..
||| . *
|||
|||
, -=-~' .-^- _
`),
);
console.log(
`\x1b[1m\n${gradient.instagram(" WELCOME TO QURAN-REELS")}\n\n\x1b[22m`,
);
}
async function AIChose() {
const answers = await questions("AI");
const imagesGenerator = new ImagesGenerator({
...settings.image,
...answers,
});
const surahSpinner = await ora(
gradient.cristal("Getting Surah data"),
).start();
const surahData = await fetchSurah({
...settings.fetch,
...answers,
});
await surahSpinner.stop();
const surah = await processAiData(
surahData,
JSON.parse(fs.readFileSync("./data/data.json")),
"./audio/audio.webm",
);
const generator = new VideosGenerator(
imagesGenerator,
{
x264Preset: "ultrafast",
},
answers.highlightWords,
);
console.log(gradient.morning("Video processing"));
const videoData = await generator.generateFromSurah(surah);
fs.writeFileSync("preview.mp4", videoData);
return videoData;
}
async function APIChose() {
const answers = await questions("API");
const imagesGenerator = new ImagesGenerator({
...settings.image,
...answers,
});
const surahSpinner = await ora(
gradient.cristal("Getting Surah data"),
).start();
const surahData = await fetchSurah({
...settings.fetch,
...answers,
});
await surahSpinner.stop();
const generator = new VideosGenerator(
imagesGenerator,
{
x264Preset: "ultrafast",
},
answers.highlightWords,
);
console.log(gradient.morning("Video processing"));
const videoData = await generator.generateFromSurah(surahData);
fs.writeFileSync("preview.mp4", videoData);
return videoData;
}
async function publish(video) {
const s3 = new S3({
accessId: process.env.AWS_ACCESS_KEY_ID,
secret: process.env.AWS_SECRET_ACCESS_KEY,
region: "eu-north-1",
});
const { caption } = await inquirer.prompt({
type: "input",
default: null,
message: "What is the Reel caption?",
name: "caption",
});
const uploadingSpinner = ora(gradient.rainbow("Uploading Video")).start();
const { Location: videoURL } = await s3.upload(video);
uploadingSpinner.stopAndPersist();
const publishSpinner = ora(
gradient.instagram("Publishing Video to Instagram"),
).start();
const reel = new Reel({
accessToken: process.env.LONG_TERM_ACCESS_TOKEN,
instagramAccountId: process.env.INSTAGRAM_ACCOUNT_ID,
});
const reelURL = await reel.post({
videoURL,
...(caption ? { caption } : {}),
});
publishSpinner.stop();
console.log(gradient.retro(`Published successfully: ${reelURL}`));
}
async function questions(type) {
const validate = (value) => {
if (!+value) return "Please enter a valid number";
return true;
};
let answers = await inquirer.prompt([
{
type: "input",
name: "id",
message: "What is the number of Surah (1-114)?",
validate(value) {
if (!+value || value > 114 || value <= 0)
return "Please enter a valid number";
return true;
},
},
...(type === "API"
? [
{
type: "input",
name: "reciter",
message: "What is the reciter id?",
default: settings.fetch.reciter,
},
]
: []),
{
type: "input",
name: "translator",
message: "What is the translator id?",
default: settings.fetch.translator,
},
{
type: "input",
name: "from",
message: "From which Ayah does the data begin?",
validate,
},
{
type: "input",
name: "to",
message: "What Ayah does the data end with?",
validate,
},
{
type: "input",
name: "background",
message: "What is the background color? (#<hex number only>)",
default: settings.image.background,
validate(value) {
const regex = /^#[0-9A-Fa-f]{6}$/i; // Regex for valid hex color code
if (!regex.test(value)) {
return "Please enter a valid hex color code (e.g., #FFFFFF)";
}
return true;
},
},
{
type: "confirm",
message: "Want to add highlight?",
name: "highlightWords",
},
]);
if (answers.highlightWords) {
const plusQues = await inquirer.prompt({
type: "input",
message: "What is the color of highlight text?",
default: settings.image.highlightColor,
name: "highlightColor",
validate(value) {
const regex = /^#[0-9A-Fa-f]{6}$/i; // Regex for valid hex color code
if (!regex.test(value)) {
return "Please enter a valid hex color code (e.g., #FFFFFF)";
}
return true;
},
});
answers = { ...answers, ...plusQues };
}
const additionalQues = await inquirer.prompt([
{
type: "confirm",
message: "Add Surah name?",
name: "surahNameEnabled",
},
]);
return { ...answers, ...additionalQues };
}