Following are the steps:
Java script:
<script type="text/javascript">
function GetLatestTweet(user)
{
// set your twitter id
if(user!='')
{
var tweeturl = 'http://twitter.com/statuses/user_timeline.json?screen_name=' + user + '&count=1&callback=?';
// using jquery built in get json method with twitter api, return only one resul
$.getJSON(tweeturl, function(data){
// Get the latest Tweet
if(data[0]!=null)
{
$("#lblLatestTweetHeader").css('display', 'inline');
var tweet = data[0].text;
// Get the created time of the latest Tweet
var tweetCreatedDate=data[0].created_at;
// format date and get duration
var newTweetCreatedDate=parseTwitterDate(tweetCreatedDate);
// process links and reply
tweet = tweet.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, function(url) {
return '<a href="'+url+'">'+url+'</a>';
}).replace(/B@([_a-z0-9]+)/ig, function(reply) {
return reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
});
var dateTimeOfTweet ='<span class="tw_time">'+newTweetCreatedDate+'</span></div>';
tweet='<div class=\"tweetBox\">'+tweet ;
// output the result
$("#tweet").html(tweet+ dateTimeOfTweet);
}
});
}
}
// format date and get duration
function parseTwitterDate(tdate) {
if(tdate!=null){
var system_date = new Date(Date.parse(tdate));
var user_date = new Date();
if (K.ie) {
system_date = Date.parse(tdate.replace(/( \+)/, ' UTC$1'))
}
var tweetdate=tdate; var month = tweetdate.substring(4,7);
var day = tweetdate.substring(8,10);
// get Time difference in seconds
var diff = Math.floor((user_date - system_date) / 1000);
if (diff <= 1) {return "now";}
if (diff < 60) {return " about "+ diff + " seconds ago";}
if (diff <= 120) {return " one minute ago";}
if (diff <= 3600) {return " about "+ Math.floor(diff / 60) + " minutes ago";}
if (diff < 7200) {return " about 1 hour ago";}
if (diff < 86400) {return " about "+ Math.floor(diff / 3600) + " hours ago";}
return " on " +day + " " + month ;
}
}
var K = function () {
var a = navigator.userAgent;
return {
ie: a.match(/MSIE\s([^;]*)/)
}}();
</script>
The function “parseTwitterDate” can be used to get the duration of the tweet
Following code is used in the controler:
/// <summary>
/// Markup format used to get latest Tweet.
/// </summary>
private const string ScriptFormat = "GetLatestTweet(\"{0}\");";
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
base.Render(writer);
SPSecurity.RunWithElevatedPrivileges(delegate
{
var markupBuilder = new StringBuilder();
var tweeterMarkupBuilder = new StringBuilder();
string latestTweetHedding = “Latest Tweet”
markupBuilder.Append("<span id=\"lblLatestTweetHeader\" style=\"display: none;\">");
markupBuilder.Append(latestTweetHedding);
markupBuilder.Append("</span>");
markupBuilder.Append("<p id=\"tweet\"></p>");
markupBuilder.Append("<!-- ctrl -->");
markupBuilder.Append("<table class=\"tblSocial\" width=\"90%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"); string userName = “nirasha”;
if (!string.IsNullOrEmpty(userName))
{
tweeterMarkupBuilder.Append("<script language='javascript'>");
tweeterMarkupBuilder.Append("$(document).ready(function() {");
tweeterMarkupBuilder.Append(string.Format(ScriptFormat, userName));
tweeterMarkupBuilder.Append("});");
tweeterMarkupBuilder.Append("</script>");
}
markupBuilder.Append("</table>");
markupBuilder.Append("<!-- ctrl -->");
// Finally emit the content
writer.Write(tweeterMarkupBuilder.ToString());
writer.Write(markupBuilder.ToString());
});
}
No comments:
Post a Comment