mastodon-archive-stats/conversion.js

167 lines
4.5 KiB
JavaScript
Raw Normal View History

2020-07-19 16:15:03 +02:00
exports.name = 'masto_conversion';
const fs = require('fs');
2020-07-19 16:15:03 +02:00
class Conversion {
2020-07-20 12:39:01 +02:00
hello() {
console.log('hello from conversion');
2020-07-19 16:15:03 +02:00
}
2020-07-20 12:39:01 +02:00
likes() {
2020-07-19 16:15:03 +02:00
// 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;
});
}
2020-07-20 12:39:01 +02:00
filterToots(toots, options) {
let minchartoots;
2020-07-20 12:39:01 +02:00
if (options.filterBiggerTottsBeforeSlicing) {
2020-07-19 16:15:03 +02:00
minchartoots = toots.filter(item => {
return item['object'].content && item['object'].content.length > options.min_length;
});
minchartoots = minchartoots.slice(0, options.max_toots);
2020-07-20 12:39:01 +02:00
} else {
2020-07-19 16:15:03 +02:00
const slice = toots.slice(0, options.max_toots);
minchartoots = slice.filter(item => {
return item['object'].content && item['object'].content.length > options.min_length;
});
}
2020-07-20 12:39:01 +02:00
minchartoots.forEach(toot => {
toot = this.findMediaUrl(toot);
toot = this.removeLastChars(toot);
return toot;
});
return minchartoots;
2020-07-19 16:15:03 +02:00
}
2020-07-20 12:39:01 +02:00
//
/**
*
* @param toot
* @returns {*}
*/
findMediaUrl(toot) {
/**
* goal:
* https://mastodon.cipherbliss.com/system/media_attachments/files/000/858/113/original/74b370672892f884.jpg?1566230144
*
* input data:
* "attributedTo":"https://mastodon.cipherbliss.com/users/tykayn",
*
* "attachment":[{"type":"Document",
* "mediaType":"image/png","url":"media_attachments/files/000/872/910/original/c82b422f302b8ec9.png",
* "name":null,
* "blurhash":"UnSOjgo~ysWAVYWBkWjXu5axVrjckqoze?Rk"}],
*
* we use the attribuedTo property to find instance, and map the url to the instance url, and add this property to the attachment
*/
if (toot['object'].attributedTo) {
let splitted = toot['object'].attributedTo.split('/');
let instanceUrl = splitted[2];
toot.instanceUrl = 'https://' + instanceUrl;
toot.attachment = toot['object'].attachment.map(att => {
att.href = toot.instanceUrl + '/system/' + att.url;
return att;
});
}
return toot;
}
removeLastChars(toot) {
toot['object'].content = toot['object'].content.trim();
return toot;
}
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,
href : tag.href,
2020-07-19 16:15:03 +02:00
counter: 0,
};
}
2020-07-20 12:39:01 +02:00
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] = {
user : this.urlToUser(copyFolk),
name : copyFolk,
counter : 0,
counterContentLength: 0,
};
}
stats.recievers[copyFolk].counter++;
stats.recievers[copyFolk].counterContentLength += elem['object'].content.length;
});
}
});
2020-07-19 16:46:34 +02:00
2020-07-20 12:39:01 +02:00
console.log('stats.hashtags', stats.hashtags[0]);
stats = {
recievers: this.sortTootsByLength(stats.recievers),
hashtags : this.sortTootsByLength(stats.hashtags),
};
return stats;
2020-07-19 16:15:03 +02:00
}
2020-07-20 12:39:01 +02:00
sortTootsByLength(stats) {
2020-07-19 16:15:03 +02:00
const statKeys = Object.keys(stats);
const arrayToSort = [];
statKeys.forEach(elem => {
arrayToSort.push(
stats[elem],
);
});
2020-07-20 12:39:01 +02:00
arrayToSort.sort((a, b) => {
return b.counter - a.counter;
2020-07-19 16:15:03 +02:00
});
return arrayToSort;
}
2020-07-20 12:39:01 +02:00
urlToUser(url) {
let sliceOfSlashes = url.split('/');
let userObject = {
url : url,
username: sliceOfSlashes[sliceOfSlashes.length - 1],
};
return userObject;
}
filterOnlyTootsWithMedias(tootList) {
console.log('filterOnlyTootsWithMedias')
return tootList.filter(toot => {
return toot['object'].attachment && toot['object'].attachment.length
});
}
2020-07-19 16:15:03 +02:00
}
2020-07-20 12:39:01 +02:00
2020-07-19 16:15:03 +02:00
exports.conversion = new Conversion();