Repository navigation
Expand file tree
/
Copy pathgenerateTutorials.mjs
More file actions
283 lines (219 loc) · 11.1 KB
/
Copy pathgenerateTutorials.mjs
File metadata and controls
283 lines (219 loc) · 11.1 KB
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
import OpenAI from "openai";
import fs from 'fs';
import xmlbuilder from 'xmlbuilder';
import Ftp from 'ftp';
import path from 'path';
import fse from 'fs-extra';
import { environment } from "./environment";
for (let i = 0; i < 100; i++) {
await generateTutorial();
}
for (let i = 0; i < 100; i++) {
await generateTutorial();
}
generateSitemap();
uploadToFtp(function () { moveFolder("backupPages", "pages"), moveFolder("generatePages", "backupPages") });
async function generateTutorial() {
console.log("Generating tutorial...");
let openai = new OpenAI({ apiKey: environment.apiKey });
let question = `
You are a helpful assistant specialized in generating detailed and SEO-friendly tutorials or manuals in HTML format. When you create a tutorial, it must be based on a totally random topic (not one of the topics listed in the user prompt). The tutorial must be **as long as possible** (Ideal length 3000 characters), clear, detailed, and formatted using HTML, with special attention to SEO basics. The tutorial should include HTML elements such as h2, h3, and other subheadings to structure the content. Keywords must be included naturally in the content. Images may also be used if appropriate.
The tutorial must follow these guidelines:
1. Title: The title should be relevant to the topic and not exceed 5 words.
2. Filename: Provide a URL-friendly filename (correct name for SEO, no spaces, only lowercase and hyphens).
3. Meta Description: Write a concise description for the meta tag (maximum 160 characters).
4. HTML Body: The tutorial content should be wrapped in a 'div' with the class "tutorial". Avoid using the h1 element in the body. Ensure the content is informative and structured using appropriate headings (h2, h3, etc.). **Make the content as long and detailed as possible (Ideal length 3000 characters)**.
5. Tags: Add a string array of relevant categories to which the tutorial applies (lowercase, hyphen-separated).
Ensure the tutorial does not include any of the following tutorials: {{Tutorials}}
And use a different topic than this: {{LastTutorial}}`;
let userQuestion = `Please create a **long, detailed** and SEO-friendly tutorial on a random topic (avoid the topics listed above). Format the tutorial in HTML and include all the required elements. The tutorial should contain the following:
- A relevant title (maximum 5 words).
- Please do not use an adjective or verb as the first word in the title. Don't use as the first word - 'Understanding', 'Essential', 'Mastering'. It may contain how-to, but it doesn't have to!
- A filename for the tutorial (URL-friendly, lowercase, with hyphens).
- A meta description (for SEO, maximum 160 characters).
- Structured content with h2, h3, and other subheadings as needed.
- Ensure the content is clear, detailed, and includes keywords naturally.
- **Make the content as long as possible** and as comprehensive as possible (Ideal length 3000 characters).
- Tags should be relevant to the topic (lowercase, hyphen-separated).
- The content should be wrapped in a 'div' with the class "tutorial", and should not include the name of the topic or h1 tag.
- Optional: Include images if applicable..
**The result must be valid JSON** with the following keys:
- 'title': Topic title.
- 'file': Filename (URL-friendly).
- 'description': Meta description.
- 'body': HTML content. **Ideal length 3000 characters**
- 'tags': Array of categories/tags related to the topic. `;
let tutorialsText = "";
let lastTutorial = "";
try {
console.log("Reading used tutorials...");
let data = fs.readFileSync('tutorials.txt', 'utf8');
let tutorials = [];
data.split('\r\n').filter(x => x).forEach(tutorial => {
var tutorialJson = JSON.parse(tutorial);
tutorials.push(tutorialJson);
});
tutorialsText = tutorials.join(', ');
tutorialsText = tutorials.map(tutorial => tutorial.title).join(', ');
lastTutorial = tutorials[tutorials.length - 1];
question = question.replaceAll('{{Tutorials}}', tutorialsText);
question = question.replaceAll('{{LastTutorial}}', lastTutorial.title);
console.log("Requesting openai tutorial generation...");
let completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [
{ role: "system", content: question },
{
role: "user",
content: userQuestion,
},
],
});
let jsonResult = completion.choices[0].message.content;
console.log("Cleaning JSON result...");
let result = JSON.parse(cleanJsonString(jsonResult));
console.log("Generation '" + result.title + "' tutorial...");
tutorials.push({ title: result.title, file: result.file });
let fileTutorials = "";
tutorials.forEach(tutorial => {
fileTutorials += JSON.stringify(tutorial) + "\r\n";
});
console.log("Saved new tutorial to file...");
fs.writeFileSync("tutorials.txt", fileTutorials, 'utf8');
let tags = result.tags.map(tag => `#${tag}`).join(' ');
console.log("Creating folder...");
let generateFolderPath = "generatePages/" + result.file;
if (!fs.existsSync(generateFolderPath)) {
fs.mkdirSync(generateFolderPath, { recursive: true })
}
console.log("Read template...");
let template = fs.readFileSync('template.html', 'utf8');
template = template.replaceAll('{{Description}}', result.description);
template = template.replaceAll('{{Title}}', result.title);
template = template.replaceAll('{{Content}}', result.body);
template = template.replaceAll('{{Tags}}', tags);
template = template.replaceAll('{{PreviousLink}}', lastTutorial.file);
console.log("Create file...");
fs.writeFileSync(generateFolderPath + "/index.html", template, 'utf8');
console.log("Tutorial generated!");
} catch (err) {
console.error(err);
}
}
function cleanJsonString(jsonString) {
const pattern = /^```json\s*(.*?)\s*```$/s;
const cleanedString = jsonString.replace(pattern, '$1');
return cleanedString.trim();
}
function generateSitemap() {
console.log("Generating sitemap...");
console.log("Reading used tutorials...");
let data = fs.readFileSync('tutorials.txt', 'utf8');
const today = new Date().toISOString().split('T')[0];
var urlset = xmlbuilder.create('urlset').att('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
urlset.ele('url')
.ele('loc', 'https://www.tutorial-expert.com').up()
.ele('lastmod', today).up()
.ele('changefreq', 'daily').up()
.ele('priority', '1.0').up();
urlset.ele('url')
.ele('loc', 'https://www.tutorial-expert.com/privacy-policy').up()
.ele('lastmod', today).up()
.ele('changefreq', 'daily').up()
.ele('priority', '0.9').up();
urlset.ele('url')
.ele('loc', 'https://www.tutorial-expert.com/terms-of-use').up()
.ele('lastmod', today).up()
.ele('changefreq', 'daily').up()
.ele('priority', '0.9').up();
urlset.ele('url')
.ele('loc', 'https://www.tutorial-expert.com/more-tutorials').up()
.ele('lastmod', today).up()
.ele('changefreq', 'daily').up()
.ele('priority', '0.9').up();
data.split('\r\n').filter(x => x).forEach(tutorial => {
var tutorialJson = JSON.parse(tutorial);
urlset.ele('url')
.ele('loc', `https://www.tutorial-expert.com/${tutorialJson.file}`).up()
.ele('lastmod', today).up()
.ele('changefreq', 'daily').up()
.ele('priority', '1.0').up();
});
const xmlString = urlset.end({ pretty: true });
fs.writeFileSync('sitemap.xml', xmlString);
console.log("Generating sitemap finished");
}
function uploadToFtp(...callbacks) {
console.log("Uploading to FTP...");
const ftpClient = new Ftp();
ftpClient.on('ready', function () {
console.log("Uploading static files...");
uploadFileToFtp(ftpClient, "ads.txt");
uploadFileToFtp(ftpClient, "adsense.js");
uploadFileToFtp(ftpClient, "adsense.min.js");
uploadFileToFtp(ftpClient, "index.html");
uploadFileToFtp(ftpClient, "index-style.css");
uploadFileToFtp(ftpClient, "index-style.min.css");
uploadFileToFtp(ftpClient, "logo.ico");
uploadFileToFtp(ftpClient, "sitemap.xml");
uploadFileToFtp(ftpClient, "style.css");
uploadFileToFtp(ftpClient, "style.min.css");
uploadFileToFtp(ftpClient, "tutorials.js");
uploadFileToFtp(ftpClient, "tutorials.txt");
uploadFileToFtp(ftpClient, "tutorials.min.js");
uploadFileToFtp(ftpClient, "web.config");
uploadFileToFtp(ftpClient, "cookies/cookies.css");
uploadFileToFtp(ftpClient, "cookies/cookies.min.css");
uploadFileToFtp(ftpClient, "cookies/cookies.js");
uploadFileToFtp(ftpClient, "cookies/cookies.min.js");
uploadFileToFtp(ftpClient, "more-tutorials/index.html");
uploadFileToFtp(ftpClient, "privacy-policy/index.html");
uploadFileToFtp(ftpClient, "terms-of-use/index.html");
console.log("Uploading tutorials...");
// fs.readdirSync('generatePages/', function (err, folders) {
// folders.forEach(function (folder) {
// console.log("Uploading folder: " + folder);
// uploadFolderWithFileToFtp(ftpClient, `${folder}/index.html`, `generatePages/${folder}/index.html`);
// });
// });
let tutorials = fs.readdirSync('generatePages/');
tutorials.forEach(function (folder) {
console.log("Uploading folder: " + folder);
uploadFolderWithFileToFtp(ftpClient, `${folder}/index.html`, `generatePages/${folder}/index.html`);
});
ftpClient.end();
ftpClient.on('end', function () {
console.log("Execute callbacks...");
callbacks.forEach(callback => {
callback();
});
});
console.log("Uploading to FTP finished");
});
console.log("FTP connecting...");
ftpClient.connect({
'host': 'd115wh.forpsi.com',
'user': 'tutorial_expertcom',
'password': 'qKcshm48dH'
});
}
function uploadFileToFtp(ftpClient, filePath) {
const targetPath = '/www/' + filePath;
ftpClient.put(filePath, targetPath, function (err, list) {
if (err) throw err;
});
}
function uploadFolderWithFileToFtp(ftpClient, filePath, sourceFilePath = null) {
const targetPath = '/www/' + filePath;
const targetDir = path.dirname(targetPath);
ftpClient.mkdir(targetDir, true, function (err, list) {
ftpClient.put(sourceFilePath ?? filePath, targetPath, function (err, list) {
if (err) throw err;
});
});
}
function moveFolder(source, destination) {
console.log("Moving folder " + source + " to " + destination);
fse.copySync(source, destination, { overwrite: true })
fse.emptyDirSync(source)
}