mastodon-archive-stats/main.js

96 lines
3.3 KiB
JavaScript
Raw Normal View History

console.log('=================================================================');
console.log('hello and welcome to the file converter from Mastodon outbox JSON');
console.log('=================================================================');
const express = require('express');
2020-07-19 16:15:03 +02:00
2020-07-19 15:48:31 +02:00
const pug = require('pug');
const app = express();
var fs = require('fs');
var listenPort = 8088;
var jsonParsedLikes, jsonParsedOutbox;
2020-07-18 17:40:15 +02:00
// const min_length = 1050; // filter only long toots
2020-07-20 12:39:01 +02:00
const min_length = 1; // filter only long toots
const max_toots = 10; // filter only long toots
2020-07-19 16:27:48 +02:00
const filterBiggerTottsBeforeSlicing = false; // filter only long toots
2020-07-20 12:39:01 +02:00
const filterOnlyHavingMedias = true; // filter only toots having medias
const writeStatsJson = false; // filter only toots having medias
2020-07-20 14:52:44 +02:00
const showMostRecentTootsOnTop = true; // filter only toots having medias
const TemplateVars = {
pageTitle : 'Mastodon export converter to HTML',
likes : jsonParsedLikes,
outbox : jsonParsedOutbox,
outboxStatistics: {},
outbox_all : jsonParsedOutbox,
2020-07-20 12:39:01 +02:00
min_length,
max_toots,
filterOnlyHavingMedias,
filterBiggerTottsBeforeSlicing,
writeStatsJson,
2020-07-20 14:52:44 +02:00
showMostRecentTootsOnTop
};
2020-07-19 16:15:03 +02:00
const masto_converter = require('./conversion');
console.log('masto_converter', masto_converter);
masto_converter.conversion.hello();
jsonParsedLikes = masto_converter.conversion.likes();
fs.readFile('source_data/outbox.json',
// callback function that is called when reading file is done
function (err, data) {
2020-07-20 12:39:01 +02:00
let toots;
// parse json
2020-07-19 15:57:09 +02:00
jsonParsedOutbox = JSON.parse(data);
2020-07-20 12:39:01 +02:00
toots = jsonParsedOutbox.orderedItems;
// access elements
2020-07-20 12:39:01 +02:00
console.log('outbox toots length', toots.length);
2020-07-20 12:39:01 +02:00
TemplateVars.outboxTotalLength = toots.length;
toots = jsonParsedOutbox.orderedItems;
if (filterOnlyHavingMedias) {
toots = masto_converter.conversion.filterOnlyTootsWithMedias(toots);
console.log('toots.length only attachements', toots.length );
}
toots = masto_converter.conversion.filterToots(toots, TemplateVars);
2020-07-19 15:48:31 +02:00
console.log('min_chars', min_length);
2020-07-20 12:39:01 +02:00
console.log('toots min char corresponding', toots.length);
TemplateVars.outbox = toots;
TemplateVars.outboxStatistics = masto_converter.conversion.makeStatsForToots(toots);
2020-07-18 17:40:15 +02:00
2020-07-19 16:46:34 +02:00
const example = TemplateVars.outbox[1]['object'];
TemplateVars.example = example;
2020-07-20 12:39:01 +02:00
console.log('example', example)
2020-07-19 15:48:31 +02:00
2020-07-20 12:39:01 +02:00
if (writeStatsJson) {
fs.writeFile('output/statistics.json', JSON.stringify(TemplateVars.outboxStatistics), errfileHandler);
}
});
2020-07-19 15:48:31 +02:00
const errfileHandler = (err) => {
if (err)
console.log(err);
else {
2020-07-20 12:39:01 +02:00
console.log('File statistics written successfully\n');
2020-07-19 15:48:31 +02:00
}
2020-07-20 12:39:01 +02:00
};
2020-07-19 15:48:31 +02:00
app.use(express.static('public'));
app.set('view engine', 'pug');
app.get('/', (req, res) => {
2020-07-19 15:48:31 +02:00
const html = pug.render('index.pug', TemplateVars);
2020-07-20 12:39:01 +02:00
fs.writeFile('output/my_toots.html', html, errfileHandler);
2020-07-19 15:48:31 +02:00
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('=================================================================');