exports.name = 'masto_conversion'; const fs = require('fs'); 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; }); } 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 = { recievers: {}, hashtags : {}}; // make statistics on who do we talk to, based on the cc field tootArray.forEach(elem => { // stats on hashtags if (elem['object'].tag) { elem['object'].tag.forEach(tag => { if(tag.type === 'Hashtag'){ if(!stats.hashtags[tag.name]){ stats.hashtags[tag.name] = { name : tag.name, counter : 0, } } stats.hashtags[tag.name].counter++; } }) } // stats on recievers of toots if (elem['object'].cc) { elem['object'].cc.forEach(copyFolk => { if (!stats.recievers[copyFolk]) { stats.recievers[copyFolk] = { name : copyFolk, counter: 0, counterContentLength: 0, }; } stats.recievers[copyFolk].counter++; stats.recievers[copyFolk].counterContentLength += elem['object'].content.length; }); } }); console.log('stats.hashtags', stats.hashtags); stats = { recievers : this.sortTootsByLength(stats.recievers), hashtags : this.sortTootsByLength(stats.hashtags), }; return stats ; } 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();