2011-07-19 22:28:48 -04:00
// JSON-P Twitter fetcher for Octopress
2011-09-29 21:04:18 -04:00
// (c) Brandon Mathis // MIT License
2011-07-19 22:28:48 -04:00
/* Sky Slavin, Ludopoli. MIT license. * based on JavaScript Pretty Date * Copyright (c) 2008 John Resig (jquery.com) * Licensed under the MIT license. */
function prettyDate ( time ) {
2011-09-26 09:35:06 -04:00
if ( navigator . appName === 'Microsoft Internet Explorer' ) {
2011-07-19 22:28:48 -04:00
return "<span>∞</span>" ; // because IE date parsing isn't fun.
2011-09-26 09:35:06 -04:00
}
var say = {
just _now : " now" ,
minute _ago : "1m" ,
minutes _ago : "m" ,
hour _ago : "1h" ,
hours _ago : "h" ,
yesterday : "1d" ,
days _ago : "d" ,
2011-09-29 21:04:18 -04:00
last _week : "1w" ,
2011-09-26 09:35:06 -04:00
weeks _ago : "w"
} ;
2011-07-19 22:28:48 -04:00
2011-09-26 09:35:06 -04:00
var current _date = new Date ( ) ,
current _date _time = current _date . getTime ( ) ,
current _date _full = current _date _time + ( 1 * 60000 ) ,
date = new Date ( time ) ,
diff = ( ( current _date _full - date . getTime ( ) ) / 1000 ) ,
day _diff = Math . floor ( diff / 86400 ) ;
2011-07-19 22:28:48 -04:00
2011-09-26 09:35:06 -04:00
if ( isNaN ( day _diff ) || day _diff < 0 ) { return "<span>∞</span>" ; }
2011-07-19 22:28:48 -04:00
2011-09-26 09:35:06 -04:00
return day _diff === 0 && (
2011-07-19 22:28:48 -04:00
diff < 60 && say . just _now ||
diff < 120 && say . minute _ago ||
diff < 3600 && Math . floor ( diff / 60 ) + say . minutes _ago ||
diff < 7200 && say . hour _ago ||
diff < 86400 && Math . floor ( diff / 3600 ) + say . hours _ago ) ||
2011-09-26 09:35:06 -04:00
day _diff === 1 && say . yesterday ||
2011-07-19 22:28:48 -04:00
day _diff < 7 && day _diff + say . days _ago ||
2011-09-29 21:04:18 -04:00
day _diff === 7 && say . last _week ||
2011-09-04 09:56:34 -04:00
day _diff > 7 && Math . ceil ( day _diff / 7 ) + say . weeks _ago ;
2011-07-19 22:28:48 -04:00
}
2011-09-26 09:35:06 -04:00
function linkifyTweet ( text , url ) {
2011-09-28 00:50:46 -04:00
// Linkify urls, usernames, hashtags
text = text . replace ( /(https?:\/\/)([\w\-:;?&=+.%#\/]+)/gi , '<a href="$1$2">$2</a>' )
. replace ( /(^|\W)@(\w+)/g , '$1<a href="http://twitter.com/$2">@$2</a>' )
. replace ( /(^|\W)#(\w+)/g , '$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>' ) ;
// Use twitter's api to replace t.co shortened urls with expanded ones.
2011-09-26 09:35:06 -04:00
for ( var u in url ) {
2011-09-28 00:50:46 -04:00
if ( url [ u ] . expanded _url != null ) {
2011-09-29 21:04:18 -04:00
var shortUrl = new RegExp ( url [ u ] . url , 'g' ) ;
text = text . replace ( shortUrl , url [ u ] . expanded _url ) ;
var shortUrl = new RegExp ( ">" + ( url [ u ] . url . replace ( /https?:\/\// , '' ) ) , 'g' ) ;
text = text . replace ( shortUrl , ">" + url [ u ] . display _url ) ;
2011-09-28 00:50:46 -04:00
}
2011-09-26 09:35:06 -04:00
}
2011-09-28 00:50:46 -04:00
return text
2011-09-26 09:35:06 -04:00
}
function showTwitterFeed ( tweets , twitter _user ) {
var timeline = document . getElementById ( 'tweets' ) ,
content = '' ;
for ( var t in tweets ) {
content += '<li>' + '<p>' + '<a href="http://twitter.com/' + twitter _user + '/status/' + tweets [ t ] . id _str + '">' + prettyDate ( tweets [ t ] . created _at ) + '</a>' + linkifyTweet ( tweets [ t ] . text . replace ( /\n/g , '<br>' ) , tweets [ t ] . entities . urls ) + '</p>' + '</li>' ;
}
timeline . innerHTML = content ;
}
function getTwitterFeed ( user , count , replies ) {
2011-09-28 01:53:44 -04:00
count = parseInt ( count , 10 ) ;
2011-09-28 00:50:46 -04:00
$ . ajax ( {
2011-09-28 01:53:44 -04:00
url : "http://api.twitter.com/1/statuses/user_timeline/" + user + ".json?trim_user=true&count=" + ( count + 20 ) + "&include_entities=1&exclude_replies=" + ( replies ? "0" : "1" ) + "&callback=?"
2011-09-28 00:50:46 -04:00
, type : 'jsonp'
, error : function ( err ) { $ ( '#tweets li.loading' ) . addClass ( 'error' ) . text ( "Twitter's busted" ) ; }
2011-09-28 01:53:44 -04:00
, success : function ( data ) { showTwitterFeed ( data . slice ( 0 , count ) , user ) ; }
2011-09-28 00:50:46 -04:00
} )
}