blowfish/processUsers.js

75 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-07-14 05:12:21 -05:00
const fs = require('fs');
const puppeteer = require("puppeteer");
const usersFolderPath = "./exampleSite/content/users/"
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]);
2023-07-14 06:37:16 -05:00
if (files[file] != 'users.json' && files[file] != '_index.md') {
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);
await page.goto(users[i].url);
await page.screenshot({ path: dir + "/feature.jpg" });
2023-07-14 05:28:12 -05:00
}
2023-10-15 12:48:20 -05:00
2023-07-14 05:28:12 -05:00
}
await browser.close();
2023-07-14 06:37:16 -05:00
});