forked from saricden/ezlo.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·362 lines (307 loc) · 10 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env node
const process = require('process');
const OpenAI = require('openai');
const SimplDB = require('simpl.db');
const { execSync } = require('child_process');
const fs = require('fs');
const https = require('https');
const readline = require('readline-sync');
require('dotenv').config()
const db = new SimplDB();
const openai = new OpenAI({
apiKey: db.get('apiKey'),
});
const arg1 = process.argv[2];
const arg2 = process.argv[3];
const notConfigured = !(db.get('apiKey') && db.get('sitePath'));
if (!arg1 && !arg2) {
console.log('Ezlo.js - AI powered article generator.');
console.log('Usage: ezlo [command] [argument]');
console.log('Commands:');
console.log(' config - Configure OpenAI API key, Hugo site path and blog niche.');
console.log('');
console.log(' draft new - Create a new draft.');
console.log(' draft - Print the path to the draft file.');
console.log(' draft edit - Open the draft file in nano.');
console.log('');
console.log(' ideas gen - Generate 3 new ideas.');
console.log(' ideas - List previously generated ideas.');
console.log(' ideas clear - Clear all ideas from the database.');
console.log('');
console.log(' thesis [idea_index] - Set the active idea and generate a thesis.');
console.log('');
console.log(' body - Generate an article body based on the thesis.');
console.log('');
console.log(' banner gen - Generate a banner image based on the active idea.');
console.log(' banner - Preview the generated banner.');
console.log('');
console.log(' audio - Start the audio generator tool (very beta).');
return;
}
else if (arg1 === 'config') {
const sitePath = readline.question('Hugo site path: ');
const apiKey = readline.question('OpenAI API key: ', { hideEchoBack: true });
const blogNiche = readline.question('A niche blog about... ');
db.set('sitePath', sitePath);
db.set('apiKey', apiKey);
db.set('blogNiche', blogNiche);
console.log('Config saved.');
return;
}
else if (notConfigured) {
console.log('Please configure your OpenAI API key and Hugo site path first.');
return;
}
if (arg1 === 'ideas' && arg2 === 'clear') {
db.set('prevIdeas', []);
console.log('All ideas cleared from the database.');
return;
}
else if (arg1 === 'ideas' && arg2 === 'gen') {
generateIdeas();
return;
}
else if (arg1 === 'ideas') {
const ideas = db.get('prevIdeas');
if (ideas.length === 0) {
console.log('No ideas found in the database.');
return;
}
else {
for (let i = 0; i < ideas.length; i++) {
console.log(i + ': ' + ideas[i]);
}
}
return;
}
else if (arg1 === 'draft' && arg2 === 'new') {
const filename = Math.random().toString(36).substring(2, 15) + '.md';
const path = require('path').resolve(__dirname, filename);
fs.writeFile(path, '', (err) => {
if (err) throw err;
console.log(`Draft created: ${path}`);
});
db.set('draftPath', path);
console.log(`Draft path set to: ${path}`);
return;
}
else if (arg1 === 'draft' && arg2 === 'edit') {
const draftPath = db.get('draftPath');
if (!draftPath) {
console.log('No draft found in the database.');
}
else {
execSync(`nano ${draftPath}`, { stdio: 'inherit' });
}
return;
}
else if (arg1 === 'draft') {
const path = db.get('draftPath');
if (!path) {
console.log('No draft found in the database.');
}
else {
console.log(path);
}
return;
}
else if (arg1 === 'thesis' && Number.isInteger(parseInt(arg2))) {
const ideaIndex = parseInt(arg2);
let allIdeas = db.get('prevIdeas');
const idea = allIdeas[ideaIndex];
if (!idea) {
console.log('Idea not found in the database.');
}
else {
console.log(`Idea: "${idea}"`);
console.log('Generating thesis...');
allIdeas[ideaIndex] = `✅ ${idea}`;
db.set('prevIdeas', allIdeas);
db.set('draftIdea', idea);
generateThesis(idea);
}
return;
}
else if (arg1 === 'thesis') {
const thesis = db.get('draftThesis');
if (!thesis) {
console.log('No thesis found in the database.');
}
else {
console.log(thesis);
}
return;
}
else if (arg1 === 'body') {
const thesis = db.get('draftThesis');
if (!thesis) {
console.log('No thesis found in the database.');
}
else {
console.log(`Thesis: "${thesis}"`);
console.log('Generating article body...');
generateBody(thesis);
}
return;
}
else if (arg1 === 'banner' && arg2 === 'gen') {
const idea = db.get('draftIdea');
let allIdeas = db.get('prevIdeas');
const ideaIndex = allIdeas.indexOf(idea);
if (!idea) {
console.log('Idea not found in the database.');
}
else {
allIdeas[ideaIndex] = `🖼️ ${idea}`;
db.set('prevIdeas', allIdeas);
generateBanner();
}
return;
}
else if (arg1 === 'banner') {
const banner = db.get('draftBanner');
if (!banner) {
console.log('No banner found in the database.');
}
else {
execSync(`open "" "${banner}"`, {
stdio: 'inherit'
});
}
return;
}
else if (arg1 === 'publish') {
const sitePath = db.get('sitePath');
const draftPath = db.get('draftPath');
const draftBanner = db.get('draftBanner');
const draftIdea = db.get('draftIdea');
const slug = draftIdea.replace(/ /g, '-').toLowerCase();
const targetPath = `${sitePath}/content/posts/${slug}.md`;
const targetBanner = `${sitePath}/static/${slug}.png`;
fs.copyFileSync(draftPath, targetPath);
fs.copyFileSync(draftBanner, targetBanner);
let article = fs.readFileSync(targetPath, 'utf8');
const frontMatter = `---\ndate: ${new Date().toISOString()}\ntitle: "${draftIdea}"\nurl: ${slug}\ncover:\n image: /${slug}.png\n---\n\n`;
article = frontMatter + article;
fs.writeFileSync(targetPath, article);
console.log(`Article published: ${targetPath}`);
}
else if (arg1 === 'audio') {
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
app.use(express.static('audio_generator'));
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/generate', async (req, res) => {
const text = req.body.text;
if (!text) {
res.send('<audio controls id="audio"></audio>');
}
else {
await generateAudio(text);
const audio = `<audio controls id="audio"><source src="./audio.mp3"></audio>`;
res.send(audio);
}
});
app.listen(port, () => {
require("openurl").open("http://localhost:" + port);
console.log(`Example app listening on port ${port}`);
});
return;
}
async function generateIdeas() {
const prevIdeas = db.get('prevIdeas');
const prompt = `Present 3 article ideas for a niche blog post about ${db.get('blogNiche')}. Do not repeat any ideas used previously.\n\nPreviously used ideas:\n${prevIdeas.join('\n')}`;
console.log('Fetching ideas from OpenAI...');
const completion = await openai.chat.completions.create({
messages: [
{"role": "system", "content": "You are a blog writer. Your response must be a JSON array contained in a key called ideas. Each idea should be a string."},
{"role": "user", "content": prompt}
],
model: "gpt-4o",
response_format: { type: "json_object" }
});
console.log(completion);
const {ideas} = JSON.parse(completion.choices[0].message.content);
if (Array.isArray(ideas)) {
db.set('prevIdeas', [
...prevIdeas,
...ideas
]);
console.log('Ideas added to database.');
}
}
async function generateThesis(idea) {
const prompt = `Create a thesis statement for a niche blog about ${db.get('blogNiche')}. Use the idea: "${idea}". The thesis must contain 3 unique key points that support the thesis. Write in plain text and do not use markdown. Do not repeat the idea verbetim.`;
console.log('Fetching thesis from OpenAI...');
const completion = await openai.chat.completions.create({
messages: [
{"role": "system", "content": "You are a blog writer."},
{"role": "user", "content": prompt}
],
model: "gpt-4o"
});
console.log(completion);
const {content} = completion.choices[0].message;
const path = db.get('draftPath');
if (path) {
fs.appendFile(path, content, (err) => {
if (err) throw err;
console.log('Thesis appended to draft.');
});
}
db.set('draftThesis', content);
console.log('Thesis set in database.');
}
async function generateBody(thesis) {
const prompt = `Create an article body explaining the thesis. Use the thesis: "${thesis}". Write in plain text and do not use markdown or section headers. Include a summary at the end.`;
console.log('Fetching article body from OpenAI...');
const completion = await openai.chat.completions.create({
messages: [
{"role": "system", "content": "You are a blog writer."},
{"role": "user", "content": prompt}
],
model: "gpt-4o"
});
console.log(completion);
const {content} = completion.choices[0].message;
const path = db.get('draftPath');
if (path) {
fs.appendFile(path, content, (err) => {
if (err) throw err;
console.log('Article body appended to draft.');
});
}
}
async function generateBanner() {
const idea = db.get('draftIdea');
const prompt = `Create a banner image for a blog post about ${idea}. The image should be in PNG format and have a resolution of 1792x1024 pixels. Do not use text at all.`;
console.log('Generating banner image using OpenAI...');
const response = await openai.images.generate({
model: "dall-e-3",
prompt,
n: 1,
size: "1792x1024",
});
console.log(response);
const image_url = response.data[0].url;
const path = db.get('draftPath').replace(/\.md$/, '.png');
const file = fs.createWriteStream(path);
https.get(image_url, (response) => {
response.pipe(file);
});
db.set('draftBanner', path);
console.log(`Banner set in database: ${path}`);
}
async function generateAudio(input) {
const path = require('path');
const speechFile = path.resolve("./audio_generator/audio.mp3");
const mp3 = await openai.audio.speech.create({
model: "tts-1-hd",
voice: "onyx",
input
});
const buffer = Buffer.from(await mp3.arrayBuffer());
await fs.promises.writeFile(speechFile, buffer);
}