Skip to content

Commit

Permalink
update to user process script
Browse files Browse the repository at this point in the history
  • Loading branch information
nunocoracao committed Nov 29, 2024
1 parent 82f5a07 commit 30062aa
Show file tree
Hide file tree
Showing 54 changed files with 159 additions and 109 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: "albertolvera.com"
tags: [Sito personale]
externalUrl: "https://albertolvera.com"
weight: 81
title: "さばっこ"
tags: [Sito personale, Blog]
externalUrl: "https://sabac.co"
weight: 961
showDate: false
showAuthor: false
showReadingTime: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: "albertolvera.com"
tags: [個人サイト]
externalUrl: "https://albertolvera.com"
weight: 81
title: "さばっこ"
tags: [個人サイト, ブログ]
externalUrl: "https://sabac.co"
weight: 961
showDate: false
showAuthor: false
showReadingTime: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: "albertolvera.com"
tags: [Personal site]
externalUrl: "https://albertolvera.com"
weight: 81
title: "さばっこ"
tags: [Personal site,Blog]
externalUrl: "https://sabac.co"
weight: 961
showDate: false
showAuthor: false
showReadingTime: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: "albertolvera.com"
tags: [个人网站]
externalUrl: "https://albertolvera.com"
weight: 81
title: "さばっこ"
tags: [个人网站, 博客]
externalUrl: "https://sabac.co"
weight: 961
showDate: false
showAuthor: false
showReadingTime: false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: "Academy Customize"
tags: [Sito aziendale]
externalUrl: "https://acamize.com/"
weight: 951
showDate: false
showAuthor: false
showReadingTime: false
showEdit: false
showLikes: false
showViews: false
layoutBackgroundHeaderSpace: false
---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: "Academy Customize"
tags: [事業所]
externalUrl: "https://acamize.com/"
weight: 951
showDate: false
showAuthor: false
showReadingTime: false
showEdit: false
showLikes: false
showViews: false
layoutBackgroundHeaderSpace: false
---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: "Academy Customize"
tags: [Business site]
externalUrl: "https://acamize.com/"
weight: 951
showDate: false
showAuthor: false
showReadingTime: false
showEdit: false
showLikes: false
showViews: false
layoutBackgroundHeaderSpace: false
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: "Academy Customize"
tags: [企业网站]
externalUrl: "https://acamize.com/"
weight: 951
showDate: false
showAuthor: false
showReadingTime: false
showEdit: false
showLikes: false
showViews: false
layoutBackgroundHeaderSpace: false
---

Expand Down
8 changes: 0 additions & 8 deletions exampleSite/content/users/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@
"College club site"
]
},
{
"title": "albertolvera.com",
"url": "https://albertolvera.com",
"source": "n/a",
"tags": [
"Personal site"
]
},
{
"title": "fahru.my.id",
"url": "https://www.fahru.my.id",
Expand Down
173 changes: 88 additions & 85 deletions processUsers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require('fs');
const crypto = require("crypto")
const crypto = require("crypto")
const puppeteer = require("puppeteer");

const configDir = "./exampleSite/config/_default";
Expand All @@ -12,107 +12,114 @@ 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);
}
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);
}
var targetFile = '_index.' + targetLangs[i] + '.md';
if (indexFiles.indexOf(targetFile) == -1) {
fs.copyFileSync(usersFolderPath + '_index.md', usersFolderPath + targetFile);
}
}

let rawdata = fs.readFileSync(usersFolderPath + 'users.json');
let users = JSON.parse(rawdata);
let userDict = {}
for (var i in users) {
userDict[users[i].title.replaceAll("/", "-")] = true;
userDict[generateDirName(users[i].url)] = true;
}

const files = fs.readdirSync(usersFolderPath);


for (file in files) {
let stats = fs.statSync(usersFolderPath + files[file]);
if (files[file] != 'users.json' && files[file].indexOf("_index.") == -1) {
if (stats.isDirectory()) {
if (!userDict[files[file].replaceAll("/", "-")]) {
console.log('deleting: ', files[file]);
fs.rmdirSync(usersFolderPath + files[file], { recursive: true, force: true });
}
} else {
console.log('deleting: ', files[file]);
fs.unlinkSync(usersFolderPath + files[file]);
}
let stats = fs.statSync(usersFolderPath + files[file]);
if (files[file] != 'users.json' && files[file].indexOf("_index.") == -1) {
if (stats.isDirectory()) {
if (!userDict[files[file]]) {
console.log('deleting: ', files[file]);
fs.rmdirSync(usersFolderPath + files[file], { recursive: true, force: true });
}
} else {
console.log('deleting: ', files[file]);
fs.unlinkSync(usersFolderPath + files[file]);
}
}
}

var cache = {};

function generateDirName(seed) {
var hash = crypto.createHash('md5');
hash.update(seed);
var dir = hash.digest('hex');
return dir;
}

async function convert(text, from, to) {
var options = {
from: from,
to: to
};
if (!cache[to])
cache[to] = {};
if (cache[to][text])
return cache[to][text];
var translated_text = await translate(text, options);
cache[to][text] = translated_text.text;
return translated_text.text;
var options = {
from: from,
to: to
};
if (!cache[to])
cache[to] = {};
if (cache[to][text])
return cache[to][text];
var translated_text = await translate(text, options);
cache[to][text] = translated_text.text;
return translated_text.text;
}

async function translateFrontMatterTags(block, targetLang, tags) {
var array = block.split("\n");
var translatedBlock = "";
for (var i = 0; i < array.length; i++) {
if (array[i].indexOf(":") > -1) {
var elements = array[i].split(":");
var newElement = "";
if (elements[0].indexOf("tags") != -1) {
translatedTags = []
for (var j in tags) {
var tempTag = await convert(tags[j], defaultLang, targetLang);
translatedTags.push(tempTag);
}
var trasnlatedTagsString = translatedTags.join(", ");
newElement = elements[0] + ": [" + trasnlatedTagsString + "]";
} else {
newElement = array[i];
}
} else {
newElement = array[i];
var array = block.split("\n");
var translatedBlock = "";
for (var i = 0; i < array.length; i++) {
if (array[i].indexOf(":") > -1) {
var elements = array[i].split(":");
var newElement = "";
if (elements[0].indexOf("tags") != -1) {
translatedTags = []
for (var j in tags) {
var tempTag = await convert(tags[j], defaultLang, targetLang);
translatedTags.push(tempTag);
}
translatedBlock += newElement + "\n";
var trasnlatedTagsString = translatedTags.join(", ");
newElement = elements[0] + ": [" + trasnlatedTagsString + "]";
} else {
newElement = array[i];
}
} else {
newElement = array[i];
}
return translatedBlock;
translatedBlock += newElement + "\n";
}
return translatedBlock;
}

puppeteer
.launch({
defaultViewport: {
width: 1280,
height: 800,
},
})
.then(async (browser) => {
.launch({
defaultViewport: {
width: 1280,
height: 800,
},
})
.then(async (browser) => {

const page = await browser.newPage();
const page = await browser.newPage();

for (var i in users) {
for (var i in users) {

var userMDFile = "---\n\
var userMDFile = "---\n\
title: \""+ users[i].title + "\"\n\
tags: ["+ users[i].tags + "]\n\
externalUrl: \""+ users[i].url + "\"\n\
weight: "+ (i+1) +"\n\
weight: "+ (i + 1) + "\n\
showDate: false\n\
showAuthor: false\n\
showReadingTime: false\n\
Expand All @@ -121,26 +128,22 @@ puppeteer
showViews: false\n\
layoutBackgroundHeaderSpace: false\n\
\r---\n";

//digest the title to create a folder
var hash = crypto.createHash('md5');
hash.update(users[i].url);
var dir = usersFolderPath + hash.digest('hex');


if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
console.log(i, users[i].title);
fs.writeFileSync(dir + '/index.md', userMDFile);
for (var j in targetLangs) {
var content = await translateFrontMatterTags(userMDFile, targetLangs[j], users[i].tags);
fs.writeFileSync(dir + '/index.' + targetLangs[j] + '.md', content);
}
await page.goto(users[i].url);
await page.screenshot({ path: dir + "/feature.jpg" });
}

var dir = usersFolderPath + generateDirName(users[i].url);

if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
console.log(i, users[i].title);
fs.writeFileSync(dir + '/index.md', userMDFile);
for (var j in targetLangs) {
var content = await translateFrontMatterTags(userMDFile, targetLangs[j], users[i].tags);
fs.writeFileSync(dir + '/index.' + targetLangs[j] + '.md', content);
}
await page.goto(users[i].url);
await page.screenshot({ path: dir + "/feature.jpg" });
}
}

await browser.close();
await browser.close();

});
});

0 comments on commit 30062aa

Please sign in to comment.