-
Notifications
You must be signed in to change notification settings - Fork 40
/
parse-mastodon.js
63 lines (42 loc) · 1.25 KB
/
parse-mastodon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const fs = require('fs');
const data = require('./outbox.json');
var posts = [];
data.orderedItems.forEach(item => {
const post = item;
if (post.object.type == "Note") {
// if this was a reply get the screen name
const re = /\/users\/(.+)\/statuses\//gm;
const reply = post?.object?.inReplyToAtomUri;
let screenName = null;
if(reply) {
for(const match of reply.matchAll(re)) {
screenName = match[1];
};
}
// gather any media
let media = [];
if(post.object.attachment.length) {
post.object.attachment.forEach(attachment => {
media.push({
type: attachment.mediaType,
url: `https://cdn.masto.host${attachment.url}`
})
});
}
posts.push({
"id": post.object.url.split("@philhawksworth/")[1],
"platform": "mastodon",
"created_at": new Date(post.published),
"full_text": post.object.content,
"in_reply_to_status_id": post.object.inReplyToAtomUri || null,
"in_reply_to_screen_name": screenName || null,
"media" : media
});
}
})
fs.writeFile('src/site/_data/toots.json', JSON.stringify(posts.reverse()), err => {
if (err) {
console.error(err);
}
console.error("Notes saved");
});