refacto functions in a class

This commit is contained in:
tykayn 2020-07-19 16:15:03 +02:00
parent 648b6bd2a7
commit c163d56ffc
2 changed files with 81 additions and 84 deletions

View File

@ -1,30 +1,74 @@
// include file system module
var fs = require('fs');
exports.name = 'masto_conversion';
const fs = require('fs');
var http = require('http');
class Conversion {
hello(){
console.log('hello from conversion')
}
likes(){
// read file likes
fs.readFile('source_data/likes.json',
// callback function that is called when reading file is done
function (err, data) {
// parse json
let jsonParsedLikes = JSON.parse(data);
// access elements
const lengthOfLikes = jsonParsedLikes.orderedItems.length;
console.log('likes length', lengthOfLikes);
return jsonParsedLikes;
});
}
// read posts
filterToots(toots,options){
let minchartoots ;
if(options.filterBiggerTottsBeforeSlicing){
minchartoots = toots.filter(item => {
return item['object'].content && item['object'].content.length > options.min_length;
});
minchartoots = minchartoots.slice(0, options.max_toots);
}else{
const slice = toots.slice(0, options.max_toots);
minchartoots = slice.filter(item => {
return item['object'].content && item['object'].content.length > options.min_length;
});
}
return minchartoots
}
makeStatsForToots(tootArray){
let stats = {};
// 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 (!stats[copyFolk]) {
stats[copyFolk] = {
name : copyFolk,
counter: 0,
counterContentLength: 0,
};
}
stats[copyFolk].counter += 1;
stats[copyFolk].counterContentLength += elem['object'].content.length;
});
}
});
return this.sortTootsByLength(stats);
}
// 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>';
// };
sortTootsByLength(stats){
const statKeys = Object.keys(stats);
const arrayToSort = [];
statKeys.forEach(elem => {
arrayToSort.push(
stats[elem],
);
});
arrayToSort.sort( (a,b)=>{
return b.counter - a.counter
});
return arrayToSort;
}
}
exports.conversion = new Conversion();

71
main.js
View File

@ -1,9 +1,9 @@
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');
@ -22,20 +22,15 @@ const TemplateVars = {
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);
});
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
@ -47,62 +42,20 @@ fs.readFile('source_data/outbox.json',
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;
});
}
minchartoots = masto_converter.conversion.filterToots(jsonParsedOutbox.orderedItems, TemplateVars)
console.log('min_chars', min_length);
console.log('toots min char corresponding', minchartoots.length);
TemplateVars.outbox = minchartoots;
const example = minchartoots[1];
TemplateVars.outboxStatistics = masto_converter.conversion.makeStatsForToots(minchartoots);
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);
const example = TemplateVars.outbox[5]['object'];
TemplateVars.example = example;
console.log('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);