diff --git a/main.js b/main.js index 3368eee..63b53aa 100755 --- a/main.js +++ b/main.js @@ -10,8 +10,9 @@ var fs = require('fs'); var listenPort = 8088; var jsonParsedLikes, jsonParsedOutbox; // const min_length = 1050; // filter only long toots -const min_length = 1; // filter only long toots -const max_toots = 20; // filter only long toots +const min_length = 500; // filter only long toots +const max_toots = 100; // filter only long toots +const filterBiggerTottsBeforeSlicing = true; // filter only long toots const TemplateVars = { pageTitle : 'Mastodon export converter to HTML', likes : jsonParsedLikes, @@ -35,44 +36,37 @@ fs.readFile('source_data/likes.json', 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; + let minchartoots ; // parse json - jsonParsedOutbox = JSON.parse(jsonData); + jsonParsedOutbox = JSON.parse(data); // access elements console.log('outbox toots length', jsonParsedOutbox.orderedItems.length); TemplateVars.outboxTotalLength = jsonParsedOutbox.orderedItems.length; - const slice = jsonParsedOutbox.orderedItems.slice(0, max_toots); + 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; + }); + } - const minchartoots = slice.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; const example = minchartoots[1]; - // make statistics on who do we talk to, based on the cc field - minchartoots.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; - }); - } - }); + statsForOutboxToots(minchartoots); + const statKeys = Object.keys(TemplateVars.outboxStatistics); const arrayToSort = []; statKeys.forEach(elem => { @@ -91,7 +85,24 @@ fs.readFile('source_data/outbox.json', 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); diff --git a/views/index.pug b/views/index.pug index 02f6646..4738a42 100644 --- a/views/index.pug +++ b/views/index.pug @@ -9,10 +9,20 @@ html(lang="en") h2 Statistics fieldset.stats h3 You did sent messages to these people - each someone in outboxStatistics - .name - a(href=someone.name)=someone.name - .counter=someone.counter + table + thead + tr + th= "name" + th= "toots to" + th= "toots lengh sum" + tbody + each someone in outboxStatistics + tr + td.name + a(href=someone.name)=someone.name + td.counter=someone.counter + td.counter=someone.counterContentLength + h2 #{outbox.length} of Messages #{outboxTotalLength} in your outbox.First #{max_toots} toots, filtered by a minimal length of #{min_length} characters of content. .columns-area__panels__main div.column