2020-07-18 17:03:17 +02:00
|
|
|
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');
|
2020-07-18 17:03:17 +02:00
|
|
|
const app = express();
|
|
|
|
var fs = require('fs');
|
2020-10-16 16:54:20 +02:00
|
|
|
var listenPort = 8080;
|
2020-07-18 17:03:17 +02:00
|
|
|
var jsonParsedLikes, jsonParsedOutbox;
|
2020-10-16 15:14:48 +02:00
|
|
|
/**
|
|
|
|
* export configuration.
|
|
|
|
* You can filter the export in the following vars
|
|
|
|
*/
|
2020-10-16 16:54:20 +02:00
|
|
|
const min_length = 0; // minmum character length of toots to display
|
2020-10-16 15:14:48 +02:00
|
|
|
const max_toots = Infinity; // maximum length
|
|
|
|
const filterBiggerTootsBeforeSlicing = false; // filter only long toots
|
2020-10-16 16:54:20 +02:00
|
|
|
const filterOnlyHavingMedias = false; // filter only toots having medias
|
2020-07-20 17:00:35 +02:00
|
|
|
const displayMedias = false; // filter only toots having medias
|
2020-10-16 16:54:20 +02:00
|
|
|
const writeStatsJson = false; // write json export file about statistics
|
|
|
|
const showMostRecentTootsOnTop = true; // sorting order
|
2020-07-18 17:03:17 +02:00
|
|
|
const TemplateVars = {
|
2020-10-16 15:14:48 +02:00
|
|
|
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,
|
2020-10-16 15:14:48 +02:00
|
|
|
filterBiggerTootsBeforeSlicing,
|
2020-07-20 12:39:01 +02:00
|
|
|
writeStatsJson,
|
2020-07-20 17:00:35 +02:00
|
|
|
showMostRecentTootsOnTop,
|
|
|
|
displayMedias,
|
2020-07-18 17:30:53 +02:00
|
|
|
};
|
2020-07-18 17:03:17 +02:00
|
|
|
|
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();
|
|
|
|
|
2020-07-18 17:03:17 +02:00
|
|
|
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;
|
2020-07-18 17:03:17 +02:00
|
|
|
// 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;
|
2020-07-18 17:03:17 +02:00
|
|
|
// access elements
|
2020-07-20 12:39:01 +02:00
|
|
|
console.log('outbox toots length', toots.length);
|
2020-07-18 17:03:17 +02:00
|
|
|
|
2020-07-20 12:39:01 +02:00
|
|
|
TemplateVars.outboxTotalLength = toots.length;
|
|
|
|
toots = jsonParsedOutbox.orderedItems;
|
|
|
|
if (filterOnlyHavingMedias) {
|
|
|
|
toots = masto_converter.conversion.filterOnlyTootsWithMedias(toots);
|
2020-07-20 17:00:35 +02:00
|
|
|
console.log('toots.length only attachements', toots.length);
|
2020-07-20 12:39:01 +02:00
|
|
|
}
|
|
|
|
toots = masto_converter.conversion.filterToots(toots, TemplateVars);
|
2020-07-19 15:48:31 +02:00
|
|
|
|
2020-07-18 17:30:53 +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'];
|
2020-07-18 17:03:17 +02:00
|
|
|
TemplateVars.example = example;
|
2020-07-20 17:00:35 +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-18 17:03:17 +02:00
|
|
|
});
|
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
|
|
|
|
2020-07-18 17:03:17 +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 17:00:35 +02:00
|
|
|
// fs.writeFile('output/my_toots.html', html, errfileHandler);
|
2020-07-19 15:48:31 +02:00
|
|
|
|
2020-07-18 17:30:53 +02:00
|
|
|
res.render('index.pug', TemplateVars);
|
2020-07-18 17:03:17 +02:00
|
|
|
|
2020-07-20 17:00:35 +02:00
|
|
|
masto_converter.conversion.fetchUserInfo();
|
2020-07-18 17:03:17 +02:00
|
|
|
});
|
2020-07-20 17:00:35 +02:00
|
|
|
masto_converter.conversion.fetchUserInfo();
|
2020-07-18 17:03:17 +02:00
|
|
|
|
|
|
|
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('=================================================================');
|