Customize twitterFetcher JS lib for our needs (e.g. custom templating output).
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
/********************************************************************* | |
### Edit by azappa/emanbrivio ### | |
+ Now it outputs a javascript array of objects so you can stylize how you want output code | |
+ Less parameters in config: | |
* widget id (required) | |
* callback function for visualizing tweets (required) | |
* maxTweets (optional) | |
* lang (optional) | |
+ You can format date by using some library like Moment.js (http://momentjs.com/) or native JS Date functions | |
-- config example -- | |
var twConfig = { | |
'id': '504169112818294784', | |
'maxTweets': 3, | |
'callback': populate | |
}; | |
-- watch output -- | |
function populate(tweets) { | |
tweets.forEach(function (t, i) { | |
console.log(t); | |
} | |
} | |
*********************************************************************/ | |
/********************************************************************* | |
* #### Twitter Post Fetcher v12.0 #### | |
* Coded by Jason Mayes 2013. A present to all the developers out there. | |
* www.jasonmayes.com | |
* Please keep this disclaimer with my code if you use it. Thanks. :-) | |
* Got feedback or questions, ask here: | |
* http://www.jasonmayes.com/projects/twitterApi/ | |
* Github: https://github.com/jasonmayes/Twitter-Post-Fetcher | |
* Updates will be posted to this site. | |
*********************************************************************/ | |
/*jshint browser:true, indent:2, laxcomma:true, loopfunc: true */ | |
'use strict'; | |
var twitterFetcher = function() { | |
var maxTweets = 20; | |
var queue = []; | |
var inProgress = false; | |
var supportsClassName = true; | |
var customCallbackFunction = null; | |
var lang = 'en'; | |
function handleTweets(tweets){ | |
customCallbackFunction(tweets); | |
} | |
function strip(data) { | |
return data.replace(/<b[^>]*>(.*?)<\/b>/gi, function(a,s){ return s; }).replace(/class=".*?"|data-query-source=".*?"|dir=".*?"|rel=".*?"/gi, ''); | |
} | |
function getElementsByClassName (node, classname) { | |
var a = []; | |
var regex = new RegExp('(^| )' + classname + '( |$)'); | |
var elems = node.getElementsByTagName('*'); | |
for (var i = 0, j = elems.length; i < j; i++) { | |
if(regex.test(elems[i].className)){ | |
a.push(elems[i]); | |
} | |
} | |
return a; | |
} | |
return { | |
fetch: function(config) { | |
if (config.maxTweets === undefined) { | |
config.maxTweets = 20; | |
} | |
if (inProgress) { | |
queue.push(config); | |
} else { | |
inProgress = true; | |
maxTweets = config.maxTweets; | |
customCallbackFunction = config.callback; | |
var script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.src = '//cdn.syndication.twimg.com/widgets/timelines/' + config.id + '?&lang=' + (config.lang || lang) + '&callback=twitterFetcher.callback&' + 'suppress_response_codes=true&rnd=' + Math.random(); | |
document.getElementsByTagName('head')[0].appendChild(script); | |
} | |
}, | |
callback: function(data) { | |
var div = document.createElement('div'); | |
div.innerHTML = data.body; | |
if (typeof(div.getElementsByClassName) === 'undefined') { | |
supportsClassName = false; | |
} | |
var tweets = []; | |
var picture = []; | |
var fullname = []; | |
var nickname = []; | |
var times = []; | |
var rts = []; | |
var tids = []; | |
var x = 0; | |
var tmp = div.getElementsByClassName('tweet'); | |
if (supportsClassName) { | |
while (x < tmp.length) { | |
if (tmp[x].getElementsByClassName('retweet-credit').length > 0) { | |
rts.push(true); | |
} else { | |
rts.push(false); | |
} | |
tweets.push(tmp[x].getElementsByClassName('e-entry-title')[0]); | |
tids.push(tmp[x].getAttribute('data-tweet-id')); | |
picture.push(tmp[x].getElementsByClassName('avatar')[0]); | |
fullname.push(tmp[x].getElementsByClassName('p-name')[0]); | |
nickname.push(tmp[x].getElementsByClassName('p-nickname')[0]); | |
times.push(tmp[x].getElementsByClassName('dt-updated')[0]); | |
x++; | |
} | |
} else { | |
while (x < tmp.length) { | |
tweets.push(getElementsByClassName(tmp[x], 'e-entry-title')[0]); | |
tids.push(tmp[x].getAttribute('data-tweet-id')); | |
picture.push(getElementsByClassName(tmp[x], 'avatar')[0]); | |
fullname.push(getElementsByClassName(tmp[x], 'p-name')[0]); | |
nickname.push(getElementsByClassName(tmp[x], 'p-nickname')[0]); | |
times.push(getElementsByClassName(tmp[x], 'dt-updated')[0]); | |
if (getElementsByClassName(tmp[x], 'retweet-credit').length > 0) { | |
rts.push(true); | |
} else { | |
rts.push(false); | |
} | |
x++; | |
} | |
} | |
if (tweets.length > maxTweets) { | |
tweets.splice(maxTweets, (tweets.length - maxTweets)); | |
picture.splice(maxTweets, (picture.length - maxTweets)); | |
fullname.splice(maxTweets, (fullname.length - maxTweets)); | |
nickname.splice(maxTweets, (nickname.length - maxTweets)); | |
times.splice(maxTweets, (times.length - maxTweets)); | |
rts.splice(maxTweets, (rts.length - maxTweets)); | |
} | |
var arrayTweets = []; | |
var y = tweets.length; | |
var n = 0; | |
while(n < y) { | |
var op = {}; | |
op.picture = strip(picture[n].getAttribute('src')); | |
op.fullname = strip(fullname[n].textContent); | |
op.nickname = strip(nickname[n].textContent); | |
op.naked = strip(nickname[n].textContent.replace('@','')); | |
op.tweet = strip(tweets[n].innerHTML); | |
op.timeposted = new Date(times[n].getAttribute('datetime').replace(/-/g,'/').replace('T', ' ').split('+')[0]).toLocaleDateString(); | |
op.tids = tids[n]; | |
arrayTweets.push(op); | |
n++; | |
} | |
handleTweets(arrayTweets); | |
inProgress = false; | |
if (queue.length > 0) { | |
twitterFetcher.fetch(queue[0]); | |
queue.splice(0,1); | |
} | |
} | |
}; | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment