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 exports.name = 'masto_conversion';
var fs = require('fs'); 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) { sortTootsByLength(stats){
// var html = buildHtml(req); const statKeys = Object.keys(stats);
// const arrayToSort = [];
// res.writeHead(200, { statKeys.forEach(elem => {
// 'Content-Type': 'text/html', arrayToSort.push(
// 'Content-Length': html.length, stats[elem],
// 'Expires': new Date().toUTCString() );
// }); });
// res.end(html); arrayToSort.sort( (a,b)=>{
// }).listen(8080); return b.counter - a.counter
// });
// function buildHtml(req) { return arrayToSort;
// var header = ''; }
// var body = ''; }
// exports.conversion = new Conversion();
// // concatenate header string
// // concatenate body string
//
// return '<!DOCTYPE html>'
// + '<html><head>' + header + '</head><body>' + body + '</body></html>';
// };

71
main.js
View File

@ -1,9 +1,9 @@
console.log('================================================================='); console.log('=================================================================');
console.log('hello and welcome to the file converter from Mastodon outbox JSON'); console.log('hello and welcome to the file converter from Mastodon outbox JSON');
console.log('================================================================='); console.log('=================================================================');
// exec conversion.js
const express = require('express'); const express = require('express');
const pug = require('pug'); const pug = require('pug');
const app = express(); const app = express();
var fs = require('fs'); var fs = require('fs');
@ -22,20 +22,15 @@ const TemplateVars = {
min_length , min_length ,
max_toots , 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', fs.readFile('source_data/outbox.json',
// callback function that is called when reading file is done // 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); console.log('outbox toots length', jsonParsedOutbox.orderedItems.length);
TemplateVars.outboxTotalLength = jsonParsedOutbox.orderedItems.length; TemplateVars.outboxTotalLength = jsonParsedOutbox.orderedItems.length;
if(filterBiggerTottsBeforeSlicing){ minchartoots = masto_converter.conversion.filterToots(jsonParsedOutbox.orderedItems, TemplateVars)
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('min_chars', min_length);
console.log('toots min char corresponding', minchartoots.length); console.log('toots min char corresponding', minchartoots.length);
TemplateVars.outbox = minchartoots; TemplateVars.outbox = minchartoots;
const example = minchartoots[1]; TemplateVars.outboxStatistics = masto_converter.conversion.makeStatsForToots(minchartoots);
statsForOutboxToots(minchartoots); const example = TemplateVars.outbox[5]['object'];
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; TemplateVars.example = example;
console.log('example', example)
fs.writeFile('output/statistics.json', JSON.stringify(TemplateVars.outboxStatistics), errfileHandler ); 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) => { const errfileHandler = (err) => {
if (err) if (err)
console.log(err); console.log(err);