Created
December 22, 2021 21:49
-
-
Save roobottom/4ae63d8f27eab60e3cc028f3f140032b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const tweets = require('./tweet.js') | |
const moment = require('moment') | |
const matter = require('gray-matter') | |
const fs = require('fs') | |
for (t of tweets) { | |
//ignore any replies or old-style RT | |
if(t.tweet.in_reply_to_user_id_str === undefined && !t.tweet.full_text.startsWith('RT @')) { | |
//does this tweet have media? | |
let allMedia = [] | |
if(t.tweet.entities.media) { | |
for (media of t.tweet.entities.media) { | |
let file = media.media_url.substring(media.media_url.lastIndexOf('/') + 1) | |
allMedia.push({ url: t.tweet.id + '-' + file }) | |
} | |
} | |
//does this tweet have links? | |
let allLinks = [] | |
if(t.tweet.entities.urls) { | |
for (url of t.tweet.entities.urls) { | |
allLinks.push(url.expanded_url) | |
} | |
} | |
//does this tweet mention a user? | |
let allMentions = [] | |
if(t.tweet.entities.user_mentions) { | |
for(mention of t.tweet.entities.user_mentions) { | |
allMentions.push(`@${mention.screen_name}`) | |
} | |
} | |
const photos = allMedia.length === 0 ? {} : { photo: allMedia } | |
const links = allLinks.length === 0 ? {} : { links: allLinks } | |
const mentions = allMentions.length === 0 ? {} : { mentions: allMentions } | |
//remove any links | |
let fileText = t.tweet.full_text.replace(/(?:https?|ftp):\/\/[\n\S]+/g, '') | |
//replace @mentions with linked mentioned | |
for (mention of allMentions) { | |
fileText = fileText.replace(new RegExp(mention, "gi"), `[${mention}](https://twitter.com/${mention})`) | |
} | |
let fileContent = matter.stringify(fileText, { | |
date: moment(t.tweet.created_at).format('YYYY-MM-DDTHH:MM:ssZ'), | |
...photos, | |
...links, | |
...mentions | |
}) | |
fs.writeFileSync(`${__dirname}/files/${moment(t.tweet.created_at).format('YYYY-MM-DD')}-${t.tweet.id}.md`, fileContent) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment