-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
322 lines (259 loc) · 10 KB
/
server.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
const { Configuration, OpenAIApi } = require("openai");
const cron = require('node-cron');
const express = require('express');
const cors = require('cors');
const dotenv = require('dotenv');
const axios = require('axios');
const { Octokit } = require('@octokit/core');
dotenv.config();
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const octokit = new Octokit({ auth: process.env.OCTOKIT_KEY });
const repoOwner = 'ruthdillonmansfield';
const repoName = 'autoblog-front-end-next';
const branch = 'main';
const topics = ["ethics", "metaphysics", "epistemology", "aesthetics", "philosophical logic", "ontology", "metametaphysics", "philosophy of science", "philosophy of religion", "philosophy of language", "philosophy of mind", "philosophy of technology", "ancient philosophy", "bioethics", "philosophy of mathematics", "philosophy of law", "philosophy of time", "philosophy of substance", "modality"]
const approaches = [
"Create a beginner's guide to a specific area within this topic.",
"Create a beginner's guide to a specific area within this topic.",
"Create a beginner's guide to a specific area within this topic.",
"Examine a specific argument within this philosophical topic.",
"Analyze the views of a particular philosopher on this topic.",
"Discuss the historical development of this philosophical school of thought.",
"Debate the validity of opposing perspectives on this issue.",
"Compare and contrast the key concepts of different philosophical theories.",
"Evaluate the implications of a specific philosophical idea within this topic on other disciplines.",
"Explain the significance of a specific thought experiment within this topic.",
"Critique the strengths and weaknesses of a particular philosophical argument."
];
const app = express();
app.use(cors());
app.get('/generate', async (req, res) => {
try {
await generateAndSaveBlogPost();
res.status(200).json({ message: 'Blog post generated and saved successfully.' });
} catch (error) {
console.error('Error while generating and saving the blog post:', error);
res.status(500).json({ message: 'Failed to generate and save blog post.', error: error.message });
}
});
async function fetchLast15Files() {
const listFilesResponse = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner: repoOwner,
repo: repoName,
path: 'posts',
ref: branch,
});
return listFilesResponse.data.slice(-15);
}
async function fetchLast15Titles(last15Files) {
const last15TitlesPromises = last15Files.map(async (file) => {
const fileContentResponse = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner: repoOwner,
repo: repoName,
path: file.path,
ref: branch,
});
const fileContent = Buffer.from(fileContentResponse.data.content, 'base64').toString();
// Extract the title from the Markdown front matter
const titleMatch = fileContent.match(/title:\s*['"](.+?)['"]/);
const title = titleMatch ? titleMatch[1] : 'Untitled';
return title;
});
return await Promise.all(last15TitlesPromises);
}
async function generateTitle() {
const randomIndex = Math.floor(Math.random() * topics.length);
const topic = topics[randomIndex];
const randomIndex2 = Math.floor(Math.random() * approaches.length);
const approach = approaches[randomIndex2];
const promptContents = `Generate a unique, specific and compelling blog title on a subtopic within the broad topic of ${topic}. ${approach}. It should be 6-10 words long.`;
const openAITitleResponse = await openai.createCompletion({
model: "text-davinci-003",
prompt: promptContents,
temperature: 0.9,
max_tokens: 40,
top_p: 1.0,
});
const generatedText = openAITitleResponse.data.choices[0].text.trim();
const cleanTitle = generatedText.replace(/["]/g, '');
const firstSentence = cleanTitle.match(/^.*?[\.?]\s/)?.[0] ?? cleanTitle;
return firstSentence;
}
async function generateDallePrompt(title) {
const openAIPromptResponse = await openai.createCompletion({
model: "text-davinci-003",
prompt: `I want to create a fantastic image for a blog post called ${title}. Your job is to imagine a simple, relevant image and describe it in less than 10 words.`,
temperature: 0.7,
max_tokens: 30,
top_p: 1.0,
});
return openAIPromptResponse.data.choices[0].text.trim();
}
function generateExcerpt(content) {
// Remove headings
const contentWithoutHeadings = content.replace(/^#+.+\n/gm, '');
// Match the first two sentences
const sentencesMatch = contentWithoutHeadings.match(/(?:^|(?<=[.?!]))\s*([^.?!]+[.?!])/g);
if (sentencesMatch) {
let excerpt = '';
let sentenceCount = 0;
for (const sentence of sentencesMatch) {
const trimmedSentence = sentence.trim();
if (trimmedSentence) {
excerpt += ' ' + trimmedSentence;
sentenceCount++;
}
if (sentenceCount >= 2) {
break;
}
}
return excerpt.trim();
}
// Return an empty string if no sentences found
return '';
}
async function generateImage(prompt) {
const openAIPromptResponse = await openai.createImage({
prompt: `Oil painting of ${prompt}`,
n: 1,
// size: "1024x1024",
size: "512x512"
// size: "256x256"
});
console.log("created image");
return openAIPromptResponse.data.data[0].url;
}
async function downloadImageToBase64(url) {
const response = await axios.get(url, { responseType: 'arraybuffer' });
const base64 = Buffer.from(response.data, 'binary').toString('base64');
return base64;
}
async function generateImageAndDownload(imagePath, outputTitle) {
const outputDallePrompt = await generateDallePrompt(outputTitle);
console.log(`Output DALL-E prompt is ${outputDallePrompt}`);
const outputImage = await generateImage(outputDallePrompt);
const imageBase64 = await downloadImageToBase64(outputImage);
console.log("Downloaded image");
await saveImage(imagePath, imageBase64)
console.log("Saved image");
return imageBase64;
}
async function generateContent(title) {
const promptContents = `Now write a compelling blog, structured with markdown, and including h2 and h3 and paragraphs. The blog must be based on this title: ${title}, a technical topic in philosophy. Don't be afraid to offer different points of view and opinions.`;
const openaiContentsResponse = await openai.createCompletion({
model: "text-davinci-003",
prompt: promptContents,
temperature: 0.7,
max_tokens: 1000,
top_p: 1.0,
});
const contentWithH1 = openaiContentsResponse.data.choices[0].text;
const contentWithoutH1 = contentWithH1.replace(/^#\s.*$/gm, '').trim();
console.log("created content: ", contentWithoutH1);
return contentWithoutH1;
}
async function saveBlogPost(filePath, markdownString, outputTitle) {
const base64EncodedContent = Buffer.from(markdownString).toString('base64');
let sha;
try {
const getFileResponse = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner: repoOwner,
repo: repoName,
path: filePath,
ref: branch
});
sha = getFileResponse.data.sha;
} catch (error) {
if (error.status !== 404) {
throw error;
}
}
const createOrUpdateFileRequest = {
owner: repoOwner,
repo: repoName,
path: filePath,
message: `Add new blog post: ${outputTitle}`,
content: base64EncodedContent,
branch: branch
};
if (sha) {
createOrUpdateFileRequest.sha = sha;
}
await octokit.request('PUT /repos/{owner}/{repo}/contents/{path}', createOrUpdateFileRequest);
}
async function saveImage(filePath, base64EncodedContent) {
let sha;
filePath = `public/${filePath}`;
try {
const getFileResponse = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner: repoOwner,
repo: repoName,
path: filePath,
ref: branch,
});
sha = getFileResponse.data.sha;
} catch (error) {
if (error.status !== 404) {
throw error;
}
}
const createOrUpdateImageRequest = {
owner: repoOwner,
repo: repoName,
path: filePath,
message: `Add new image for blog post`,
content: base64EncodedContent,
branch: branch,
};
if (sha) {
createOrUpdateImageRequest.sha = sha;
}
await octokit.request('PUT /repos/{owner}/{repo}/contents/{path}', createOrUpdateImageRequest);
}
async function generateAndSaveBlogPost() {
try {
const outputTitle = await generateTitle();
console.log(`\nOutput title is ${outputTitle}`);
const slug = outputTitle.toLowerCase().replace(/[^a-zA-Z0-9\s]/g, '').replace(/\s+/g, "-");
console.log(`\nSlug is ${slug}`);
// Save the blog post to the front-end repository
const filePath = `posts/${slug}.md`;
// Save the image to the /assets/blog/ folder with the slug
const imagePath = `assets/blog/${slug}.png`;
const [outputImage, outputContent] = await Promise.all([
generateImageAndDownload(imagePath, outputTitle),
generateContent(outputTitle),
]);
console.log(outputContent);
const excerpt = generateExcerpt(outputContent);
console.log(`\nGenerated excerpt is:`, excerpt);
// Create a Markdown string with the required keys and values
const markdownString = `---
title: "${outputTitle}"
excerpt: "${excerpt}"
coverImage: "/${imagePath}"
date: "${new Date().toISOString()}"
ogImage:
url: "/${imagePath}"
---
${outputContent}
`;
await saveBlogPost(filePath, markdownString)
console.log(`Successfully created/updated ${slug}.md in the ${repoName} repository.`);
} catch (error) {
console.error('Error while generating and saving the blog post:', error);
}
}
// Call the function immediately when the server starts
// generateAndSaveBlogPost();
// Set the cron job to run at 9 AM GMT every 2 days
// cron.schedule('0 9 */2 * *', generateAndSaveBlogPost);
// cron.schedule('0 9 * * *', generateAndSaveBlogPost);
// Start the Express server
const port = process.env.PORT || 3001;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});