mastodon-archive-stats/main.js

133 lines
4.4 KiB
JavaScript
Executable File

console.log('=================================================================');
console.log('hello and welcome to the file converter from Mastodon outbox JSON');
console.log('=================================================================');
// exec conversion.js
const express = require('express');
const pug = require('pug');
const app = express();
var fs = require('fs');
var listenPort = 8088;
var jsonParsedLikes, jsonParsedOutbox;
// const min_length = 1050; // filter only long toots
const min_length = 500; // filter only long toots
const max_toots = 100; // filter only long toots
const filterBiggerTottsBeforeSlicing = true; // filter only long toots
const TemplateVars = {
pageTitle : 'Mastodon export converter to HTML',
likes : jsonParsedLikes,
outbox : jsonParsedOutbox,
outboxStatistics: {},
outbox_all : jsonParsedOutbox,
min_length ,
max_toots ,
};
// read file likes
fs.readFile('source_data/likes.json',
// callback function that is called when reading file is done
function (err, data) {
// parse json
jsonParsedLikes = JSON.parse(data);
// access elements
const lengthOfLikes = jsonParsedLikes.orderedItems.length;
console.log('likes length', lengthOfLikes);
const example = jsonParsedLikes.orderedItems[10];
console.log('example', example);
});
fs.readFile('source_data/outbox.json',
// callback function that is called when reading file is done
function (err, data) {
let minchartoots ;
// parse json
jsonParsedOutbox = JSON.parse(data);
// access elements
console.log('outbox toots length', jsonParsedOutbox.orderedItems.length);
TemplateVars.outboxTotalLength = jsonParsedOutbox.orderedItems.length;
if(filterBiggerTottsBeforeSlicing){
minchartoots = jsonParsedOutbox.orderedItems.filter(item => {
return item['object'].content && item['object'].content.length > min_length;
});
minchartoots = minchartoots.slice(0, max_toots);
}else{
const slice = jsonParsedOutbox.orderedItems.slice(0, max_toots);
minchartoots = slice.filter(item => {
return item['object'].content && item['object'].content.length > min_length;
});
}
console.log('min_chars', min_length);
console.log('toots min char corresponding', minchartoots.length);
TemplateVars.outbox = minchartoots;
const example = minchartoots[1];
statsForOutboxToots(minchartoots);
const statKeys = Object.keys(TemplateVars.outboxStatistics);
const arrayToSort = [];
statKeys.forEach(elem => {
arrayToSort.push(
TemplateVars.outboxStatistics[elem],
);
});
arrayToSort.sort( (a,b)=>{
return b.counter - a.counter
});
TemplateVars.outboxStatistics = arrayToSort;
console.log('TemplateVars.outbox.length', TemplateVars.outbox.length, TemplateVars.outbox[5]['object'].content.length);
TemplateVars.example = example;
fs.writeFile('output/statistics.json', JSON.stringify(TemplateVars.outboxStatistics), errfileHandler );
});
const statsForOutboxToots = (tootArray)=>{
// make statistics on who do we talk to, based on the cc field
tootArray.forEach(elem => {
if (elem['object'].cc) {
elem['object'].cc.forEach(copyFolk => {
if (!TemplateVars.outboxStatistics[copyFolk]) {
TemplateVars.outboxStatistics[copyFolk] = {
name : copyFolk,
counter: 0,
counterContentLength: 0,
};
}
TemplateVars.outboxStatistics[copyFolk].counter += 1;
TemplateVars.outboxStatistics[copyFolk].counterContentLength += elem['object'].content.length;
});
}
});
};
const errfileHandler = (err) => {
if (err)
console.log(err);
else {
console.log("File statistics written successfully\n");
}
}
app.use(express.static('public'));
app.set('view engine', 'pug');
app.get('/', (req, res) => {
const html = pug.render('index.pug', TemplateVars);
fs.writeFile('output/my_toots.html', html, errfileHandler );
res.render('index.pug', TemplateVars);
});
app.listen(listenPort, () => console.log(`Server is live at http://localhost:${listenPort}`));
console.log('=================================================================');
console.log('made by Tykayn from CipherBliss - https://mastodon.cipherbliss.com/@tykayn');
console.log('=================================================================');