blowfish/processUsers.js

99 lines
2.8 KiB
JavaScript
Raw Normal View History

2023-07-14 05:12:21 -05:00
const fs = require('fs');
const puppeteer = require("puppeteer");
const configDir = "./exampleSite/config/_default";
const defaultLang = "en";
2023-07-14 05:12:21 -05:00
const usersFolderPath = "./exampleSite/content/users/"
var targetLangs = []
const configFiles = fs.readdirSync(configDir);
configFiles.forEach(file => {
if (file.indexOf("languages.") > -1) {
var lang = file.split(".")[1];
if (lang != defaultLang) {
targetLangs.push(lang);
}
}
});
const indexFiles = fs.readdirSync(usersFolderPath);
for(var i in targetLangs){
var targetFile = '_index.' + targetLangs[i] + '.md';
if(indexFiles.indexOf(targetFile) == -1){
fs.copyFileSync(usersFolderPath + '_index.md', usersFolderPath + targetFile);
}
}
2023-07-14 05:12:21 -05:00
let rawdata = fs.readFileSync(usersFolderPath + 'users.json');
let users = JSON.parse(rawdata);
2023-07-14 06:37:16 -05:00
let userDict = {}
2023-07-20 15:34:37 -05:00
for (var i in users) {
2023-07-14 06:37:16 -05:00
userDict[users[i].title.replaceAll("/", "-")] = true;
}
2023-07-14 05:12:21 -05:00
2023-07-14 05:28:12 -05:00
const files = fs.readdirSync(usersFolderPath);
2023-07-14 06:37:16 -05:00
2023-07-14 05:12:21 -05:00
2023-07-14 05:28:12 -05:00
for (file in files) {
2023-07-14 05:12:21 -05:00
2023-07-14 05:28:12 -05:00
let stats = fs.statSync(usersFolderPath + files[file]);
if (files[file] != 'users.json' && files[file].indexOf("_index.") == -1){
2023-07-14 05:28:12 -05:00
if (stats.isDirectory()) {
2023-07-20 15:34:37 -05:00
if (!userDict[files[file].replaceAll("/", "-")]) {
2023-07-14 06:37:16 -05:00
console.log('deleting: ', files[file]);
fs.rmdirSync(usersFolderPath + files[file], { recursive: true, force: true });
}
2023-07-14 05:28:12 -05:00
} else {
2023-07-14 06:37:16 -05:00
console.log('deleting: ', files[file]);
2023-07-14 05:28:12 -05:00
fs.unlinkSync(usersFolderPath + files[file]);
}
}
}
2023-07-14 05:12:21 -05:00
2023-07-14 05:28:12 -05:00
puppeteer
.launch({
defaultViewport: {
width: 1280,
height: 800,
},
})
.then(async (browser) => {
2023-07-14 05:12:21 -05:00
2023-07-14 05:28:12 -05:00
const page = await browser.newPage();
2023-07-14 05:12:21 -05:00
2023-07-14 05:28:12 -05:00
for (var i in users) {
var userMDFile = "---\n\
title: \""+ users[i].title + "\"\n\
2023-07-20 15:34:37 -05:00
tags: ["+ users[i].tags + "]\n\
2023-07-14 05:28:12 -05:00
externalUrl: \""+ users[i].url + "\"\n\
2023-07-20 15:34:37 -05:00
date: "+ (9999 - i) + "-08-08\n\
2023-07-14 05:28:12 -05:00
showDate: false\n\
showAuthor: false\n\
showReadingTime: false\n\
showEdit: false\n\
showLikes: false\n\
showViews: false\n\
layoutBackgroundHeaderSpace: false\n\
\r---\n";
2023-07-14 06:37:16 -05:00
var dir = usersFolderPath + users[i].title.replaceAll("/", "-");
2023-07-14 05:28:12 -05:00
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
2023-10-15 12:48:20 -05:00
}
console.log(i, users[i].title);
fs.writeFileSync(dir + '/index.md', userMDFile);
for (var j in targetLangs) {
fs.writeFileSync(dir + '/index.' + targetLangs[j] + '.md', userMDFile);
}
await page.goto(users[i].url);
await page.screenshot({ path: dir + "/feature.jpg" });
2023-07-14 05:28:12 -05:00
}
await browser.close();
2023-07-14 06:37:16 -05:00
});