Last active
December 22, 2015 02:08
-
-
Save jlord/6400975 to your computer and use it in GitHub Desktop.
Parsing tweet mentions and links from Spreadsheet data (which is via Sheetsee.js and IFTTT.com)
* Sorry about the crazy indents, I couldn't get Gists to do them correctly. Who makes this thing even!?
This file contains hidden or 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
<!-- a placeholder in the body of your page --> | |
<div id="twitterTweet"></div> | |
<!-- a template before /body closes --> | |
<script id="twitterTweet" type="text/html"> | |
<table> | |
{{#rows}} | |
<tr><td>{{date}}</td></tr> | |
<tr><td>{{{tweet}}}</td> | |
{{/rows}} | |
</table> | |
</script> | |
<!-- include the js --> | |
<script type="text/javascript" src="tweet.js"></script> | |
<script type="text/javascript" src="tabletop.js"></script> | |
<script type="text/javascript" src="ICanHaz.js"></script> |
This file contains hidden or 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
document.addEventListener('DOMContentLoaded', function() { | |
var key = yourSpreadsheetsKey | |
Tabletop.init( { key: key, callback: renderTweets, simpleSheet: true } ) | |
}) | |
renderTweets = function(data) { | |
var data = data | |
var twitterData = data.reverse() // so you display latest tweet | |
var tweet = findLinks(twitterData[0]) | |
twitterData[0].tweet = tweet | |
// render template | |
var twitterTweet = ich.twitterTweet({ | |
"rows": twitterData[0] | |
}) | |
document.getElementById('twitterTweet').innerHTML = twitterTweet | |
} | |
// find any links in the tweet, | |
// then call function to find mentions | |
function findLinks(tweet) { | |
if (tweet.tweet) { | |
var linkPattern = /(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi | |
var tweet = tweet.tweet | |
var links = tweet.match(linkPattern) | |
var linkLinks = linkLink(links) | |
var newTweet = injectLinks(tweet, links, linkLinks) | |
return tweetMentions(newTweet) | |
} | |
} | |
// find mentions | |
function tweetMentions(tweet) { | |
if (tweet) { | |
var mentionPattern = /\B@[a-z0-9_-]+/gi | |
var mentions = tweet.match(mentionPattern) | |
var linkMentions = linkMention(mentions) | |
var newTweet = injectLinks(tweet, mentions, linkMentions) | |
} | |
return newTweet | |
} | |
// when you have mentions, | |
// wrap them in html links | |
function linkMention(mentions) { | |
var linkMentions = [] | |
mentions.forEach(function(mention){ | |
mention = '<a href="http://www.twittter.com/' + mention + '" target="_blank">' + mention + '</a>' | |
linkMentions.push(mention) | |
}) | |
return linkMentions | |
} | |
// when you have links, | |
// wrap them in html links | |
function linkLink(links) { | |
var linkLinks = [] | |
links.forEach(function(link) { | |
link = '<a href="' + link + '" target="_blank">' + link + '</a>' | |
linkLinks.push(link) | |
}) | |
return linkLinks | |
} | |
// take the link wrapped links or mentions | |
// and inject them back into the tweet | |
function injectLinks(tweet, mentions, linkMentions) { | |
for (var i = 0; i < mentions.length; i++) { | |
var newTweet = tweet.replace(mentions[i], linkMentions[i]) | |
} | |
return newTweet | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment