Created
September 15, 2020 07:53
-
-
Save GraemeFulton/4a34ecdbb53c8f4af2ec862939e43f40 to your computer and use it in GitHub Desktop.
Get twitter link data
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 _ = require('underscore'); | |
var unescape = require('lodash.unescape'); | |
getTwitterLink(link){ | |
if (link.indexOf('twitter.com') > -1 && link.indexOf('/status') > -1) { | |
//use twitter publish oembed thing | |
var twitterPublishUrl = 'https://publish.twitter.com/oembed?url=' + link + '&omit_script=1' | |
var meta = await fetch(twitterPublishUrl).then(res => res.json()) | |
.then(json => { return json }); | |
//clean the content (convert url encoded characters) | |
let content = meta.html.replaceAll(/(?:https?|ftp):\/\/[\n\S]+/g, '') | |
content = striptags(content) | |
//use underscore | |
content = _.unescape(content) | |
//use loadash (don't know if you need both!) | |
content = unescape(content) | |
//remove weird dash things | |
content = content.replaceAll('—', '–') | |
let author = meta.author_name | |
let title = meta.author_name + ' on Twitter' | |
let image = 'https://help.twitter.com/content/dam/help-twitter/logos/card_wide_blue.png' | |
let description = content | |
let metadata = {author:author?author:'No author found', title:title?title:'No Title Found', image, description?description:'No description found'} | |
return metadata | |
} else{ | |
//throw error not found | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment