-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
52 lines (47 loc) · 1.71 KB
/
index.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
import { tweets } from './tweet';
import fs from 'fs';
import crypto from 'crypto';
import ora from 'ora';
import config from './config'
let ignored = 0;
let created = 0;
let counter = 0;
const length = tweets.length;
const spinner = ora('Converting Tweets to posts').start();
tweets.forEach((tweet) => {
counter++;
let imgPath;
let hasMedia = false;
if (tweet.in_reply_to_status_id || tweet.in_reply_to_user_id) {
ignored++;
} else {
created++;
let content = config.frontMatterTemplate.replace('{TWEET_DATE}', tweet.created_at);
content = content.replace('{TITLE}', `${tweet.full_text.substring(0,25)}...`).replace(/\\/g, '\');
content = content.replace('{TWEET_DATE}', tweet.created_at);
content = content.replace('{CONTENT}', tweet.full_text.replace('"', "'"));
content = content.replace('{TWEET_ID}', tweet.id);
if (tweet.extended_entities && tweet.extended_entities.media.length > 0) {
hasMedia = true;
const media = tweet.extended_entities.media[0];
imgPath = `${config.tweetMediaPath}/${tweet.id}-${media.media_url.split('media/')[1]}`;
content = content.replace('{TWEET_IMAGE}', imgPath);
} else {
content = content.replace('{TWEET_IMAGE}', 'No media found for this Tweet');
}
if (hasMedia) {
content = content + `\n![](${imgPath})`;
}
let fileName = `${crypto.createHash('sha1').update(tweet.created_at).digest('hex')}`
content = content.replace('{SLUG}', fileName);
fs.writeFile(`./posts/${fileName}.md`, content, (err) => {
if(err) {
return console.log(err);
}
});
if (counter === length) {
spinner.stop();
console.log(`${created} posts created | ${ignored} tweets ignored`);
}
}
});