node server displaying biggest toots

This commit is contained in:
Baptiste Lemoine 2020-07-18 17:03:17 +02:00
commit bbb7976a31
9 changed files with 1527 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
source_data/*.json
.idea
node_modules

5
README.md Executable file
View File

@ -0,0 +1,5 @@
# NodeJS Converter of Mastodon export to HTML files
install dependencies
Run the main.js file
It will create a file showing your longest posts.

30
conversion.js Executable file
View File

@ -0,0 +1,30 @@
// include file system module
var fs = require('fs');
var http = require('http');
// read posts
// http.createServer(function (req, res) {
// var html = buildHtml(req);
//
// res.writeHead(200, {
// 'Content-Type': 'text/html',
// 'Content-Length': html.length,
// 'Expires': new Date().toUTCString()
// });
// res.end(html);
// }).listen(8080);
//
// function buildHtml(req) {
// var header = '';
// var body = '';
//
// // concatenate header string
// // concatenate body string
//
// return '<!DOCTYPE html>'
// + '<html><head>' + header + '</head><body>' + body + '</body></html>';
// };

65
main.js Executable file
View File

@ -0,0 +1,65 @@
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 app = express();
var fs = require('fs');
var listenPort = 8088;
var jsonParsedLikes, jsonParsedOutbox;
const min_length = 1000; // filter only long toots
const TemplateVars = {
pageTitle: "Mastodon export converter to HTML",
likes: jsonParsedLikes,
outbox: jsonParsedOutbox,
outbox_all: jsonParsedOutbox,
min_length: min_length,
}
// 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[0];
console.log('example', example)
});
fs.readFile('source_data/outbox.json',
// callback function that is called when reading file is done
function (err, data) {
// json data
var jsonData = data;
// parse json
jsonParsedOutbox = JSON.parse(jsonData);
// access elements
console.log('outbox toots length', jsonParsedOutbox.orderedItems.length);
const example = jsonParsedOutbox.orderedItems[0];
const minchartoots = jsonParsedOutbox.orderedItems.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 ;
console.log('TemplateVars.outbox.length', TemplateVars.outbox.length,TemplateVars.outbox[5]['object'].content.length )
TemplateVars.example = example;
});
app.use(express.static('public'));
app.set('view engine', 'pug');
app.get('/', (req, res) => {
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('=================================================================');

0
output/likes.html Normal file
View File

0
output/outbox.html Normal file
View File

7
package.json Normal file
View File

@ -0,0 +1,7 @@
{
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.4",
"pug": "^3.0.0"
}
}

16
views/index.pug Normal file
View File

@ -0,0 +1,16 @@
doctype html
html(lang="en")
head
title= pageTitle
style
body
h1=pageTitle
h2 #{outbox.length} Messages in your outbox. Filtered by a minimal length of #{min_length}
each oredredItem in outbox
fieldset
a(href=oredredItem.object.id)
=oredredItem.object.published
div.published=oredredItem.object.published
blockquote.published(unescaped!=oredredItem.object.content)
//- each object in outbox

1401
yarn.lock Normal file

File diff suppressed because it is too large Load Diff