2023-01-23 02:38:13 +01:00
|
|
|
require("checkenv").check();
|
|
|
|
const logger = require("fancy-log");
|
|
|
|
const Twitter = require("twitter");
|
|
|
|
const fs = require("fs-extra");
|
2018-01-10 04:16:44 +01:00
|
|
|
|
2023-01-23 02:38:13 +01:00
|
|
|
const outputDirectory = "../../../site/content/entry";
|
2018-01-07 06:30:04 +01:00
|
|
|
|
2018-01-10 04:16:44 +01:00
|
|
|
const client = new Twitter({
|
2023-01-23 02:38:13 +01:00
|
|
|
consumer_key: process.env.TWITTER_CONSUMER_KEY,
|
|
|
|
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
|
|
|
|
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
|
|
|
|
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
|
2018-01-10 04:16:44 +01:00
|
|
|
});
|
|
|
|
|
2018-04-15 01:35:24 +02:00
|
|
|
// https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url/22648406#22648406
|
2023-01-23 02:38:13 +01:00
|
|
|
/** @param {string} str */
|
2018-04-15 01:35:24 +02:00
|
|
|
function isURL(str) {
|
2023-01-23 02:38:13 +01:00
|
|
|
const urlRegex =
|
|
|
|
"^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$";
|
|
|
|
const url = new RegExp(urlRegex, "i");
|
|
|
|
return str.length < 2083 && url.test(str);
|
2018-04-15 01:35:24 +02:00
|
|
|
}
|
|
|
|
|
2023-01-23 02:38:13 +01:00
|
|
|
async function run() {
|
|
|
|
logger.info(
|
|
|
|
`Getting user timeline information for ${process.env.TWITTER_SCREEN_NAME}.`
|
|
|
|
);
|
|
|
|
let tweets = await client
|
|
|
|
.get("statuses/user_timeline", {
|
|
|
|
screen_name: process.env.TWITTER_SCREEN_NAME,
|
|
|
|
})
|
|
|
|
.then((tweets) => {
|
|
|
|
return tweets
|
|
|
|
.slice(0, process.env.TWITTER_TIMELINE_COUNT + 1 || 50 + 1)
|
|
|
|
.map((x, index) => {
|
|
|
|
return {
|
|
|
|
id: x.id_str,
|
|
|
|
date: new Date(x.created_at),
|
|
|
|
author: x.user.screen_name,
|
|
|
|
message: x.text,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
logger.info(`Got ${tweets.length} tweets from the Twitter API.`);
|
|
|
|
// Write each tweet as entry content.
|
|
|
|
return tweets.forEach(async (x) => {
|
|
|
|
if (isURL(x.message)) {
|
|
|
|
logger.warn(
|
|
|
|
`Skipping frontmatter generation for tweet ${x.id} -- just a URL.`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2018-04-14 08:06:58 +02:00
|
|
|
|
2023-01-23 02:38:13 +01:00
|
|
|
let tweetRef = `tweet_${x.id}`;
|
|
|
|
let frontmatterPath = `${outputDirectory}/${tweetRef}/index.md`;
|
|
|
|
let frontmatterContents = `+++
|
2018-04-15 01:35:24 +02:00
|
|
|
date = "${x.date.toISOString()}"
|
2018-04-14 08:06:58 +02:00
|
|
|
title = "Tweet ${x.id} by ${x.author}"
|
|
|
|
twitter = true
|
|
|
|
twitterId = ${x.id}
|
|
|
|
twitterUrl = "https://twitter.com/${x.author}/status/${x.id}"
|
|
|
|
+++
|
|
|
|
|
2023-01-23 02:38:13 +01:00
|
|
|
${x.message}`;
|
|
|
|
await fs.outputFile(frontmatterPath, frontmatterContents);
|
|
|
|
logger.info(`Wrote '${frontmatterPath}' to filesystem.`);
|
|
|
|
});
|
2018-04-14 08:06:58 +02:00
|
|
|
}
|
|
|
|
|
2023-01-23 02:38:13 +01:00
|
|
|
run().catch((err) => {
|
|
|
|
logger.error(err);
|
|
|
|
process.exit(1);
|
2018-01-10 04:16:44 +01:00
|
|
|
});
|