Updated datetime syntax for twitter frontmatter.

This commit is contained in:
Chris 2018-04-14 19:35:24 -04:00
parent 1b7fcfb67a
commit 09f9e110ec

View file

@ -14,13 +14,20 @@ const client = new Twitter({
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
}); });
// https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url/22648406#22648406
function isURL(str) {
var 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]*)?$';
var url = new RegExp(urlRegex, 'i');
return str.length < 2083 && url.test(str);
}
const run = async () => { const run = async () => {
console.info(`Getting user timeline information for ${process.env.TWITTER_SCREEN_NAME}.`) console.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 => { 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 || 5 + 1).map((x, index) => { return tweets.slice(0, process.env.TWITTER_TIMELINE_COUNT + 1 || 50 + 1).map((x, index) => {
return { return {
id: x.id_str, id: x.id_str,
date: dateFormat(x.created_at, 'mmmm d, yyyy'), date: new Date(x.created_at),
author: x.user.screen_name, author: x.user.screen_name,
message: x.text message: x.text
} }
@ -30,10 +37,15 @@ const run = async () => {
console.info(`Got ${tweets.length} tweets from the Twitter API.`) console.info(`Got ${tweets.length} tweets from the Twitter API.`)
// Write each tweet as entry content. // Write each tweet as entry content.
return tweets.forEach(async (x) => { return tweets.forEach(async (x) => {
if (isURL(x.message)) {
console.warn(`Skipping frontmatter generation for tweet ${x.id} -- just a URL.`)
return
}
let tweetRef = `tweet_${x.id}` let tweetRef = `tweet_${x.id}`
let frontmatterPath = `${outputDirectory}/${tweetRef}/index.md` let frontmatterPath = `${outputDirectory}/${tweetRef}/index.md`
let frontmatterContents = `+++ let frontmatterContents = `+++
date = "${x.date}" date = "${x.date.toISOString()}"
title = "Tweet ${x.id} by ${x.author}" title = "Tweet ${x.id} by ${x.author}"
twitter = true twitter = true
twitterId = ${x.id} twitterId = ${x.id}